0% found this document useful (0 votes)
26 views54 pages

List Part1 Handout

Uploaded by

Vector Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views54 pages

List Part1 Handout

Uploaded by

Vector Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

C ONCEPTS L IST ADT I MPLEMENTATION

Fundamental Data Structures


Part1: Lists

Phung Hua Nguyen


HCMC University of Technology

April 6, 2020
C ONCEPTS L IST ADT I MPLEMENTATION

O UTLINE

C ONCEPTS

L IST ADT

I MPLEMENTATION
Array-based
Linked
C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

I S I T A L IST ?


C ONCEPTS L IST ADT I MPLEMENTATION

W HAT IS A L IST ?

A list Not a list

List is a finite, ordered sequence of data items, known as


elements. So, each element, except the last, has a unique
successor.

<3, 4, 2, 6, 1 >
C ONCEPTS L IST ADT I MPLEMENTATION

W HAT CAN YOU DO ON A LIST ?

I on the whole list


I clear the list,
clear(<3, 4, 2, 6, 1 >) ⇒ <>
I concatenate two lists,
<3, 4, 2, 6, 1 >+ <5, 9 >⇒ <3, 4, 2, 6, 1, 5, 9 >
I sort the list,
sort(<3, 4, 2, 6, 1 >) ⇒ <1, 2, 3, 4, 6 >
I length of the list,
length(<3, 4, 2, 6, 1 >) ⇒ 5
I reverse the list,
reverse(<3, 4, 2, 6, 1 >) ⇒ <1, 6, 2, 4, 3 >
I on an element of the list
C ONCEPTS L IST ADT I MPLEMENTATION

W HAT CAN YOU DO ON A LIST ?

I on an element of the list


I cursor at a position
I move cursor to the beginning/first
moveToFirst(<3, 4, 2, 6,| 1 >) ⇒<| 3, 4, 2, 6, 1 >
I move cursor to the last
moveToLast(<3, 4,| 2, 6, 1 >) ⇒ <3, 4, 2, 6, 1 |>
I move cursor to position i
moveToPos(<| 3, 4, 2, 6, 1 >,2) ⇒ <3, 4,| 2, 6, 1 >
I move cursor forward
next(<3, 4,| 2, 6, 1 >) ⇒ <3, 4, 2,| 6, 1 >
I move cursor backward
prev(<3, 4,| 2, 6, 1 >) ⇒ <3,| 4, 2, 6, 1 >
I position of the cursor
currPos(<3, 4,| 2, 6, 1 >) ⇒ 2
C ONCEPTS L IST ADT I MPLEMENTATION

W HAT CAN YOU DO ON A LIST ?

I on an element of the list


I get value of the element at the cursor
getValue(<3, 4,| 2, 6, 1 >) ⇒ 2
I insert an element at the cursor/a position
insert(<3, 4,| 2, 6, 1 >,9) ⇒ <3, 4,| 9, 2, 6, 1 >
I delete an element at a position/the cursor
remove(<3, 4,| 2, 6, 1 >) ⇒ <3, 4,| 6, 1 >
C ONCEPTS L IST ADT I MPLEMENTATION

L IST ADT

template <typename T>


class List { // List ADT

public:
List() {}
virtual ˜List() {}

virtual void clear() = 0;


virtual int length() const = 0;
virtual void print() const = 0;
C ONCEPTS L IST ADT I MPLEMENTATION

L IST ADT

virtual void moveToStart() = 0;


virtual void moveToEnd() = 0;
virtual void moveToPos(int pos) = 0;
virtual void prev() = 0;
virtual void next() = 0;
virtual int currPos() const = 0;

virtual const T& getValue() const = 0;

virtual void insert(const T& item) = 0;


virtual void append(const T& item) = 0;

virtual T remove() = 0;
};
C ONCEPTS L IST ADT I MPLEMENTATION

E XAMPLE

I To iterate on the list to do something


for(L.moveToStart();
L.currPos() < L.length();
L.next()) {
it = L.getValue();
dosomething(it);
}
C ONCEPTS L IST ADT I MPLEMENTATION

E XAMPLE

I To print all elements in the list


for(L.moveToStart();
L.currPos() < L.length();
L.next()) {
it = L.getValue();
cout << it << " ";
}
C ONCEPTS L IST ADT I MPLEMENTATION

I MPLEMENTATION

I Array-based List

3 4 2 6 1
0 1 2 3 4 5 6 7 8

I Linked List

3 4 2 6 1
C ONCEPTS L IST ADT I MPLEMENTATION

A RRAY- BASED L IST

listSize
3 4 2 6 1
0 1 2 3 4 5 6 7 8

MAXSIZE
I How to implement a cursor?
I How to move the cursor?
I How to get the value of a list at the cursor position?
I What is the complexity of these actions?
C ONCEPTS L IST ADT I MPLEMENTATION

A RRAY- BASED L IST I MPLEMENTATION


template <typename T>
class AList : public List<T> {
private:
T * arr;
int listSize = 0;
int cursor = 0;
const int MAXSIZE = 50;
public:
AList() {
arr = new T[MAXSIZE];
}
...
};
...
AList<int> x;
AList<int>* p = new AList<int>();
C ONCEPTS L IST ADT I MPLEMENTATION

M OVE THE CURSOR TO POSITION POS

void moveToPos(int pos) {


Assert((pos>=0)&&(pos<=listSize),"Pos out of range")
cursor = pos;
}
What is the time complexity of this method?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 2 6 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

listSize = 5
I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 2 6 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 2 6 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 2 6 1 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 2 6 1 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 2 6 6 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 2 6 6 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 2 2 6 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 2 2 6 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 9 2 6 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT A NEW ELEMENT TO AN ARRAY- BASED LIST

listSize = 6
I insert(<3, 4, | 2, 6, 1 >, 9) 3 4 9 2 6 1
I cursor = 2 0 1 2 3 4 5 6 7 8

I What is the worst case complexity of the insertion


algorithm?
I Write the insertion algorithm?
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT M ETHOD

void insert(const T& it) {


Assert(listSize < MAXSIZE, "List capacity exceeded");
for(int i=listSize; i>cursor; i--)
arr[i] = arr[i-1]; //Shift elements up
arr[cursor] = it; // to make room
listSize++; // Increment list size
}
C ONCEPTS L IST ADT I MPLEMENTATION

L INKED L IST

<3, 4, | 2, 6, 1 >

head curr last

3 4 2 6 1
C ONCEPTS L IST ADT I MPLEMENTATION

L INKED L IST E LEMENT


template <typename T>
class Link {
public:
T data;
Link<T>* next;
//Constructors
Link(const T& dataval, Link<T>* nextval=NULL)
{
data = dataval;
next = nextval;
}
Link(Link<T>* nextval = NULL)
{
next = nextval;
}
}
C ONCEPTS L IST ADT I MPLEMENTATION

E XAMPLE

z y x

4 5 3

Link<int> * x = new Link<int>(3);


Link<int> * y = new Link<int>(5,x);
Link<int> * z = new Link<int>(y);
z -> data = 4;
cout << z -> data << '\n';
cout << z -> next -> data << '\n';
cout << z -> next -> next -> data << '\n';
C ONCEPTS L IST ADT I MPLEMENTATION

L INKED L IST

<3, 4, | 2, 6, 1 >

head curr last

3 4 2 6 1
C ONCEPTS L IST ADT I MPLEMENTATION

L INKED L IST T YPE

template <typename T>


class LList: public List<T>{
private:
Link<T>* head;
Link<T>* last;
Link<T>* curr;
int listSize;
public:
LList() {
head = last = curr = new Link<T>(NULL);
listSize = 0;
}
...
};
C ONCEPTS L IST ADT I MPLEMENTATION

E XAMPLE

Write a constructor that creates a linked list with one element


whose value is passed through the parameter of the
constructor.
LList(const T& val)
head curr last

LList(const T& val) {


last = new Link<T>(val);
head = curr = new Link<T>(last);
listSize = 1;
}
C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT AN ELEMENT INTO A LINKED LIST


insert(<3, 4, | 2, 6, 1 >,9) ⇒ <3, 4, | 9, 2, 6, 1 >

head curr last

3 4 2 6 1

What is the complexity of the insertion algorithm?


C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT AN ELEMENT INTO A LINKED LIST


insert(<3, 4, | 2, 6, 1 >,9) ⇒ <3, 4, | 9, 2, 6, 1 >

head curr 9 last

3 4 2 6 1

1.create a new node containing the new data

What is the complexity of the insertion algorithm?


C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT AN ELEMENT INTO A LINKED LIST


insert(<3, 4, | 2, 6, 1 >,9) ⇒ <3, 4, | 9, 2, 6, 1 >

head curr 9 last

3 4 2 6 1

2. its next points to current node

What is the complexity of the insertion algorithm?


C ONCEPTS L IST ADT I MPLEMENTATION

I NSERT AN ELEMENT INTO A LINKED LIST


insert(<3, 4, | 2, 6, 1 >,9) ⇒ <3, 4, | 9, 2, 6, 1 >

head curr 9 last

3 4 2 6 1

3. change curr → next to the new node.

What is the complexity of the insertion algorithm?


C ONCEPTS L IST ADT I MPLEMENTATION

I NSERTION ALGORITHM

1. create a new node containing the new data


2. its next points to current node
3. change curr → next to the new node.
void insert(const T& it) {
curr->next = new Link<T>(it, curr->next);
if (last == curr)
last = curr->next;
listSize++;
}
C ONCEPTS L IST ADT I MPLEMENTATION

D ELETE THE ELEMENT AT THE CURSOR

remove(<3, 4, | 2, 6, 1 >) ⇒ <3, 4, | 6, 1 >

head curr temp last

3 4 6 1
C ONCEPTS L IST ADT I MPLEMENTATION

D ELETE THE ELEMENT AT THE CURSOR

remove(<3, 4, | 2, 6, 1 >) ⇒ <3, 4, | 6, 1 >

head curr temp last

3 4 2 6 1
C ONCEPTS L IST ADT I MPLEMENTATION

D ELETE THE ELEMENT AT THE CURSOR

remove(<3, 4, | 2, 6, 1 >) ⇒ <3, 4, | 6, 1 >

head curr temp last

3 4 2 6 1
C ONCEPTS L IST ADT I MPLEMENTATION

D ELETE THE ELEMENT AT THE CURSOR

remove(<3, 4, | 2, 6, 1 >) ⇒ <3, 4, | 6, 1 >

head curr temp last

3 4 2 6 1
C ONCEPTS L IST ADT I MPLEMENTATION

D ELETE THE ELEMENT AT THE CURSOR

remove(<3, 4, | 2, 6, 1 >) ⇒ <3, 4, | 6, 1 >

head curr temp last

3 4 6 1

What is the complexity of the removal algorithm?


C ONCEPTS L IST ADT I MPLEMENTATION

W HAT SHOULD DO WHEN KILLING A LINKED LIST ?

Remove all elements of the list before killing the list


˜LList(){
while (head != NULL) {
curr = head;
head = head -> next;
delete curr;
}
}

You might also like