DS Lec 6 Linked List Implementation

17
7/23/2019 DS Lec 6 Linked List Implementation http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 1/17  Linked List: Insertion and Deletion Linked List: Insertion and Deletion Lecture # 6 Lecture # 6

Transcript of DS Lec 6 Linked List Implementation

Page 1: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 1/17

 

Linked List: Insertion and DeletionLinked List: Insertion and Deletion

Lecture # 6Lecture # 6

Page 2: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 2/17

 

Implementation of Singly LinkedImplementation of Singly Linked

ListList To implement the singly linked list weTo implement the singly linked list we

implement two classes i.e.implement two classes i.e.

One class for nodes of the list &One class for nodes of the list &

Another class for access to the listAnother class for access to the list

Page 3: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 3/17

 

Class for nodes of the listClass for nodes of the list

class Node {class Node {

 pulic! pulic!

int info"int info"

 Node ne$t" Node ne$t" Node%!ne$t%' {( Node%!ne$t%' {(

 Node%int el) Node n * ' { Node%int el) Node n * ' {

info * el"info * el"

ne$t * n"ne$t * n"((

("("

Page 4: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 4/17

 

Class for Singly Linked ListClass for Singly Linked List

class +inglyLinkedList {class +inglyLinkedList {

 pri,ate! pri,ate!

 Node head Node head"--pointer to first node of a list"--pointer to first node of a list

 Node tail Node tail ""--pointer to last node of a list--pointer to last node of a list

 pulic! pulic!+inglyLinkedList%!head%')tail%' {(+inglyLinkedList%!head%')tail%' {(

+inglyLinkedList%"+inglyLinkedList%"

/ontinue on ne$t slide0

Page 5: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 5/17

 

 ool is1mpty%" ool is1mpty%"--list is empty--list is empty

,oid addTo2ead%int el",oid addTo2ead%int el" --inserts an element in front of list--inserts an element in front of list

,oid addToTail%int el",oid addToTail%int el"--inserts an element at the end of list--inserts an element at the end of list

,oid delete3rom2ead%",oid delete3rom2ead%"--delete a node from the front of a list--delete a node from the front of a list

,oid delete3romTail%",oid delete3romTail%"--delete a node form the end of a list--delete a node form the end of a list

,oid delete%int el",oid delete%int el"--delete a node form list whose ,alue is el--delete a node form list whose ,alue is el

 Node find%int el" Node find%int el"--3ind the node from list whose ,alue is el--3ind the node from list whose ,alue is el--and returns its pointer.  --and returns its pointer.

,oid prints%",oid prints%"--prints the contents of a list--prints the contents of a list

 ool is4nList%int el" ool is4nList%int el"--check that an element el e$ists in a list or not--check that an element el e$ists in a list or not

("("

Class for Singly Linked ListClass for Singly Linked List

Page 6: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 6/17

 

Insert Node in front of a listInsert Node in front of a list

Adding a node at the eginning of a linked list is performed inAdding a node at the eginning of a linked list is performed in

four steps.four steps.

5.5. An empty node is created. 4t is empty in the sense that theAn empty node is created. 4t is empty in the sense that the

 program performing insertion does not assign any ,alues to program performing insertion does not assign any ,alues to

the data memers of the node.the data memers of the node.

a

head

  c d

tail2. The node's info member is initialized to a particular value.

a

head

  c d

tail

e

Page 7: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 7/17

 

Insert Node in front of a listInsert Node in front of a list

.. 7ecause the node is eing included at the front of the list) the7ecause the node is eing included at the front of the list) the

ne$t memer ecomes a pointer to the first node on the list)ne$t memer ecomes a pointer to the first node on the list)

i.e. the current ,alue of headi.e. the current ,alue of head

4. The new node precedes all the nodes on the list, but this fact

has to be reflected in the value of head; otherwise the new node

is not accessible. Therefore head is updated to become the

pointer to the new node

a

head

  c d

tail

e

a

head

  c d

tail

e

Page 8: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 8/17

 

Insert Node in front of a listInsert Node in front of a list

void SinglyLinkedList::addToHead(int el void SinglyLinkedList::addToHead(int el  ) )

{ {   Node *n = new Node(e1, 0); Node *n = new Node(e1, 0);  //Creates new node //Creates new node

tail

head

if (isEmpty)  //first node of a list 

head = tail = n;

else  // Insert node in front of the list 

n

e a

head

  c d

tail

n

e

n->next = head;

head = n;

head

  }

 }

Page 9: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 9/17

 

Insert Node at end of a listInsert Node at end of a list The process of adding a new node to the end of the list hasThe process of adding a new node to the end of the list has

fi,e steps.fi,e steps.

5.5. An empty node is created.An empty node is created.a

head

  c d

tail2. The node’s info member is initialized to a value

a

head

  c d

tail

e

3. Because the node is bein included at the end of the list, the ne!t

member is set to null.a

head

  c d

tail

e

Page 10: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 10/17

 

Insert Node at end of a listInsert Node at end of a list8.8. The node is now included in the list y making the ne$tThe node is now included in the list y making the ne$t

memer of the last node of the list a pointer to the newlymemer of the last node of the list a pointer to the newly

created node.created node.

". The new node follows all the nodes of the list, but this fact has

to be reflected in the value of tail, which now becomes the

pointer to the new node.

a

head

  c d

tail

e

a

head

  c d

tail

e

Page 11: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 11/17

 

Insert Node at end of a listInsert Node at end of a list

void SinglyLinkedList::void SinglyLinkedList::addToTail(int el)addToTail(int el)

{ {   Node *n = new Node(e1, 0); Node *n = new Node(e1, 0);  //Creates new node //Creates new node

n

e

tail

headif(isEmpty)  //First node of a list 

head = tail = n;

else  //Insert node at the end of a list 

n

ea

head

  c d

tail

tail->next = n;tail = n;

tail

  }

 }

Page 12: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 12/17

 

Delete Node from front of a listDelete Node from front of a list

void SinglyLinkedList:: deleteFromHeadl()void SinglyLinkedList:: deleteFromHeadl()

{{ if%head 9* 'if%head 9* ' --Non empty list--Non empty list

  {{

#ode $n % head;

&fhead %% tail( ))if onl* one node in the list

n

d

tail

head

else

a

head

  c d

tailn

head % head+ne!t;

head

  delete n; -

  -

head % tail % ;

Page 13: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 13/17

 

Delete Node from end of a listDelete Node from end of a listvoid SinglyLinkedList:: deleteFromTail()void SinglyLinkedList:: deleteFromTail()

{{  if%head 9* 'if%head 9* ' --Non empty list--Non empty list

  if%head * * tailif%head * * tail --Only one node in a list--Only one node in a list

  {{

a

head

  c d

tailn

else/ #ode $n % head;

 whilen+ne!t 0% tail( n % n+ne!t;

n

tail % n;

delete tail;

  tail+ne!t % ;

  -

d

tail

headdelete head;

  head % tail %   -

tail

Page 14: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 14/17

 

Delete Node from ListDelete Node from List

void SinglyLinkedList:: Delete(int el)void SinglyLinkedList:: Delete(int el)

This function deletes a node whose entry is el from the listThis function deletes a node whose entry is el from the list

  a c d

header  :eletion of a node containing c from a linked list

The figure shows how the node containing c is deleted..

Page 15: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 15/17

 

Delete Node from ListDelete Node from List

void SinglyLinkedList:: Delete(T el)void SinglyLinkedList:: Delete(T el)

{ {  if(head != 0)if(head != 0)  //Non empty list  //Non empty list 

if(head == tail && el == head info)if(head == tail && el == head info)  //only one node in a list  //only one node in a list  

{ { d

tail

head

delete head;  head = tail = 0; }else if(el == head -> info)  //List has more then one nodes { 

a

head

  c d

tailtemp

ode !temp = head;

head = head -> next;

head

  delete temp;  //"n #ld head is deleted 

 }

$ontin%e on next slide&''

Page 16: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 16/17

 

Delete Node from ListDelete Node from List

else { else {  //"ist has more then one nodes and non head node is //"ist has more then one nodes and non head node is deleted deleted 

ode !tmp = head;

hile(tmp = 0 ** tmp -> next ->info = el)

tmp = tmp -> next;

a

head

  c d

tail

If(tmp = 0) {  //ode of +al%e el exists

tmp -> next = tmp -> next -> next;

If(d == tail)  //last node of a list 

tail = tmp;

tmp

  delete d;

  }

  } }

 Node * d = tmp ne#t;

d

Page 17: DS Lec 6 Linked List Implementation

7/23/2019 DS Lec 6 Linked List Implementation

http://slidepdf.com/reader/full/ds-lec-6-linked-list-implementation 17/17

 

DestrutorDestrutor

void SinglyLinkedList :: ~SinglyLinkedList()void SinglyLinkedList :: ~SinglyLinkedList()

{ { 

if(!is$mpty())if(!is$mpty())  //"ist is not empty //"ist is not empty

while(head != 0)while(head != 0)delete%romead();delete%romead();

 ' '