Presentation is loading. Please wait.

Presentation is loading. Please wait.

Circular Linked List Singly Circular Linked List Doubly Circular Linked List.

Similar presentations


Presentation on theme: "Circular Linked List Singly Circular Linked List Doubly Circular Linked List."— Presentation transcript:

1 Circular Linked List Singly Circular Linked List Doubly Circular Linked List

2 Circular Linked List void make(int a) { ptr=new LIST; if (!head) head=ptr; else rear->next=ptr; ptr->dann=a; rear=ptr; rear->next=head; }

3 Doubly Linked List struct NDD { int val; NDD * next; NDD * prev; }; NDD *beg=NULL, *end=NULL;

4 List Building void make(int a) { NDD *rex; rex=new NDD; rex->val=a; if (!beg) beg=rex; else end->next=rex; rex->prev=end; end=rex; end->next=NULL; } // The new rex points to a NDD struct // Store data in the new node. // If the list is empty, the rex pointer // changes the beg pointer // Else change end.next field from NULL to point // to the new node // Set rex.prev field equal to the prev (end) node // Set end to point to the new node // set end.next field equal to a null pointer

5 List Building void make(int a) { NDD *rex; rex=new NDD; rex->val=a; if (!beg) beg=rex; else end->next=rex; rex->prev=end; end=rex; end->next=NULL; } prevnext rex 5 beg NULL prevnext rex 3 end NULL prevnext rex 9 NULL

6 Deleting a node 1.We locate the previous node using the previous field. 2.We make the next field of this node point to the node following the one in the cursor position rex: rex->prev->next=rex->next; 3.Then we make the previous field of this following node point to the node preceding the one in the cursor position rex: rex->next->prev=rex->prev; 4.The list node pointed to by the cursor rex becomes useless and should be deleted: delete rex;

7 Deleting a node NDD rex=beg; while(rex!=NULL && rex->val!=ad) rex=rex->next; if (rex!=NULL) { rex->prev->next=rex->next; rex->next->prev=rex->prev; delete rex; } prev3nextprev6nextprev-9nextprev11nextprev4next beg end NULL rex

8 Traversing the list NDD *rex=beg; while (rex!=NULL) rex=rex->next; NDD *rex=end; while (rex!=NULL) rex=rex->prev;

9 Stacks LIFO A Push (A) A B Push (B) C Push (C) A B C A B Pop A B

10 Array-based implementation array A; the variable top (-1…(capacity-1)); the capacity.

11 struct STACK { int dann; STACK *prev; }; STACK *top=NULL; Linked List-based implementation

12 Operations void push(int a) {STACK *q; q=new STACK; q->prev=top; q->dann=a; top=q; } // new q points to the stack struct // set q.prev field to the top // (add a new node to the top of the stack) // store data in the new node // set top to point to the new node Push a new element onto the stack prev q NULL 1 top prev q 2 q 3

13 Remove the top element from the stack int pop(void) {STACK *q; int temp=-1; if (!top) cout << "\nEmpty"; else {temp=top->dann; q=top->prev; delete top; top=q; } return temp; } // store the top node data // the q holds the position of the // next node in the stack // the top is deleted // set top equal to the q prev NULL 1 top prev 2 q 3 4 temp=4

14 Return the top element value int peek (void) {int temp; if (!top) { cout << "\nEmpty"; return -1; } temp=top->dann; return temp; } prev NULL 1 top prev 2 3 4 temp=4

15 Destroying the stack void clear (void) {STACK *q; while (top) { q=top->prev; delete top; top=q; } // while the top pointer is not equal to the NULL // the q pointer is used to hold the position // of the next node in the stack // the top is deleted // set top equal to the q prev NULL 1 top prev 2 3 4 q

16 Stack applications Reverse a word. Palindrome strings “A man, a plan, a canal—Panama!” “Able I was ere, I saw Elba.” “Won ton? Not now!” “Madam, I’m Adam.” “Eve.”

17 struct NDD { int val; NDD * next; NDD * prev; }; NDD *beg=NULL, *end=NULL; void make(int a) { NDD *rex; rex=new NDD; rex->val=a; if (!beg) beg=rex; else end->next=rex; rex->prev=end; end=rex; end->next=NULL; } NDD rex=beg; while(rex!=NULL && rex->val!=ad) rex=rex->next; if (rex!=NULL) { rex->prev->next=rex->next; rex->next->prev=rex->prev; delete rex; } NDD *rex=beg; while (rex!=NULL) rex=rex->next; NDD *rex=end; while (rex!=NULL) rex=rex->prev;


Download ppt "Circular Linked List Singly Circular Linked List Doubly Circular Linked List."

Similar presentations


Ads by Google