Linked list
Download
1 / 59

Linked List - PowerPoint PPT Presentation


  • 121 Views
  • Uploaded on

Linked List. Joe Meehean. Linked List. …. item N. phead :. item 1. Conceptual Picture N items chained together using pointers p ointed to by variable head Advantage allows list to grow indefinitely without copying constant time inserts and removes just (un)hook into/from chain.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Linked List' - jaunie


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Linked list

Linked List

Joe Meehean


Linked list1
Linked List

item N

phead:

item 1

  • Conceptual Picture

    • N items

    • chained together using pointers

    • pointed to by variable head

  • Advantage

    • allows list to grow indefinitely without copying

    • constant time inserts and removes

    • just (un)hook into/from chain


List node
List Node

item 1

  • Private nested class

    • nested class: class defined within a class

    • private nested: not visible outside outer class

  • Member variables

    • public:

    • T data;

    • Node<T> *next;

  • Constructor

    • Node( const T& data = T(), Node<T>* n = NULL)


Connecting list nodes
Connecting List Nodes

Node<string>* head;

phead:

head = new Node<string>(“ant”);

ant

phead:

head->next = new Node<string>(“bat”);

bat

ant

phead:


DISCUSSION

BREAK!!!

bat

phead:

ant

cat

n:

  • Given head and a pointer n

  • What expression gets node containing:

    • ant

    • bat

    • cat


DISCUSSION

BREAK!!!

bat

phead:

ant

cat

n:

  • Given head and a pointer n

  • What expression gets node containing:

    • ant => *head

    • bat => *(head->next), *n

    • cat => *(head->next->next), *(n->next)


Inserting nodes
Inserting Nodes

tmp:

jar

phead:

cat

bat

ant

n:


Inserting nodes1
Inserting Nodes

tmp:

jar

phead:

cat

bat

ant

n:


Inserting nodes2
Inserting Nodes

tmp:

jar

phead:

cat

bat

ant

n:


Removing nodes
Removing Nodes

n:

jar

phead:

cat

bat

ant


Removing nodes1
Removing Nodes

n:

jar

phead:

cat

bat

ant


Using n odes to make a list
Using Nodes to make a List

  • LCLinkedList<T>

  • Member Variables

    • intnumItems;

    • Node<T>* phead;

  • Methods

    • same ADT as ArrayList

    • also has push_front, pop_front

    • same public methods


Push front
push_front

phead:

cat

bat

tmp:

ant


Push front1
push_front

phead:

cat

bat

tmp:

ant


Push front2
push_front

phead:

cat

bat

tmp:

ant


Push front3
push_front

void push_front(const T& newItem){

Node* tmp = new Node(newItem);

tmp->next = phead;

phead = tmp;

numItems++;

}


Push back
push_back

tmp:

phead:

cat

bat

ant


Push back1
push_back

tmp:

phead:

cat

bat

ant


Push back2
push_back

void push_back(const T& newItem){

// list is empty (special case)

if( phead == NULL ){

phead = new Node(newItem);

}else{

// find the end of the list

Node* pNode = phead;

while(pNode->next != NULL){

pNode = pNode->next;

}

// add the item

pNode->next = new Node(newItem);

// increment the count

numItems++;

}}



Recall push front
Recall: push_front

void push_front(const T& newItem){

Node* tmp = new Node(newItem);

tmp->next = phead;

phead = tmp;

numItems++;

}


Recall push back
Recall: push_back

void push_back(const T& newItem){

// list is empty (special case)

if( phead == NULL ){

phead = new Node(newItem);

}else{

// find the end of the list

Node* pNode = phead;

while(pNode->next != NULL){

pNode = pNode->next;

}

// add the item

pNode->next = new Node(newItem);

// increment the count

numItems++;

}}


Header node
Header Node

phead:

bat

ant

Header

Eliminates special 1st node case for add and remove

Create header at initialization

Don’t count in size

Ignore for contains


Push back with header
push_back (with header)

void push_back(const T& newItem){

// find the end of the list

Node* pNode = phead;

while(pNode->next != NULL){

pNode = pNode->next;

}

// add the item

pNode->next = new Node(newItem);

// increment the count

numItems++;

}


DISCUSSION

BREAK!!!

  • Can you modify LCLinkedList to push_back(…) in O(1)?

    • Hint: You can add member variables


Tail pointer
Tail Pointer

ptail:

phead:

bat

ant

Header

push_back now constant time

but, we must update tail pointer when adding to end


Tail pointer1
Tail Pointer

ptail:

phead:

Header

tail pointer points at header if list is empty


Pop back
pop_back()

ptail:

phead:

bat

ant

Header


Pop back1
pop_back()

phead:

ptail:

bat

ant

Header


Pop back with header and tail pointer
pop_back(with header and tail pointer)

void pop_back(){

if( phead != ptail ){

// find the node just before ptail

Node* pNode = phead;

while(pNode->next != ptail){

pNode= pNode->next;

}

// remove the node

pNode->next = NULL;

ptail = pNode;

// decrement the count

numItems--;

}

}


Pop back2
pop_back()

ptail:

phead:

bat

ant

Header

Tail pointer close to the right position

Still must iterate over nearly entire list

Can we make remove at end O(1)?


DISCUSSION

BREAK!!!

  • Can you modify LCLinkedList to pop_back() in O(1)?

    • Hint: You can modify the structure of the list


Doubly linked list
Doubly Linked List

ptail:

phead:

ant

bat

Header

  • Node

  • Member variables

    • public:

    • T data;

    • Node<T> *next;

    • Node<T> *prev


Tail node
Tail Node

ptail:

phead:

Tail

bat

Header

  • Tail node

    • removes special cases just like header node

    • tail and header node called sentinels


Pop back3
pop_back()

phead:

ptail:

Tail

bat

Header


Pop back4
pop_back()

phead:

ptail:

Tail

bat

Header


Pop back5
pop_back()

phead:

ptail:

Tail

bat

Header


Pop back doubly linked with sentinels
pop_back(doubly linked with sentinels)

void pop_back(){

if( phead->next != ptail ){

ptail->prev = ptail->prev->prev;

ptail->prev->next = ptail;

// decrement the count

numItems--;

}

}


Linked lists vs array lists
Linked Lists vs Array Lists

  • Space

    • linked needs two extra pointers per entry

    • linked needs two extra nodes

    • array may be up to N/2-1 too large

    • array may be way too big if some elements removed

    • array better small data items (e.g., ints)

    • linked way better for big items (e.g., classes)

    • too close to call for medium items (e.g., 3 ints)


Linked lists vs array lists1
Linked Lists vs Array Lists

Time


Linked lists vs array lists2
Linked Lists vs Array Lists

  • Ease implementation

    • fairly close

    • linked list has more special cases and work-arounds for special cases



Expanding list functionality
Expanding List Functionality

  • What if we want to remove an item from the middle of a linked list?

    • remove at i

    • traverse list from beginning until we reach i

    • fix up the pointers

    • O(N)


Expanding list functionality1
Expanding List Functionality

  • What if we were already moving through the list?

    • e.g., remove all even numbers

    • if we did this internally (inside the list class),we would have a pointer to the node to remove

    • O(1)


Linked list iterators
Linked List Iterators

  • LCList::Iterator

  • Public nested class in LCList

  • Member data

    • Node<T> * currNode;

  • Friends

    • LCList<T>


Linked list iterator
Linked List Iterator

Iterator::Iterator() :

currNode(NULL){}

Iterator::Iterator(Node* pNode) :

currNode(pNode){}

void Iterator::operator ++(){

currNode = currNode->next;

}

void Iterator::operator –-(){

currNode = currNode->prev;

}

T& Iterator::operator *(){

return currNode->data;

}


Linked list iterator1
Linked List Iterator

Iterator LCList::begin(){

return Iterator(phead->next);

}

Iterator LCList::end(){

return Iterator(ptail);

}


Remove iterator
remove(Iterator)

phead:

Head

bat

cat

Tail

rat

ant

ptail:

iter:


Remove iterator1
remove(Iterator)

phead:

Head

bat

cat

Tail

rat

ant

ptail:

iter:


Remove iterator2
remove(Iterator)

phead:

Head

bat

cat

Tail

rat

ant

ptail:

iter:


Linked list iterator2
Linked List Iterator

Iterator LCList::remove(Iterator itr){

Node* pNode = iter.currNode;

// make a new iterator to return back

Iterator retVal = (pNode->next);

// update the pointers

pNode->prev->next = pNode->next;

pNode->next->prev = pNode->prev;

// clean up the memory

delete pNode;

// decrement the count

nrItems--;

return retVal;

}


Insert iterator
insert(Iterator)

phead:

Head

bat

cat

Tail

rat

ant

ptail:

iter:


Insert iterator1
insert(Iterator)

phead:

Head

bat

cat

Tail

rat

ant

ptail:

iter:


Insert iterator2
insert(Iterator)

phead:

Head

bat

cat

Tail

rat

ant

ptail:

iter:


Insert iterator3
insert(Iterator)

phead:

Head

bat

cat

Tail

rat

ant

ptail:

iter:


Linked list iterator3
Linked List Iterator

void LCList::insert(Iterator itr,

const T& t){

// extract the node from the iterator

Node* pNode = iter.currNode

// make a new node to store the item

Node* newNode = new Node(t,

pNode->prev,

pNode);

// fix up the pointers

pNode->prev->next = newNode;

pNode->prev = newNode;

// increment the count

nrItems++;

}


Linked list iterators1
Linked List Iterators

  • Potential errors in remove and insert

    • uninitialized iterators

    • iterators for other lists

  • Fixes

    • check iterator’s current node for NULL

    • add a new member value to Iterator:List<T>* myList


Linked lists vs array lists3
Linked Lists vs Array Lists

Time for iterative work



ad