Title | DSA Experiment 12 (Aryan Dubey 06) Code and Output |
---|---|
Author | Tanmay Mahajan |
Course | Data Structures |
Institution | University of Mumbai |
Pages | 30 |
File Size | 633.7 KB |
File Type | |
Total Downloads | 113 |
Total Views | 138 |
DSA CODDE...
Name: Aryan Dubey Roll No: 06 Class: D10B
DSA Experiment No – 12
Code: //Doubly Circular Linked List. #include #include struct node { int data; struct node *llink, *rlink; }; struct node * newNode() // Function for Memory Allocation. { struct node *n; n =(struct node *)malloc(sizeof(struct node)); return n; } struct node *head = NULL, *end = NULL, *ptr; int item, nodes = 1; int c1, z = 1, t; void create()// Function for Linked List Creation. { int c; printf("\nLinked List Creation:"); do
{ struct node *temp; temp = newNode(); printf("\nEnter element no %d to be inserted in the doubly circular linked list : ",nodes); scanf("%d",&temp->data);
if(head==NULL) { head=temp; temp->rlink = NULL; temp->llink = NULL; } else { ptr=head; while(ptr->rlink!=NULL) { ptr=ptr->rlink; } ptr->rlink = temp; temp->llink = ptr; temp->rlink = NULL; end = temp; } nodes ++; printf("Enter 1 to add another node or 0 to go to MENU : "); scanf("%d",&c); }while (c!=0); end->rlink = head;
head->llink = end; printf("\nDoubly circular linked list successfully created with %d nodes.",nodes - 1); display(); } void insertAtHead() //Function for inserting element at the head node. { struct node *temp; temp = newNode(); printf("\nEnter the value to be inserted as the new head node : "); scanf("%d",&item); temp->data = item; temp->rlink = head; temp->llink = end; end->rlink = temp; head = temp; printf("\n%d successfully inserted as the head node.",head->data); nodes ++; display(); } void insertBefore()// Function for inserting before a value. { display(); struct node *temp, *preptr; temp = newNode(); int x; printf("\nEnter the element before which value is to be inserted: "); scanf("%d",&x); if (x == head->data) {
insertAtHead(); } else { ptr = head; preptr = head; while(ptr->data != x) { preptr = ptr; ptr = ptr->rlink; } printf("\nEnter the value to be inserted: "); scanf("%d",&temp->data); preptr->rlink = temp; ptr->llink = temp; temp->llink = preptr; temp->rlink = ptr; printf("\n%d successfully inserted before %d.",temp->data,x); nodes ++; display(); } } void insertAfter() //Function for Inserting element after a Value { display(); struct node *temp, *postptr; temp = newNode(); int x; printf("\nEnter the element after which the value is to be inserted: ");
scanf("%d",&x); ptr = head; while (ptr->data != x) { ptr = ptr->rlink; } if (ptr->rlink == NULL) { insertAtEnd(); } else { printf("\nEnter the value to be inserted: "); scanf("%d",&temp->data); postptr = ptr->rlink; postptr->llink = temp; ptr->rlink = temp; temp->rlink = postptr; temp->llink = ptr; printf("\n%d successfully inserted after %d.",temp->data,x); nodes ++; display(); } } void insertAtEnd() // Function for Inserting at the end of the list. { struct node *temp; temp = newNode(); printf("\nEnter the element to be inserted at the end: ");
scanf("%d",&item); temp->data = item; ptr = head; while (ptr->rlink != head) { ptr = ptr->rlink; } ptr->rlink = temp; temp->llink = ptr; temp->rlink = head; head->llink = temp; end = temp; printf("\n%d successfully inserted at the end",end->data); nodes ++; display(); } void insertByPosition()// Function for Inserting at a particular Position. { struct node *temp, *postptr; temp = newNode(); int pos, y; display(); countNodes(); printf("\nNote: If you want to insert before the head node enter position as 0 and if you want to insert after the end node enter position as %d.\nEnter a position where you want to insert an element: ",nodes - 1); scanf("%d",&pos); if(pos < 0) { printf("\nPlease enter a position between 0 and %d",nodes -1);
} else if (pos > nodes -1) { printf("\nPlease enter a position between 0 and %d",nodes -1); } else if (pos == 0) { insertAtHead(); } else if (pos == nodes -1) { insertAtEnd(); } else { printf("\nEnter the element you want to insert: "); scanf("%d",&y); temp->data = y; ptr = head; int w = 1; while (w != pos) { ptr = ptr->rlink; w ++; } postptr = ptr->rlink; postptr->llink = temp; ptr->rlink = temp; temp->rlink = postptr;
temp->llink = ptr; printf("\n%d successfully inserted at position %d.",temp->data,pos); nodes ++; display(); } } void deleteAtHead() // Function for deleting element at the head node. { ptr = head; head = head->rlink; end->rlink = head; head->llink = end; printf("\nThe element at head (%d) was successfully deleted.\nNow %d is the new head element.",ptr->data,head->data); free(ptr); nodes --; display(); } void deleteBefore()// Function for deleting before a particular value. { struct node *preptr, *postptr; int x; display(); printf("\nEnter the element whose preceding value you want to delete: "); scanf("%d",&x); if (x == head->data) { printf("\nAs the list is circular the node before the head node is the end node.\nAs a result the end node will be deleted."); deleteAtEnd();
} else if (x == head->rlink->data) { deleteAtHead(); } else { ptr = head; while (ptr->data != x) { ptr = ptr->rlink; } ptr = ptr->llink; preptr = ptr->llink; postptr = ptr->rlink; printf("\nThe element before %d (%d) was successfully deleted.",x,ptr->data); free(ptr); preptr->rlink = postptr; postptr->llink = preptr; nodes --; display(); } } void deleteAfter()// Function for deleting after a particular value. { struct node *preptr, *postptr; int x; display(); printf("\nEnter the element whose succeeding value you want to delete: ");
scanf("%d",&x); if (x == end->data) { printf("\nAs the list is circular the node after the end node is the head node.\nAs a result the head node will be deleted."); deleteAtHead(); } else if (x == end->llink->data) { deleteAtEnd(); } else { ptr = head; while (ptr->data != x) { ptr = ptr->rlink; } ptr = ptr->rlink; preptr = ptr->llink; postptr = ptr->rlink; printf("\nThe element after %d (%d) was successfully deleted.",x,ptr->data); free(ptr); preptr->rlink = postptr; postptr->llink = preptr; nodes --; display(); } } void deleteAtEnd()// Function for deleting at the end of the linked list.
{ ptr = end; end = end->llink; head->llink = end; end->rlink = head; printf("\nThe element at the end (%d) was successfully removed.\nNow %d is the new end node.",ptr->data,end->data); free(ptr); nodes --; display(); } void deleteByPosition()// Function for deleting at a particular Position. { int pos; display(); countNodes(); printf("\nEnter the position of that element which you want to delete: "); scanf("%d",&pos); if(pos < 1) { printf("\nPlease enter a position between 1 and %d",nodes -1); } else if (pos > nodes -1) { printf("\nPlease enter a position between 1 and %d",nodes -1); } else if (pos == 1) { deleteAtHead(); }
else if (pos == nodes -1) { deleteAtEnd(); } else { struct node *preptr, *postptr; ptr = head; int w = 1; while (w != pos) { ptr = ptr->rlink; w ++; } preptr = ptr->llink; postptr = ptr->rlink; printf("\nThe element at position %d (%d) was successfully deleted.",pos,ptr->data); free (ptr); preptr->rlink = postptr; postptr->llink = preptr; nodes --; display(); } } void deleteByValue()// Function for deleting a particular Value. { int x; display(); printf("\nEnter the value that you want to delete: ");
scanf("%d",&x); if (x == head->data) { deleteAtHead(); } else { ptr = head; while (ptr->data != x) { ptr = ptr->rlink; } if (ptr->rlink == NULL) { deleteAtEnd(); return; } else { struct node *preptr, *postptr; preptr = ptr->llink; postptr = ptr->rlink; printf("\n%d was successfully deleted.",ptr->data); free (ptr); preptr->rlink = postptr; postptr->llink = preptr; nodes --; display(); }
} } void updateAtHead()// Function for updating the head node. { int x; printf("\nEnter the new value for the head node: "); scanf("%d",&x); head->data = x; printf("\nHead node successfully updated."); display(); } void updateAtEnd()// Function for updating the end node. { int x; printf("\nEnter the new value for the end node: "); scanf("%d",&x); end->data = x; printf("\nEnd node successfully updated."); display(); } void updateAtPosition()// Function for updating a value at a particular position. { int pos; display(); countNodes(); printf("\nEnter the element's position which you want to update: "); scanf("%d",&pos); if(pos < 1) {
printf("\nPlease enter a position between 1 and %d",nodes -1); } else if (pos > nodes -1) { printf("\nPlease enter a position between 1 and %d",nodes -1); } else if (pos == 1) { updateAtHead(); } else if (pos == nodes -1) { updateAtEnd(); } else { int x; printf("\nEnter the new value for position %d: ",pos); scanf("%d",&x); ptr = head; int w = 1; while (w != pos) { ptr = ptr->rlink; w ++; } ptr->data = x; printf("\nNode at position %d successfully updated.",pos); display();
} } void updateByValue()// Function for updating a particular value. { int x, y; display(); printf("\nEnter the value which you want to update: "); scanf("%d",&x); if (x == head->data) { updateAtHead(); } else { ptr = head; while(ptr->data != x) { ptr = ptr->rlink; } if (ptr->rlink == head) { updateAtEnd(); } else { printf("\nEnter the new value for the node: "); scanf("%d",&y); ptr->data = y; printf("\nNode successfully updated.");
display(); } } } void search() // Function for searching for a particular value in the list. { int x, pos = 0; printf("\nEnter the value you want to search for in the list: "); scanf("%d",&x); ptr = head; do { if (ptr->data == x) { pos ++; printf("\nValue (%d) found at position %d.",x,pos); return; } else { pos ++; printf("\nValue (%d) not found at position %d.",x,pos); } ptr = ptr->rlink; }while(ptr->rlink != head); if (ptr->data == x ) { pos ++; printf("\nValue (%d) found at position %d.",x,pos);
return; } else { pos ++; printf("\nValue (%d) not found at position %d.",x,pos); } } void countNodes()//Function for displaying the number of nodes in the list. { printf("\nThe total no of nodes in the list are : %d",nodes -1); } void display()//Function for displaying the elements in the list. { if(head == NULL) { printf("\nList is Empty!"); } else { ptr=head; printf("\nElements in the List are : "); printf("\nEnd node"); while(ptr->rlink != head) { printf("",ptr->data); ptr=ptr->rlink; } printf("Head node\n",end->data);
} } void main() { int ch; create(); do { printf("\n***MENU***\n1.Insertion\n2.Deletion\n3.Update\n4.Search\n5.Count Nodes\n6.Display\nEnter 0 to Exit\nEnter your choice : "); scanf("%d",&ch); switch(ch) { case 1: { printf("\nInsertion\n1.Insert at head\n2.Insert before\n3.Insert after\n4.Insert at end\n5.Insert at position\nEnter your choice : "); scanf("%d",&ch); switch(ch) { case 1: { printf("Insert at head"); insertAtHead(); break; } case 2: { printf("Insert before"); insertBefore();
break; } case 3: { printf("Insert after"); insertAfter(); break; } case 4: { printf("Insert at end"); insertAtEnd(); break; } case 5: { printf("Insert at position"); insertByPosition(); break; } default: { printf("Invalid Input!"); break; } } break; } case 2:
{ printf("\nDeletion\n1.Delete at head\n2.Delete before\n3.Delete after\n4.Delete at end\n5.Delete at Position\n6.Delete by value\nEnter your choice : "); scanf("%d",&ch); switch(ch) { case 1: { printf("Delete at head"); deleteAtHead(); break; } case 2: { printf("Delete before"); deleteBefore(); break; } case 3: { printf("Delete after"); deleteAfter(); break; } case 4: { printf("Delete at end"); deleteAtEnd(); break; }
case 5: { printf("Delete at position"); deleteByPosition(); break; } case 6: { printf("Delete by value"); deleteByValue(); break; } default: { printf("Invalid Input!"); break; } } break; } case 3: { printf("\nUpdate\n1.Update the head node\n2.Update the end node\n3.Update at position\n4.Update by value\nEnter your choice : "); scanf("%d",&ch); switch(ch) { case 1: { printf("Update the head node");
updateAtHead(); break; } case 2: { printf("Update the end node"); updateAtEnd(); break; } case 3: { printf("Update at position"); updateAtPosition(); break; } case 4: { printf("Update by value"); updateByValue(); break; } default: { printf("Invalid Input!"); break; } } break; }
case 4: { printf("\nSearch"); search(); break; } case 5: { printf("\nCount Nodes"); countNodes(); break; } case 6: { printf("\nDisplay"); display(); break; } case 0: { printf("Exiting the MENU..."); break; } default: { printf("Invalid Input!"); break; } }
}while(ch!=0); }
Output:...