DS Lec 7 Linked List Applications

12
7/23/2019 DS Lec 7 Linked List Applications http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 1/12   Applications of Linked List Doubly Linked List Data Structures Lecture # 7

Transcript of DS Lec 7 Linked List Applications

Page 1: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 1/12

 

 Applications of Linked List

Doubly Linked List

Data Structures

Lecture # 7

Page 2: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 2/12

 

 Application of Dynamic List

int main()

SinglyLinkedList<char> *l = new SinglyLinkedList<char>();

head tail

Page 3: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 3/12

 

 Application of Dynamic List

int main()

{ SinglyLinkedList<char> *l = ne SinglyLinkedList<char>!"

l->addToHead(a!);l 

tail

head

a

Page 4: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 4/12

 

 Application of Dynamic List

int main()

{ SinglyLinkedList<char> *l = ne SinglyLinkedList<char>!"

l ->addToHead(a!);

l ->addToHead("!);

  l ->addToTail(c!);

  l ->addToTail(d!);

  l ->delete#romTail()  l ->delete(a!);

  ret$rn %;

 &

head

 b c

tail

Page 5: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 5/12

 

Dynamic Stack

$ %e can implement the stack dynamically using the linkedlist&

$ Declaration of type Dynamic Stack is'

class 'ynamicStack { 

 riate

+ode *to; ,,ointer to to node o stack 

 $"lic

'ynamicStack()to(%){ &

.'ynamicStack();

 &;

s -> top

Empty Stack 

DynamicStack *s = new DynamicStack;

 // Add other functions of stack …………..

Page 6: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 6/12

 

Dynamic Stack

$ (re)iously e implemented the stack datastructure as an array&

$ %e ha)e seen that in a stack elements areinserted !pushed" and remo)ed !popped" at thesame end of the list the top of the stack&

$ Since the element that is remo)ed first is the onethat has been aiting the shortest length of timea stack is called a last+in first+out list !L,-."&

$  A stack can also be implemented as a linked listin hich all insertions and deletions areperformed at the list head&

Page 7: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 7/12

 

Dynamic /ueue

  0hink o)er it1

It is Easy to make thingsHard

It is Hard to make things

Easy

Page 8: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 8/12

 

Doubly Linked List

$ (roblem ith the member function of

singly linked list delete#romTail()

$Doubly linked list can be tra)ersed in bothdirections

a1 a2 a3 a4 a5

Page 9: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 9/12

 

,mplementation of Doubly Linked List

class +ode { 

 $"lic

int ino;

+ode *ne/t0 *re;

+ode()ino(%) { ne/t = re = %;

 &

+ode(int el0 +ode *n = %0 +ode * = %) { 

ino = el;

ne/t = n; re = ;

 &

 &;

Page 10: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 10/12

 

class 'o$"lyLinkedList { 

 riate

+ode *head0 *tail;

 $"lic'o$"lyLinkedList(){ 

head = tail =%;

 &

oid addTo'LLTail(int)

oid delete#rom'LLTail();

1111111111122

 &;

,mplementation of Doubly Linked List

Page 11: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 11/12

 

,nsert 2ode at the 3nd of a Doubly Linked List

oid 'o$"lyLinkedList addTo'LLTail(int el)

{ i(head == %) ,,List is emty  

1 5

head tail

head = tail = new !ode"el#; 1

head tail

else // Insert node at the end of the list 

{  tail = new Node(e1, 0, tail);

$

tail

  tail ! "re# ! ne$t = tail 

  %

 %

Page 12: DS Lec 7 Linked List Applications

7/23/2019 DS Lec 7 Linked List Applications

http://slidepdf.com/reader/full/ds-lec-7-linked-list-applications 12/12

 

Delete 2ode from the 3nd of a Doubly Linked List

oid 'o$"lyLinkedList delete#rom'LLTail()

{ i(head 3= %) ,,List not emty  

i(head == tail) { ,,4 only one node in the list 

1 5

head tail

1

head tail

delete head; head = tail = 0; %else // &ore then one nodes in the list  

{ tail = tail ! "re#; tail

delete tail ! ne$t;  tail ! ne$t = 0;

  %

 %