1 / 41

Circular Linked List

Circular Linked List. Circular Linked Lists. In linear linked lists if a list is traversed (all the elements visited) an external pointer to the list must be preserved in order to be able to reference the list again.

barong
Download Presentation

Circular Linked List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Circular Linked List

  2. Circular Linked Lists • In linear linked lists if a list is traversed (all the elements visited) an external pointer to the listmust be preserved in order to be able to reference the list again. • Circular linked lists can be used to help the traverse the same list again and again if needed. Acircular list is very similar to the linear list where in the circular list the pointer of the last nodepoints not NULL but the first node.

  3. Circular Linked Lists A Linear Linked List

  4. Circular Linked Lists

  5. Circular Linked Lists

  6. Circular Linked Lists • In a circular linked list there are two methods to know if a node is the first node or not. • Either a external pointer, list, points the first node or • A header node is placed as the first node of the circular list. • The header node can be separated from the others by either heaving a sentinel value as theinfo part or having a dedicated flag variable to specify if the node is a header node or not.

  7. CIRCULAR LIST with header node • The header node in a circular list can be specified by asentinel value or a dedicated flag: • Header Node with Sentinel: Assume that info part contains positive integers. Therefore the infopart of a header node can be -1. The following circular list is an example for a sentinel used torepresent the header node: struct node{ int info; struct node *next; }; typedef struct node *NODEPTR;

  8. CIRCULAR LIST with header node

  9. CIRCULAR LIST with header node • Header Node with Flag: In this case a extra variable called flag can be used to represent theheader node. For example flag in the header node can be 1, where the flag is 0 for the othernodes. struct node{ int flag; int info; struct node *next; }; typedef struct node *NODEPTR;

  10. CIRCULAR LIST with header node

  11. Advantages • Given any node, we can traverse the entire list. • Certain operations, such as concatenation and splitting of string, is more efficient with circular linked list.

  12. Disadvantages • Danger of an infinite loop ! (The header node is used to prevent infinite loop)

  13. C Implementation • The structure definition of the circular linked lists and the linear linked list is the same: struct NodeC { int info; struct NodeC* nextptr; }; typedef struct NodeC NodeC; NodeC *first = NULL; NodeC *rear = NULL;

  14. C Implementation … //Function to insert at beginning in a circular linked list void insertBeg(int item) { NodeC *ptr = (NodeC *)malloc(sizeof(NodeC)); ptr->info = item; if(first == NULL) { ptr->next = ptr; first = rear = ptr; } else { ptr->next = first; rear->next = ptr; first = ptr; } }

  15. C Implementation … //Function to insert at end in a circular linked list void insertEnd(int item) { NodeC *ptr = (NodeC *)malloc(sizeof(NodeC)); ptr->info = item; if(first == NULL) { ptr->next = ptr; first = rear = ptr; } else { ptr->next = first; rear->next = ptr; rear = ptr; } }

  16. C Implementation … // Function to return the address of node whose info part is given NodeC* getAdd(int key) { NodeC *tmp = first; for(; (tmp->info != key)&&tmp != NULL; tmp = tmp->next); if(tmp == NULL) { printf("The node with this info is not in the list\n"); return NULL; } else return(tmp); }

  17. C Implementation … // Function to insert a node after a given node, whose info is given void insertLoc(int item, int key) { NodeC *ptr = (NodeC*)malloc(sizeof(NodeC)); NodeC *loc = getAdd(key); ptr->info = item; ptr->next = loc->next; loc->next = ptr; }

  18. C Implementation … //Function to delete from the beginning of circular linked list int delBeg() { NodeC *temp; int item; if(first == NULL) { printf("List is empty\n"); return -1; } else { item = first->info; temp = first; first = first->next; rear->next = first; free(temp); printf("The node whose info is %d, is deleted\n", item); return item; } }

  19. C Implementation … //Function to delete from the end of circular linked list. int delEnd() { NodeC *loc; int item; if(first == NULL) { printf("List is empty\n"); return -1; } else { NodeC *temp = first; while(temp->next != first) { loc = temp; temp = temp->next; } loc->next = first; item = temp->info; rear = loc; free(temp); printf("The node whose info is %d, is deleted\n", item); return item; } }

  20. C Implementation … // Function to delete a node after a given node, whose info is given int delLoc(int key) { NodeC *temp; int item; NodeC *loc = getAdd(key); item = loc->next->info; temp = loc->next; loc->next = loc->next->next; free(temp); printf("The node whose info is %d, is deleted\n", item); return item; }

  21. C Implementation … void showList() { if(first!=NULL) { NodeC * ptr = first; while(ptr->next!=first) { printf("List element\t %d\n", ptr->info); ptr = ptr->next; } printf("List element\t %d\n", ptr->info); } else printf("List is empty\n"); }

  22. Deleting a node from the list • To delete a node in the end, traverse the list till the last node is reached using temporary pointer p. • The pointer is loc made to point the node before the node which is to be deleted. • loc->next=p->next

  23. Example • Consider a circular linked list with a header node, where each node contains the name,account number and the balance of a bank customer. The header node contains a sentinelaccount number to be -99. • (a) Write an appropriate node structure definition for the circular linked list. • (b) Write a function to display the full records of the customers with negative balance.

  24. a) struct node{ char Name[15];int AccNo; float Balance; struct node *next; }; typedef struct node *NODEPTR; b) Assume that the list pointer points the header with the sentinelaccount number -99. void DispNegBalanca(NODEPTR *plist) { NODEPTR p; p=*plist; if(p == NULL){ printf(“There is no list!\n”); exit(1); } p=p->next; while(p->AccNo!=-99){ if(p->Balance < 0.0) printf(“The Customer Name:%s\nThe Account No:%d\nThe Balance:%.2f\n”, p->Name, p->AccNo, p->Balance); p=p->next; } }

  25. Example • Write a function that returns the average of the numbers in a circular list. Assume that thefollowing node structure is used, where the flag variable is 1 for the header node and 0 for all theother nodes. struct node{ int flag; float info; struct node *next; }; typedef struct node *NODEPTR;

  26. float avList(NODEPTR *plist)/*assume that plist points the header node*/ { int count=0; float sum =0.0; NODEPTR p; p=*plist; if((p == NULL)){ printf(“Empty list\n”); exit(1); } do{ sum=sum + p->info; p =p->next; count++; }while(p->flag !=1); return sum/count; }

  27. Applications • Can be used as a timesharing problem solved by the operating system. • In a timesharing environment, the operating system must maintain a list of present users and must alternately allow each user to use a small slice of CPU time, one user at a time. • The operating system will pick a user, let him/her use a small amount of CPU time and then move on to the next user, etc. • For this application, there should be no NULL pointers unless there is absolutely no one requesting CPU time.

  28. Doubly Linked list // Node definition for doubly linked list struct NodeD { int info; struct NodeD* right; struct NodeD* left; }; typedef struct NodeD NodeD;

  29. Doubly Linked list … Insertion • We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C

  30. Doubly Linked list … //Function to insert a NodeD at the beginning of the linked list NodeD* insertBeg(int item, NodeD* head) { NodeD *ptr = (NodeD*)malloc(sizeof(NodeD)); ptr->info = item; ptr->left = head; ptr->right = head->right; head->right->left = ptr; head->right = ptr; return head; }

  31. Doubly Linked list … //Function to insert a NodeD at the end of the linked list NodeD* insertEnd(int item, NodeD* head) { NodeD *ptr = (NodeD *)malloc(sizeof(NodeD)); ptr->info = item; ptr->left = head->left; ptr->right = head; head->left->right = ptr; head->left = ptr; return head; }

  32. Doubly Linked list … // Function to return the address of NodeD whose info part is given NodeD* getAdd(int key, NodeD *head) { NodeD *tmp = head->right; if(tmp != head) { for(; (tmp->info != key) && (tmp!=head); tmp = tmp->right); return tmp; } else { printf("The NodeD with this info is not in the list\n"); return NULL; } }

  33. Doubly Linked list … // Function to insert a NodeD after a given NodeD, whose info is given NodeD* insertLoc(int item, int key, NodeD *head) { NodeD *ptr = (NodeD*)malloc(sizeof(NodeD)); NodeD *loc = getAdd(key, head); ptr->info = item; // Now four pointer modifications ptr->left = loc; ptr->right = loc->right; loc->right->left = ptr; loc->right = ptr; return head; }

  34. p A B C D Deletion • We visualize remove(p), where p == last() A B C p D A B C

  35. Doubly Linked list … //Function to delete a NodeD from the beginning of the linked list NodeD* delBeg(NodeD *head) { NodeD *temp; int item; if(head->right == head) { printf("List is empty\n"); return NULL; } else { temp = head->right; head->right = temp->right; temp->right->left = head; item = temp->info; free(temp); printf("The NodeD whose info is %d, is deleted\n", item); return head; } }

  36. Doubly Linked list … //Function to delete a NodeD from the end of the linked list NodeD* delEnd(NodeD *head) { NodeD *temp; int item; NodeD *loc; if(head->right == head) { printf("List is empty\n"); return NULL; } else { temp = head->left; loc = temp->left; head->left = loc; loc->right = head; item = temp->info; free(temp); printf("The NodeD whose info is %d, is deleted\n", item); return head; } }

  37. Doubly Linked list … //Function to display the list void showList(NodeD *head) { if(head->right != head) { NodeD* ptr = head; do { ptr = ptr->right; printf("List element\t%d\n", ptr->info); } while(ptr->right != head); } else printf("List is empty\n"); }

  38. Reversing a simple linked list //Function to reverse the simple linked list Node * revList(Node * list) { if(list!=NULL) { Node *ptr = list; Node *prev = NULL; Node *temp; while(ptr!=NULL) { temp = ptr; ptr = ptr->next; temp->next = prev; prev = temp; } list = prev; } else printf("List is empty\n"); return list; }

  39. Reversing a doubly linked list //Function to reverse the doubly linked list NodeD * revListNew(NodeD * head) { if(head->right != head) { NodeD* ptr = head->right; NodeD* prev = head; NodeD* temp; head->left = head->right; while(ptr != head) { temp = ptr; ptr = ptr->right; temp->right = prev; temp->left = ptr; prev = temp; } head->right = prev; } else printf("List is empty\n"); return head; }

  40. Sorting of simple linked list //Function to sort the simple linked list Node* sortList(Node *list) { if(list != NULL) { Node *tmp = list->next; Node *loc; Node *res = (Node*)malloc(sizeof(Node)); res->info = list->info; res->next = NULL; for(;tmp != NULL; tmp = tmp->next) { if(tmp->info < res->info) res = insertBeg(tmp->info, res); else { loc = res; while((tmp->info > loc->next->info)&&(loc->next != NULL)) loc = loc->next; res = insertLoc(tmp->info, loc->info, res); } } return res; } else { printf("List is empty\n"); return list;} }

  41. Sorting of doubly linked list //Function to sort the doubly linked list NodeD* sortList(NodeD *head) { if(head->right != head) { NodeD *tmp = head->right->right; NodeD *loc; NodeD *res = (NodeD*)malloc(sizeof(NodeD)); res->right = res->left = res; res = insertBeg(head->right->info, res); for(;tmp != head; tmp = tmp->right) { if(tmp->info < res->info) res = insertBeg(tmp->info, res); else { loc = res->right; while((tmp->info > loc->right->info)&&(loc->right != res)) loc = loc->right; res = insertLoc(tmp->info, loc->info, res); } } return res; } else { printf("List is empty\n"); return head;} }

More Related