Successfully reported this slideshow.
Your SlideShare is downloading. ×

Stack, Queue, Linked List.pptx

Ad

Prof. Sonali
Mhatre
DATA STRUCTURES AND
ANALYSIS

Ad

 Array
 List
 Tuple
 Vector
 Data Frame
 Stack
 Queue
 Linked List
DATA STRUCTURES

Ad

STACK AS A DATA STRUCTURE

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Upcoming SlideShare
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
Loading in …3
×

Check these out next

1 of 93 Ad
1 of 93 Ad
Advertisement

More Related Content

Advertisement

Stack, Queue, Linked List.pptx

  1. 1. Prof. Sonali Mhatre DATA STRUCTURES AND ANALYSIS
  2. 2.  Array  List  Tuple  Vector  Data Frame  Stack  Queue  Linked List DATA STRUCTURES
  3. 3. STACK AS A DATA STRUCTURE
  4. 4. STACK AS A DATA STRUCTURE
  5. 5. STACK AS A DATA STRUCTURE
  6. 6. STACK AS A DATA STRUCTURE
  7. 7. 30 20 10 STACK AS A DATA STRUCTURE Top 2 1 0
  8. 8. STACK AS A DATA STRUCTURE
  9. 9.  LIFO (Last In First Out)  Push  Pop STACK AS A DATA STRUCTURE
  10. 10. QUEUE AS DATA STRUCTURE
  11. 11.  FIFO (First In First Out)  Enqueue  Deque QUEUE AS DATA STRUCTURE
  12. 12. CIRCULAR QUEUE F=-1 R=-1 10 F=0 R=0 10 20 F=0 R=1
  13. 13. CIRCULAR QUEUE 10 20 30 F=0 R=2 10 20 30 40 50 F=0 R=4 10 20 30 40 F=0 R=3 Queue is Full
  14. 14. CIRCULAR QUEUE 20 30 40 50 F=1 R=4 60 30 40 50 F=2 R=0 60 20 30 40 50 F=1 R=0 Queue is Full
  15. 15. CIRCULAR QUEUE 60 70 30 40 50 F=2 R=1 60 70 80 40 50 F=3 R=2 60 70 40 50 F=3 R=1 Queue is Full Queue is Full
  16. 16. CIRCULAR QUEUE 60 70 80 50 F=4 R=2 60 70 80 90 50 F=4 R=3 Queue is Full 60 70 80 90 F=0 R=3 60 70 80 90 100 F=0 R=4 Queue is Full
  17. 17. int isFull(struct Queue * q) { if(((abs(q->front-q->rear))==SIZE-1) || q- >rear==q->front-1) return 1; else return 0; } QUEUE ISFULL
  18. 18. CIRCULAR QUEUE F=-1 R=-1 Queue is empty 10 F=0 R=0 F=1 R=0 Queue is empty? Queue is full? 20 R=1 F=1
  19. 19. CIRCULAR QUEUE R=-1 F=-1 10 R=0 F=0 10 20 R=1 F=0 20 R=1 F=1
  20. 20. CIRCULAR QUEUE Queue is Empty?? Queue is Full?? R=1 F=0 30 R=2 F=2 40 R=3 F=3 50 R=4 F=4
  21. 21. If(Front==Rear) then Front=Rear=-1; CIRCULAR QUEUE
  22. 22. PRIORITY QUEUE
  23. 23.  Array Implementation  Insert and Sort  Remove and Rearrange  Linked List Implementation PRIORITY QUEUE IMPLEMENTATIONS
  24. 24. DOUBLE ENDED QUEUE
  25. 25. DOUBLE ENDED QUEUE R=-1 F=-1 10 20 R = 5 F = 4
  26. 26. 30 10 20 R = 5 F = 3 Insert Element at Front End 40 30 10 20 F = 2 R = 5 50 40 30 10 20 F = 1 R = 5 DOUBLE ENDED QUEUE
  27. 27. Insert Element at Front End 60 50 40 30 10 20 F = 0 R = 5 60 50 40 30 10 20 70 F = 9 R = 5 60 50 40 30 10 20 11 22 80 70 F = 6 R = 5 Queue is Full DOUBLE ENDED QUEUE
  28. 28. DOUBLE ENDED QUEUE R=-1 F=-1 10 20 R = 5 F = 4
  29. 29. DOUBLE ENDED QUEUE 10 20 30 R = 6 F = 4 Insert Element at Rear End 10 20 30 40 R = 7 F = 4 10 20 30 40 50 60 R = 9 F = 4
  30. 30. 70 10 20 30 40 50 60 R = 0 F = 4 Insert Element at Rear End DOUBLE ENDED QUEUE 70 80 10 20 30 40 50 60 R = 1 F = 4 70 80 90 11 10 20 30 40 50 60 R = 3 F = 4 Queue is Full
  31. 31.  Insertions and Deletions from both ends  I/P restricted  Input from only one end and Output from either end  O/P restricted  Input from either end and Output from only one end DOUBLE ENDED QUEUE
  32. 32. SELF REFERENTIAL STRUCTURES struct Complex { int real; int imaginary; } In main() Complex c[3]; 3 4 -5 7 2 5 …… 65004 65005 65006 65007 65008 65009 65010 65011 65012 65013 65014 65015 …… c[0] c[1] c[2] c[0]= 3+4i c[1]= -5+7i c[2]= 2+5i
  33. 33. struct Complex { int real; int imaginary; struct Complex * next } In main() Complex *c; c->next; c->next->next; SELF REFERENTIAL STRUCTURES c 3+4i next -5+7i next 2+5i next NULL
  34. 34. 3 4 72004 …… 65004 65005 65006 65007 65008 65009 65010 65011 65012 65013 65014 65015 …… c SELF REFERENTIAL STRUCTURES -5 7 62012 …… 72004 72005 72006 72007 72008 72009 72010 72011 72012 72013 72014 72015 …… next 2 5 NULL …… 62012 62013 62014 62015 62016 62017 62018 62019 62020 62022 62023 62024 …… next
  35. 35.  Self Referential Structure LINKED LIST 10 next 20 next 30 next 40 NULL
  36. 36. 10 next 20 next 30 next 40 NULL LINKED LIST tmp tmp tmp tmp tmp = tmp ->next
  37. 37. 10 next 20 next 30 next 40 NULL LINKED LIST Start/First tmp tmp tmp tmp
  38. 38. 10 next 20 next 30 next 40 NULL LINKED LIST start s
  39. 39. 10 next 20 next 30 next 40 NULL LINKED LIST (ADD AT BEGINNING) start s 50 NULL nn next start
  40. 40. 10 next 20 next 30 next 40 NULL LINKED LIST (ADD AT END) start t 50 NULL nn s t t t next
  41. 41. struct Node n; //Static struct Node * n = (struct Node *) malloc (sizeof (struct Node)); //Dynamic MALLOC (DYNAMIC MEMORY ALLOCATION)
  42. 42. …… 65004 65005 65006 65007 65008 65009 65010 65011 65012 65013 65014 65015 …… n MALLOC (DYNAMIC MEMORY ALLOCATION) …… 65004 65005 65006 65007 65008 65009 65010 65011 65012 65013 65014 65015 …… n 65004
  43. 43. 10 next 20 next 30 next 40 next LINKED LIST(ADD AT A POSITION) start t s 100 NULL nn t t 50 NULL Position=4 next Count=1 Count=2 Count=3
  44. 44. 10 next 20 next 30 next 40 next LINKED LIST(ADD AT A POSITION) start t s 100 NULL nn t t 50 NULL Position=4 next Count=1 Count=2 Count=3 ?
  45. 45. t=s; while(c<pos-1) { t=t->next; c++; } nn->next=t->next; t->next=nn; LINKED LIST(ADD AT A POSITION)
  46. 46. 10 next 20 next 30 next 40 next LINKED LIST(ADD BEFORE) start t s 100 NULL nn t t 50 NULL Before = 40 t pt null
  47. 47. 10 next 20 next 30 next 40 next LINKED LIST(ADD BEFORE) start t s 100 NULL nn t t 50 NULL Before = 40 next t pt pt pt
  48. 48. 10 next 20 next 30 next 40 next LINKED LIST (ADD BEFORE) start t s 100 NULL nn t t 50 NULL Before = 40 next t pt pt pt
  49. 49. t=s; pt=NULL; while(t!=NULL) { if(t->a==b) break; pt=t; t=t->next; } if(pt==NULL) { nn->next=t; s=nn; } else { pt->next=nn; nn->next=t; } LINKED LIST (ADD BEFORE)
  50. 50. 10 next 20 next 30 next 40 next LINKED LIST (ADD AFTER) start t s 100 NULL nn t t 50 NULL After = 30 next
  51. 51. t=s; while(t!=NULL) { if(t->a==b) break; t=t->next; } nn->next=t->next; t->next=nn; LINKED LIST(ADD AFTER)
  52. 52. 10 next 20 next 30 next 40 NULL LINKED LIST (DELETE BEGINNING) start s s start s=s->next;
  53. 53. 10 next 20 next 30 next 40 next LINKED LIST (DELETE END) start t s t t 50 NULL t pt pt pt pt t pt->next=NULL; NULL
  54. 54. t=s; pt=NULL; while(t->next!=NULL) { pt=t; t=t->next; } if(pt==NULL) s=NULL; else pt->next=NULL; LINKED LIST (DELETE END)
  55. 55. 10 next 20 next 30 next 40 next LINKED LIST (DELETE IN BETWEEN NODE) start t s t t 50 NULL pt pt pt->next=t->next; Delete = 30
  56. 56. 10 next 20 next 30 NULL LINKED LIST (REVERSING A LIST) start t s nt nnt t nt nnt NULL s NULL while(nt!=NULL) next t nt s start
  57. 57. while(nt!=NULL) { nt->next=t; t=nt; s=nt; nt=nnt; nnt=nnt->next; } LINKED LIST (REVERSING A LIST)
  58. 58. 10 next 20 next 30 next 40 NULL CIRCULAR LINKED LIST start s next
  59. 59. 10 next 20 next 30 next 40 NULL CIRCULAR LINKED LIST (ADD AT BEGINNING) start s next 100 NULL nn next l start s
  60. 60. if(s==NULL) { s=nn; s->next=s; } else { struct Node * l=getLast(s); nn->next=s; l->next=nn; s=nn; CIRCULAR LINKED LIST (ADD AT BEGINNING)
  61. 61. 10 next 20 next 30 next 40 NULL CIRCULAR LINKED LIST (ADD AT END) start s next 100 NULL nn next l
  62. 62. if(s==NULL) { s=nn; s->next=s; } else { struct Node * l=getLast(s); nn->next=s; l->next=nn; } CIRCULAR LINKED LIST (ADD AT END)
  63. 63. 10 next 20 next 30 next 40 CIRCULAR LINKED LIST (DELETE BEGINNING) start s next l start s
  64. 64. struct Node * l=getLast(s); if(s==l) s=NULL; else { l->next=s->next; s=s->next; } CIRCULAR LINKED LIST (DELETE BEGINNING)
  65. 65. 10 next 20 next 30 next 40 CIRCULAR LINKED LIST (DELETE END) start s next l t t t
  66. 66. struct Node * l=getLast(s); if(s==l) s=NULL; else { struct Node * t=s; while(t->next!=l) t=t->next; t->next=s; } CIRCULAR LINKED LIST (DELETE END)
  67. 67.  Double pointer  Pointer to previous Node  Pointer to Next Node struct Node { int a; struct Node * prev; struct Node * next; } DOUBLY LINKED LIST a prev next
  68. 68. DOUBLY LINKED LIST NULL 100 next prev 200 next prev 300 next prev 400 next prev 500 NULL start
  69. 69. DOUBLY LINKED LIST (ADD AT BEGINNING) NULL 100 next prev 200 next prev 300 next prev 400 next prev 500 NULL start NULL 1000 NULL nn next prev s start s
  70. 70. if(s==NULL) s=nn; else { nn->next=s; s->prev=nn; s=nn; } DOUBLY LINKED LIST (ADD AT BEGINNING)
  71. 71. DOUBLY LINKED LIST (ADD AT END) NULL 100 next prev 200 next prev 300 next prev 400 next prev 500 NULL start NULL 1000 NULL nn s t t t t t next prev
  72. 72. if(s==NULL) s=nn; else { struct Node * t; t=s; while(t->next!=NULL) t=t->next; t->next=nn; nn->prev=t; } DOUBLY LINKED LIST (ADD AT END)
  73. 73. NULL 100 next prev 200 next prev 300 next prev 400 NULL start NULL 1000 NULL s t t Position = 3 Count = 1 Count = 2 next prev DOUBLY LINKED LIST (ADD AT A POSITION)
  74. 74. t=s; while(c<pos-1) { t=t->next; c++; } t->next->prev=nn; nn->next=t->next; nn->prev=t; t->next=nn; DOUBLY LINKED LIST (ADD AT A POSITION)
  75. 75. NULL 100 next prev 200 next prev 300 next prev 400 NULL start NULL 1000 NULL s t t Before = 300 next prev DOUBLY LINKED LIST (ADD BEFORE) t pt=NULL pt pt
  76. 76. if(pt==NULL) { nn->next=t; t->prev=nn; s=nn; } DOUBLY LINKED LIST (ADD BEFORE) else { pt->next=nn; t->prev=nn; nn->next=t; nn->prev=pt; }
  77. 77. Without pt ???????? DOUBLY LINKED LIST (ADD BEFORE)
  78. 78. NULL 100 next prev 200 next prev 300 next prev 400 NULL start NULL 1000 NULL s t t Before = 300 next prev DOUBLY LINKED LIST (ADD BEFORE) t
  79. 79. t->prev->next=nn; nn->prev=t->prev; t->prev=nn; nn->next=t; DOUBLY LINKED LIST (ADD BEFORE)
  80. 80. NULL 100 next prev 200 next prev 300 next prev 400 NULL start NULL 1000 NULL s t t After = 200 next prev DOUBLY LINKED LIST (ADD AFTER)
  81. 81. nn->next=t->next; t->next->prev=nn; t->next=nn; nn->prev=t; DOUBLY LINKED LIST (ADD AFTER)
  82. 82. NULL 100 next prev 200 next prev 300 next prev 400 next prev 500 NULL start DOUBLY LINKED LIST (DELETE BEGINNING) s NULL s start
  83. 83. s->next->prev=NULL; s=s->next; DOUBLY LINKED LIST (DELETE BEGINNING)
  84. 84. NULL 100 next prev 200 next prev 300 next prev 400 NULL start s DOUBLY LINKED LIST (DELETE END) t pt=NULL t pt t pt t pt NULL
  85. 85. t=s; pt=NULL; while(t->next!=NULL) { pt=t; t=t->next; } if(pt==NULL) s=NULL; else pt->next=NULL; DOUBLY LINKED LIST (DELETE END)
  86. 86. Without pt ???????? DOUBLY LINKED LIST (DELETE END)
  87. 87. NULL 100 next prev 200 next prev 300 next prev 400 NULL start s DOUBLY LINKED LIST (DELETE AT A POSITION) t pt=NULL t pt t pt Position = 3 Count = 1 Count = 2
  88. 88. t=s; pt=NULL; while(c<pos) { pt=t; t=t->next; c++; } if(pt==NULL) s=NULL; else { pt->next=t->next; t->next->prev=pt; } DOUBLY LINKED LIST (DELETE AT A POSITION)
  89. 89. NULL 100 next prev 200 next prev 300 next prev 400 NULL start s DOUBLY LINKED LIST (DELETE AFTER) t t After = 200
  90. 90. t->next=t->next->next; t->next->prev=t; DOUBLY LINKED LIST (DELETE AFTER)
  91. 91. NULL 100 next prev 200 next prev 300 next prev 400 NULL start s DOUBLY LINKED LIST (DELETE BEFORE) t pt=NULL t pt t pt Before = 400 ppt=NULL ppt pt ppt
  92. 92. t=s; pt=NULL; ppt=NULL; while(t!=NULL) { if(t->a==b) break; ppt=pt; pt=t; t=t->next; } ppt->next=pt->next; t->prev=pt->prev; DOUBLY LINKED LIST (DELETE BEFORE)
  93. 93. Array Linked List Data elements are stored in contiguous locations in memory. New elements can be stored anywhere and a reference is created for the new element using pointers. Array elements can be accessed randomly using the array index. Random accessing is not possible in linked lists. The elements will have to be accessed sequentially. Insertion and Deletion operations are costlier since the memory locations are consecutive and fixed. Insertion and Deletion operations are fast and easy in a linked list. Memory is allocated during the compile time (Static memory allocation). Memory is allocated during the run-time (Dynamic memory allocation).

×