Linked List. List Definitions Linear relationship Each element except the first has a unique...

65
Linked List

Transcript of Linked List. List Definitions Linear relationship Each element except the first has a unique...

Page 1: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Linked List

Page 2: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

List Definitions

• Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique successor.

• Length The number of items in a list; the length can vary over time.

Page 3: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Abstract Data Type (ADT)

• A data type whose properties (domain and operations) are specified independently of any particular implementation.

• Logical (or ADT) level: abstract view of the domain and operations.

• Implementation level: specific representation of the structure to hold the data items, and the coding for operations.

Page 4: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

4 Basic Kinds of ADT Operations

• Constructor/Destructor – creates/deletes a new instance (object) of an ADT.

• Transformer -- changes the state of one or more of the data values of an instance.

• Observer -- allows us to observe the state of one or more of the data values of an instance without changing them.

• Iterator -- allows us to process all the components in a data

structure sequentially.

Page 5: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

ADT List Operations

• Transformers – MakeEmpty( ) – InsertItem ( )– DeleteItem( )

• Observers – LengthIs( )– RetrieveItem( )

• Iterators – ResetList( )– GetNextItem( )

change state

observe state

process all

Page 6: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

class List{public :

void UnsortedType ( ) ; int LengthIs ( ) const ; void RetrieveItem (ItemType& item,

bool& found ) ;void InsertItem ( ItemType item ) ; void DeleteItem ( ItemType item );void ResetList ( );void GetNextItem ( ItemType& item );

private :int length ; ItemType info[MAX_ITEMS] ; int currentPos ;

} ;

Class List

use array to

store data

Page 7: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Singly Linked List Diagram

Pointer to front node

A B C NULL

Nodes

Data carried in node Pointer to next node

Page 8: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Node from Shop Program Diagram

A

Item item; Node *link;

class Node {public: Node(Item i);private: Item item; Node *link;};

Page 9: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Empty Insert Example (1 of 6)

size

prev cur pos

0

anItem

A? ?

void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting A at position 0 in empty list

front

0

Page 10: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Empty Insert Example (2 of 6)

size

prev cur pos

0

anItem

A? ?

assert(pos >= 0 && pos <= size); size++;

front

1

Page 11: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Empty Insert Example (3 of 6)

size

prev cur pos

0

anItem

A?

if (pos == 0) { // Inserting at the front cur = front;

front

1

Page 12: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Empty Insert Example (4 of 6)

size

prev cur pos

0

anItem

A?

front = new Node(anItem);

front

1

A

item link

?

Page 13: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Empty Insert Example (5 of 6)

size

prev cur pos

0

anItem

A?

front->link = cur;

front

1

A

item link

Page 14: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Empty Insert Example (6 of 6)

size

return; }

front

1

A

item link

Page 15: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Front Insert Example (1 of 6)

front

A B C

item link item link item link

prev cur pos

0

anItem

D? ?

void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting D at position 0

size

3

Page 16: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Front Insert Example (2 of 6)

front

A B C

item link item link item link

prev cur pos

0

anItem

D? ?

assert(pos >= 0 && pos <= size); size++;

size

4

Page 17: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Front Insert Example (3 of 6)

front

A B C

item link item link item link

prev cur pos

0

anItem

D?

if (pos == 0) { // Inserting at the front cur = front;

size

4

Page 18: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Front Insert Example (4 of 6)

front

A B C

item link item link item link

prev cur pos

0

anItem

D?

front = new Node(anItem);

size

4

D

item link

?

Page 19: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Front Insert Example (5 of 6)

front

A B C

item link item link item link

prev cur pos

0

anItem

D?

front->link = cur;

size

4

D

item link

Page 20: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Front Insert Example (6 of 6)

front

A B C

item link item link item link

return; }

size

4

D

item link

Page 21: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Middle Insert Example (1 of 8)void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting D at position 2

front

A B C

item link item link item link

prev cur pos

2

anItem

D? ?

size

3

Page 22: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Middle Insert Example (2 of 8) assert(pos >= 0 && pos <= size); size++;

front

A B C

item link item link item link

prev cur pos

2

anItem

D? ?

size

4

Page 23: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Middle Insert Example (3 of 8) prev = NULL; cur = front;

front

A B C

item link item link item link

prev cur pos

2

anItem

D

size

4

Page 24: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Middle Insert Example (4 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; }

First Iteration

front

A B C

item link item link item link

prev cur pos

1

anItem

D

size

4

Page 25: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Middle Insert Example (5 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; }

Second Iteration

front

A B C

item link item link item link

prev cur pos

0

anItem

D

size

4

Page 26: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Middle Insert Example (6 of 8) prev->link = new Node(anItem);

front

A B C

item link item link item link

prev cur pos

0

anItem

D

size

4

D ?

item link

Page 27: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Middle Insert Example (7 of 8) prev->link->link = cur;

front

A B C

item link item link item link

prev cur pos

0

anItem

D

size

4

D

item link

Page 28: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Middle Insert Example (8 of 8)}

front

A B C

item link item link item link

size

4

D

item link

Page 29: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

End Insert Example (1 of 8)void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting D at position 3

front

A B C

item link item link item link

prev cur pos

3

anItem

D? ?

size

3

Page 30: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

End Insert Example (2 of 8) assert(pos >= 0 && pos <= size); size++;

front

A B C

item link item link item link

prev cur pos

3

anItem

D? ?

size

4

Page 31: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

End Insert Example (3 of 8) prev = 0; cur = front;

front

A B C

item link item link item link

prev cur pos

3

anItem

D0

size

4

Page 32: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

End Insert Example (4 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; }

First Iteration

front

A B C

item link item link item link

prev cur pos

2

anItem

D

size

4

Page 33: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

End Insert Example (5 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; }

Second Iteration

front

A B C

item link item link item link

prev cur pos

1

anItem

D

size

4

Page 34: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

End Insert Example (6 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; }

Third Iteration

front

A B C

item link item link item link

prev cur pos

0

anItem

D

size

4

Page 35: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

End Insert Example (7 of 8) prev->link = new Node(anItem); prev->link->link = cur;

front

A B C

item link item link item link

prev cur pos

0

anItem

D

size

4

D

item link

Page 36: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

End Insert Example (8 of 8) }

front

A B C

item link item link item link

size

4

D

item link

Page 37: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Remove Example (1 of 7)void ShoppingList::remove(int pos) { Node *cur, *prev; Removing at position 1

front

A B C

item link item link item link

prev cur pos

1? ?

size

3

Page 38: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Remove Example (2 of 7) assert(pos >= 0 && pos < size); size--;

front

A B C

item link item link item link

prev cur pos

1? ?

size

2

Page 39: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Remove Example (3 of 7) prev = NULL; cur = front;

front

A B C

item link item link item link

prev cur pos

1

size

2

Page 40: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Remove Example (4 of 7) while (pos > 0) { prev = cur; cur = cur->link; pos--; }

front

A B C

item link item link item link

prev cur pos

0

size

2

First iteration

Page 41: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Remove Example (5 of 7) prev->link = cur->link;

front

A B C

item link item link item link

prev cur pos

0

size

2

Page 42: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Remove Example (6 of 7) delete cur;

front

A B C

item link item link item link

prev cur pos

0

size

2

Page 43: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Remove Example (7 of 7)}

front

A C

item link item link

size

2

Page 44: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Delete All Example (1 of 8)void ShoppingList::deleteAll() { Node *kil;

front

A B C

item link item link item link

kil

?

size

3

Page 45: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Delete All Example (2 of 8) while (front != NULL) { kil = front; front = front->link;

A B C

item link item link item link

kil

frontsize

3

First iteration

Page 46: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Delete All Example (3 of 8) kil->link = NULL; delete kil; }

A B C

item link item link item link

kil

frontsize

3

First iteration

Page 47: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Delete All Example (4 of 8) while (front != NULL) { kil = front; front = front->link;

B C

item link item link

kil

frontsize

3

Second iteration

Page 48: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Delete All Example (5 of 8) kil->link = NULL; delete kil; }

B C

item link item link

kil

frontsize

3

Second iteration

Page 49: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Delete All Example (6 of 8) while (front != NULL) { kil = front; front = front->link;

C

item link

kil

frontsize

3

Third iteration

Page 50: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Delete All Example (7 of 8) kil->link = 0; delete kil; }

C

item link

kil

frontsize

3

Third iteration

0

Page 51: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Delete All Example (8 of 8) size = 0;}

frontsize

0

Page 52: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Copy Constructors and Overloaded = Operators

• Copy constructors used to make copies when passing by value and returning by value in functions

• Operator = used for assignment

• Both have default behaviors if not user-defined, which is to do a bitwise copy of all member data

• The default behavior can be a problem when there are pointers in member data (shallow copying)

• Users should define their own copy constructor and overloaded operator = when they are creating a class with pointer data members

Page 53: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Deep Copying (1 of 3)

front

A B C 0

item link item link item linksize

3

slist1

slist2front

X Y 0

item link item linksize

2

• Consider the following two ShoppingList objects

• What would the result of slist2 = slist1; be if the default copy behavior took place?

Page 54: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Deep Copying (2 of 3)

front

A B C 0

item link item link item linksize

3

slist1

slist2front

X Y 0

item link item linksize

3

• slist2 becomes a “shallow copy” of slist1• slist2 doesn’t get a copy of slist1 data

• All references to slist2 data are lost (mem. leak)

Page 55: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Deep Copying (3 of 3)

front

A B C 0

item link item link item linksize

3

slist1

slist2frontsize

3

• Here is what slist2 would look like as a “deep copy” of slist1

• Note that slist2 has its own set of data

A B C 0

item link item link item link

Page 56: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Clone Example (1 of 8)void ShoppingList::clone(const ShoppingList &slist) { Node *cur1, *cur2;

front

A B C 0

item link item link item linksize

3

slist

*thisfront

X Y 0

item link item linksize

2

cur1

?

cur2

?

Page 57: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Clone Example (2 of 8) this->deleteAll(); this->size = slist.size;

front

A B C 0

item link item link item linksize

3

front

0

size

3

cur1

?

cur2

?slist

*this

Page 58: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Clone Example (3 of 8) this->front = new Node(*(slist.front));

front

A B C 0

item link item link item linksize

3

frontsize

3

cur1

?

cur2

?

A

item link

0

slist

*this

Page 59: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Clone Example (4 of 8) cur1 = this->front; cur2 = slist.front;

front

A B C 0

item link item link item linksize

3

frontsize

3

cur1 cur2

A

item link

0

slist

*this

Page 60: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Clone Example (5 of 8) while (cur2->link) { cur2 = cur2->link; cur1->link = new Node(*cur2); cur1 = cur1->link; }

front

A B C 0

item link item link item linksize

3

frontsize

3

cur1 cur2

A

item link

slist

*this

B

item link

0

First iteration

Page 61: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Clone Example (6 of 8) while (cur2->link) { cur2 = cur2->link; cur1->link = new Node(*cur2); cur1 = cur1->link; }

front

A B C 0

item link item link item linksize

3

frontsize

3

cur1 cur2

A

item link

slist

*this

B

item link

Second iteration

C 0

item link

Page 62: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Clone Example (7 of 8) cur1 = cur2 = 0;

front

A B C 0

item link item link item linksize

3

frontsize

3

cur1

0

cur2

0

A

item link

slist

*this

B

item link

C 0

item link

Page 63: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Clone Example (8 of 8)}

front

A B C 0

item link item link item linksize

3

frontsize

3 A

item link

slist

*this

B

item link

C 0

item link

Page 64: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Doubly Linked List

• Each node has 2 pointers, one to the node before and one to the node after

• No longer limited to left-to-right pointer movement

• Typically, a rear pointer is employed to simplify reverse traversals

front

A

data rlinkllink

B

data rlinkllink

C

data rlinkllink

rear

Page 65: Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.

Circular Linked List

• “Last” node’s link points at the “front” node

• Concept of “front” optional (alternative: “current”)

cur

A

data link

B

datalink

Cdata

link

Edata

link

D

datalink