Link list

803 views

Published on

Singly Linked list implementation in Stacks,Linear Queue and Circular Queue

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
803
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
76
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Link list

  1. 1. LINKED LISTPRESENTED BY: Javeria (11-arid-3303) MIT-3 University Institute of Information Technology, Rawalpindi (UIIT, UAAR)
  2. 2. INTRODUTION Link List is an ordered collection of elements called nodes. The nodes are connected by pointer. Each node has two parts 1) Data Field – Stores data of the node. Same data type 2) Link Field – Store address of the next node ( i.e. Link to the next node) NEXT NODE : DATA
  3. 3. ARRAY VS. LINK LIST Aspect Array Link ListSize  Fixed size.  Grow and contract according to insertions and deletions.Storage  Static: It’s location is  Dynamic: It’s node iscapacity allocated during compile located during run time. time.Accessing the  Direct or random access  Sequential accesselement method. method.  Specify the array index or  Traverse starting from the subscript. first node in the list by pointer. 4
  4. 4. SINGLY LINK LISTIMPLEMENTATION INSTACKS: A B C topIMPLEMENTATION IN LINEARQUEUE: front A B C rearIMPLEMENTATION IN CIRCULAR QUEUE: A B C rear DOUBLY LINK LIST A B C rear
  5. 5. LINKED LISTIMPLEMENTATION Stacks Linear Queue Circular Queue
  6. 6. Functions◦ We will discuss the following functions inabove three data structures:  Insertion  Deletion  Display  Search
  7. 7. LINK LIST IMPLEMENTATION OF STACKS
  8. 8. Defining the linked list datastructure: struct node { int data; node *next; }; Data next node
  9. 9. STACK CLASSclass stack{ public: node * top; stack( ) { top top=NULL; }
  10. 10. Push methodvoid push(int x){ node * ptr=new node; top ptr->data=x; ptr->next=top; top=ptr;}
  11. 11. Push methodvoid push(int x) ptr{ node * ptr=new node; top ptr->data=x; ptr->next=top; top=ptr;}
  12. 12. Push methodvoid push(int x) ptr{ x node * ptr=new node; top ptr->data=x; ptr->next=top; top=ptr;}
  13. 13. Push methodvoid push(int x) ptr{ x node * ptr=new node; top ptr->data=x; ptr->next=top; top=ptr;}
  14. 14. Push methodvoid push(int x) ptr{ x node * ptr=new node; top ptr->data=x; ptr->next=top; top=ptr;}
  15. 15. Push methodvoid push(int x){ top ptr node * ptr=new node; ptr->data=x; x ptr->next=top; top=ptr;}
  16. 16. - All nodes are connected to each other through pointers- Link of the first node is NULL pointer denoted by X- Null pointer indicates the start of the stack list top D C B A
  17. 17. Pop Methodint pop(){ top if(top==NULL) { cout<<"nlist emptyn"; D return; } C else { int temp=top->data; node* ptr=top; B top=top->next; delete ptr; return temp; A }}
  18. 18. Pop Methodint pop(){ top temp=D if(top==NULL) { cout<<"nlist emptyn"; D return ; } C else { int temp=top->data; node* ptr=top; B top=top->next; delete ptr; return temp; A }}
  19. 19. Pop Methodint pop() ptr{ top temp=D if(top==NULL) { cout<<"nlist emptyn"; D return ; } C else { int temp=top->data; node* ptr=top; B top=top->next; delete ptr; return temp; A }}
  20. 20. Pop Methodint pop(){ ptr temp=D if(top==NULL) { D cout<<"nlist emptyn"; return ; top } C else { int temp=top->data; node* ptr=top; B top=top->next; delete ptr; return temp; A }}
  21. 21. Pop Methodint pop(){ ptr temp=D if(top==NULL) { D cout<<"nlist emptyn"; return ; top } C else { int temp=top->data; node* ptr=top; B top=top->next; delete ptr; return temp; A }}
  22. 22. Display Methodvoid display(){ node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A}
  23. 23. Display Methodvoid display(){ node * temp; tem top p temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A}
  24. 24. Display Methodvoid display(){ node * temp; tem top p temp=top; display D if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A}
  25. 25. Display Methodvoid display(){ node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; tem p return; C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A}
  26. 26. Display Methodvoid display(){ node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; tem p return; C display C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A}
  27. 27. Display Methodvoid display(){ node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } tem while(temp!=NULL) p { B cout<<temp->data<<" "; temp=temp->next; } A}
  28. 28. Display Methodvoid display(){ node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } tem while(temp!=NULL) p display B { B cout<<temp->data<<" "; temp=temp->next; } A}
  29. 29. Display Methodvoid display(){ node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; tem temp=temp->next; p } A}
  30. 30. Display Methodvoid display(){ node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; tem temp=temp->next; p display A } A}
  31. 31. Display Methodvoid display(){ node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A} temp = NULL
  32. 32. Search Methodvoid search(int x){ int a=0; //index calculated from top node * temp=top; x=B while(temp!=NULL) a=0 { top if(temp->data==x) { D cout<<"found at index "<<a; break; } else C { temp=temp->next; a++; B } } if(temp==NULL) cout<<“value not found"; A}};
  33. 33. Search Methodvoid search(int x){ int a=0; //index calculated from top node * temp=top; x=B while(temp!=NULL) tem a=0 { top p if(temp->data==x) { D cout<<"found at index "<<a; break; } else C { temp=temp->next; a++; B } } if(temp==NULL) cout<<“value not found"; A}};
  34. 34. Search Methodvoid search(int x){ int a=0; //index calculated from top node * temp=top; x=B while(temp!=NULL) a=1 { top if(temp->data==x) { D cout<<"found at index "<<a; break; tem } p else C { temp=temp->next; a++; B } } if(temp==NULL) cout<<“value not found"; A}};
  35. 35. Search Methodvoid search(int x){ int a=0; //index calculated from top node * temp=top; x=B while(temp!=NULL) a=2 { top if(temp->data==x) { D cout<<"found at index "<<a; break; } else C { tem temp=temp->next; p a++; B } } if(temp==NULL) cout<<“value not found"; A}};
  36. 36. Search Methodvoid search(int x){ int a=0; //index calculated from top node * temp=top; x=B while(temp!=NULL) a=2 { top if(temp->data==x) { D cout<<"found at index "<<a; break; } else C { tem temp=temp->next; p a++; found at B } index 2 } if(temp==NULL) cout<<“value not found"; A}};
  37. 37. Main Method case 2:void main() y=s.pop();{clrscr(); if(y!=NULL)int n, a, y, z; cout<<"nvalue popped is: "<<y;int e=0; break;stack s; case 3: do s.display(); {cout<<"npress 1 to push"; break; cout<<"npress 2 to pop"; case 4: cout<<"npress 3 to display"; cout<<“enter value to search?”; cout<<“n press 4 to search”; cin>>z; cout<<"npress 5to exitn"; s.search(z); cin>>n; break; switch(n) case 5: { e=1; case 1: break; cout<<"enter a number"; } cin>>a; } while(e==0); s.push(a); } break;
  38. 38. TIME COMPLEXITY “Time Complexity is a measure of the amount of time required to execute an algorithm.” Push ◦ Best Case: O(1) ◦ Worst Case: O(1) Pop ◦ Best Case: O(1) ◦ Worst Case: O(1) Search ◦ Best Case: O(1) ◦ Worst Case: O(n)
  39. 39. LINK LISTIMPLEMENTATION OF LINEAR QUEUE
  40. 40. Defining the linked list datastructure: struct node { int data; node *next; }; Data next node
  41. 41. Class Linear Queueclass linearQueue{ public: node *rear; node *front; linearQueue() rear { front rear=NULL; front=NULL; }
  42. 42. Insert Methodvoid insert(int x) rear front{ node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr;}
  43. 43. Insert Methodvoid insert(int x) rear front ptr{ node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr;}
  44. 44. Insert Methodvoid insert(int x) rear front ptr{ node * ptr=new x node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr;}
  45. 45. Insert Methodvoid insert(int x) rear front ptr{ node * ptr=new x node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr;}
  46. 46. Insert Methodvoid insert(int x) rear front ptr{ node * ptr=new x node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr;}
  47. 47. Insert Method rearvoid insert(int x) front{ ptr node * ptr=new x node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr;}
  48. 48. Insert Methodvoid insert(int x){ node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else rear { front ptr rear->next=ptr; } A ptr x ptr->next=NULL; rear=ptr;}
  49. 49. Insert Methodvoid insert(int x){ node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else rear { front ptr rear->next=ptr; } A x ptr->next=NULL; rear=ptr;}
  50. 50. Insert Methodvoid insert(int x){ node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else rear { front ptr rear->next=ptr; } A ptr x ptr->next=NULL; rear=ptr;}
  51. 51. Insert Methodvoid insert(int x){ node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else rear { front ptr rear->next=ptr; } A ptr x ptr->next=NULL; rear=ptr;}
  52. 52. Remove Methodint remove(){ if(rear==NULL) { rear temp= A front cout<<"nlist emptyn"; return 0; A } else { int temp=front->data; node* ptr=front; if(front->next==NULL) { rear=NULL; } else { front=ptr->next; } delete ptr; return temp; }}
  53. 53. Remove Methodint remove() temp= A{ if(rear==NULL) ptr { rear front cout<<"nlist emptyn"; return 0; A } else { int temp=front->data; node* ptr=front; if(front->next==NULL) { rear=NULL; } else { front=ptr->next; } delete ptr; return temp; }}
  54. 54. Remove Methodint remove() temp= A{ if(rear==NULL) ptr { front rear cout<<"nlist emptyn"; return 0; A } else { int temp=front->data; node* ptr=front; if(front->next==NULL) { rear=NULL; } else { front=ptr->next; } delete ptr; return temp; }}
  55. 55. Remove Methodint remove() temp= A{ if(rear==NULL) ptr { front rear cout<<"nlist emptyn"; return 0; A } else { int temp=front->data; node* ptr=front; if(front->next==NULL) { rear=NULL; } else { front=ptr->next; } delete ptr; return temp; }}
  56. 56. Remove Methodint remove(){ if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else { int temp=front->data; temp=A node* ptr=front; if(front->next==NULL) ptr { rear=NULL; } front rear else A B { front=ptr->next; } delete ptr; return temp; }}
  57. 57. Remove Methodint remove(){ if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else { int temp=front->data; temp=A node* ptr=front; if(front->next==NULL) front ptr { rear=NULL; } rear else A B { front=ptr->next; } delete ptr; return temp; }}
  58. 58. Remove Methodint remove(){ if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else { int temp=front->data; temp=A node* ptr=front; if(front->next==NULL) front ptr { rear=NULL; } rear else A B { front=ptr->next; } delete ptr; return temp; }}
  59. 59. Display Methodvoid display(){ if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; front rear } A B C}
  60. 60. Display Methodvoid display(){ if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C}
  61. 61. Display Methodvoid display(){ if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) display A { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C}
  62. 62. Display Methodvoid display(){ if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C}
  63. 63. Display Methodvoid display(){ if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) display B { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C}
  64. 64. Display Methodvoid display(){ if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C}
  65. 65. Display Methodvoid display(){ if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) display C { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C}
  66. 66. Search Methodvoid search(int x){ int a=0; node * temp=front; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=0 else { temp=temp->next; front rear a++; } A B C } if(temp==NULL) cout<<“value not found";}};
  67. 67. Search Methodvoid search(int x){ int a=0; node * temp=front; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=0 else { temp temp=temp->next; front rear a++; } A B C } if(temp==NULL) cout<<“value not found";}};
  68. 68. Search Methodvoid search(int x){ int a=0; node * temp=front; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=1 else { temp temp=temp->next; front rear a++; } A B C } if(temp==NULL) cout<<“value not found";}};
  69. 69. Search Methodvoid search(int x){ int a=0; node * temp=front; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=2 else { temp temp=temp->next; front rear a++; } A B C } if(temp==NULL) cout<<“value not found";}};
  70. 70. Search Methodvoid search(int x){ int a=0; node * temp=front; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=2 else { temp temp=temp->next; front rear a++; } A B C } found at if(temp==NULL) index 2 cout<<“value not found";}};
  71. 71. Main Method case 2:void main() y=q.pop();{clrscr(); if(y!=NULL)int n, a, y, z; cout<<"nvalue popped is: "<<y;int e=0; break; linearQueue q; case 3: do q.display(); {cout<<"npress 1 to push"; break; cout<<"npress 2 to pop"; case 4: cout<<"npress 3 to display"; cout<<“enter value to search?”; cout<<“n press 4 to search”; cin>>z; cout<<"npress 5 to exitn"; q.search(z); cin>>n; break; switch(n) case 5: { e=1; case 1: break; cout<<"enter a number"; } cin>>a; } while(e==0); q.push(a); } break;
  72. 72. TIME COMPLEXITY Insert ◦ Best Case: O(1) ◦ Worst Case: O(1) Remove ◦ Best Case: O(1) ◦ Worst Case: O(1) Search ◦ Best Case: O(1) ◦ Worst Case: O(n)
  73. 73. LINK LIST IMPLEMENTATION OF CIRCULAR QUEUE
  74. 74. Defining the linked list datastructure: struct node { int data; node *next; }; Data next node
  75. 75. Class circularQueueclass circularQueue{ public: node *rear; rear circularQueue() { rear=NULL; }
  76. 76. Insert Method rearvoid insert(int x){ node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else { ptr->next=rear->next; rear->next=ptr; } rear=ptr;}
  77. 77. Insert Method rear ptrvoid insert(int x){ node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else { ptr->next=rear->next; rear->next=ptr; } rear=ptr;}
  78. 78. Insert Method rear ptrvoid insert(int x) x{ node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else { ptr->next=rear->next; rear->next=ptr; } rear=ptr;}
  79. 79. Insert Method rear ptr Addressvoid insert(int x) x of itself{ node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else { ptr->next=rear->next; rear->next=ptr; } rear=ptr;}
  80. 80. Insert Method rear ptr Addressvoid insert(int x) x of itself{ node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else { ptr->next=rear->next; rear->next=ptr; } rear=ptr;}
  81. 81. Insert Methodvoid insert(int x){ node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else rear ptr { A Address of itself x ptr->next=rear->next; rear->next=ptr; } rear=ptr;}
  82. 82. Insert Methodvoid insert(int x){ node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else rear ptr { A Address of itself x ptr->next=rear->next; rear->next=ptr; } rear=ptr;}
  83. 83. Insert Methodvoid insert(int x){ node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else rear ptr { A x ptr->next=rear->next; rear->next=ptr; } rear=ptr;}
  84. 84. Insert Methodvoid insert(int x){ node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; rear else ptr { A x ptr->next=rear->next; rear->next=ptr; } rear=ptr;}
  85. 85. Remove Methodint remove(){ if(rear==NULL) { cout<<"nlist emptyn"; return 0; rear } else A Address of itself { int temp=rear->next->data; node* ptr=rear->next; if(rear==rear->next) { rear=NULL; } else {rear->next=rear->next->next; } delete ptr; return temp; }}
  86. 86. Remove Methodint remove(){ if(rear==NULL) temp=A { cout<<"nlist emptyn"; return 0; rear } else A Address of itself { int temp=rear->next->data; node* ptr=rear->next; if(rear==rear->next) { rear=NULL; } else {rear->next=rear->next->next; } delete ptr; return temp; }}
  87. 87. Remove Methodint remove(){ if(rear==NULL) temp=A { cout<<"nlist emptyn"; ptr return 0; rear } else A Address of itself { int temp=rear->next->data; node* ptr=rear->next; if(rear==rear->next) { rear=NULL; } else {rear->next=rear->next->next; } delete ptr; return temp; }}
  88. 88. Remove Methodint remove(){ if(rear==NULL) temp=A { cout<<"nlist emptyn"; ptr return 0; } else A Address of itself { int temp=rear->next->data; node* ptr=rear->next; rear if(rear==rear->next) { rear=NULL; } else {rear->next=rear->next->next; } delete ptr; return temp; }}
  89. 89. Remove Methodint remove(){ if(rear==NULL) temp=A { cout<<"nlist emptyn"; ptr return 0; } else A Address of itself { int temp=rear->next->data; node* ptr=rear->next; rear if(rear==rear->next) { rear=NULL; } else {rear->next=rear->next->next; } delete ptr; return temp; }}
  90. 90. Remove Methodint remove(){ if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else { int temp=rear->next->data; node* ptr=rear->next; rear if(rear==rear->next) { rear=NULL; A B C } else {rear->next=rear->next->next; } delete ptr; return temp; }}
  91. 91. Remove Methodint remove(){ if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else temp=A { int temp=rear->next->data; node* ptr=rear->next; rear if(rear==rear->next) { rear=NULL; A B C } else {rear->next=rear->next->next; } delete ptr; return temp; }}
  92. 92. Remove Methodint remove(){ if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else temp=A { int temp=rear->next->data; node* ptr=rear->next; ptr rear if(rear==rear->next) { rear=NULL; A B C } else {rear->next=rear->next->next; } delete ptr; return temp; }}
  93. 93. Remove Methodint remove(){ if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else temp=A { int temp=rear->next->data; node* ptr=rear->next; ptr rear if(rear==rear->next) { rear=NULL; A B C } else {rear->next=rear->next->next; } delete ptr; return temp; }}
  94. 94. Remove Methodint remove(){ if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else temp=A { int temp=rear->next->data; node* ptr=rear->next; ptr rear if(rear==rear->next) { rear=NULL; A B C } else {rear->next=rear->next->next; } delete ptr; return temp; }}
  95. 95. Display Methodvoid display(){ if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { cout<<temp->data<<" "; temp=temp->next; }while(temp!=rear->next); rear } A B C.
  96. 96. Display Methodvoid display(){ if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { cout<<temp->data<<" "; temp=temp->next; }while(temp!=rear->next); temp rear }. A B C
  97. 97. Display Methodvoid display(){ if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { display A cout<<temp->data<<" "; temp=temp->next; }while(temp!=rear->next); temp rear }. A B C
  98. 98. Display Methodvoid display(){ if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { cout<<temp->data<<" "; temp=temp->next; }while(temp!=rear->next); temp rear } A B C.
  99. 99. Display Methodvoid display(){ if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { display B cout<<temp->data<<" "; temp=temp->next; }while(temp!=rear->next); temp rear } A B C.
  100. 100. Display Methodvoid display(){ if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { cout<<temp->data<<" "; temp=temp->next; temp }while(temp!=rear->next); rear } A B C.
  101. 101. Display Methodvoid display(){ if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { display C cout<<temp->data<<" "; temp=temp->next; temp }while(temp!=rear->next); rear } A B C.
  102. 102. Search Methodvoid search(int x){ int a=0; node * temp= rear->next while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=0 else { temp=temp->next; a++; rear } C } A B if(temp==NULL) cout<<“value not found";}};
  103. 103. Search Methodvoid search(int x){ int a=0; node * temp=rear->next; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=0 else { temp=temp->next; temp a++; rear } C } A B if(temp==NULL) cout<<“value not found";}};
  104. 104. Search Methodvoid search(int x){ int a=0; node * temp=rear->next; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=1 else { temp=temp->next; temp a++; rear } C } A B if(temp==NULL) cout<<“value not found";}};
  105. 105. Search Methodvoid search(int x){ int a=0; node * temp=rear->next; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=2 else { temp=temp->next; temp a++; rear } C } A B if(temp==NULL) cout<<“value not found";}};
  106. 106. Search Methodvoid search(int x){ int a=0; node * temp=rear->next; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=2 else { temp=temp->next; temp a++; rear } C } A B found at if(temp==NULL) index 2 cout<<“value not found";}};
  107. 107. Main Method case 2:void main() y=q.pop();{clrscr(); if(y!=NULL)int n, a, y, z; cout<<"nvalue popped is: "<<y;int e=0; break; circularQueue q; case 3: do q.display(); {cout<<"npress 1 to push"; break; cout<<"npress 2 to pop"; case 4: cout<<"npress 3 to display"; cout<<“enter value to search?”; cout<<“n press 4 to search”; cin>>z; cout<<"npress 5 to exitn"; q.search(z); cin>>n; break; switch(n) case 5: { e=1; case 1: break; cout<<"enter a number"; } cin>>a; } while(e==0); q.push(a); } break;
  108. 108. TIME COMPLEXITY Insert ◦ Best Case: O(1) ◦ Worst Case: O(1) Remove ◦ Best Case: O(1) ◦ Worst Case: O(1) Search ◦ Best Case: O(1) ◦ Worst Case: O(n)
  109. 109. THANK YOU!

×