tampurus

4.4 Singly Circular link list

Dec 5th, 2021 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int ll_size = 0;
  4. struct Node{
  5.     int value;
  6.     struct Node *next;
  7. };
  8. typedef struct Node Node;
  9. struct Node *head = NULL;
  10. void Insertion(int pos , int ele){
  11.     Node *temp = ( Node*) ( malloc(sizeof(Node) ) );
  12.     if( ( pos>ll_size+1 || pos<1)  || temp == NULL){
  13.         printf("\nYou entered wrong position or memory is not allocated");
  14.         return;
  15.     }
  16.     temp->value = ele;
  17.     if(pos == 1){
  18.         if(head == NULL){
  19.             temp->next = temp;
  20.         }else{
  21.             Node *ptr = head;
  22.             while(ptr->next != head){
  23.                 ptr = ptr->next;
  24.             }
  25.             ptr->next = temp;
  26.             temp->next = head;
  27.         }
  28.         head = temp;
  29.     }
  30.     else{
  31.         struct Node *ptr = head;
  32.         pos-=2;
  33.         while(pos--){
  34.             ptr = ptr->next;
  35.         }
  36.         temp->next = ptr->next;
  37.         ptr->next = temp;
  38.     }
  39.     ll_size++;
  40. }
  41. void Deletion(int pos){
  42.     if(head == NULL || (pos>ll_size || pos<1) ){
  43.         printf("\nlinked list is empty already or you have entered wrong position");
  44.         return;
  45.     }
  46.     Node* dele = head;
  47.     if(pos == 1){
  48.         if(head->next == head){
  49.             head = NULL;
  50.         }else{
  51.             Node *ptr = head;
  52.             while(ptr->next != head){
  53.                 ptr = ptr->next;
  54.             }
  55.             ptr->next = head->next;
  56.             head = head->next;
  57.         }
  58.         free(dele);
  59.     }
  60.     else{
  61.         Node *ptr = head;
  62.         pos-=2;
  63.         while(pos--){
  64.             ptr = ptr->next;
  65.         }
  66.         dele = ptr->next;
  67.         ptr->next = ptr->next->next;
  68.         free(dele);
  69.     }
  70.     ll_size--;
  71. }
  72. void Print(){
  73.     Node *ptr  = head;
  74.     if(head != NULL){
  75.         do{
  76.             printf("%d ",ptr->value);
  77.             ptr = ptr->next;
  78.         }while(ptr != head);
  79.     }
  80.     printf("& there are %d nodes in link list\n",ll_size);
  81. }
  82. int main()
  83. {
  84.     int choice,ele,pos,key;
  85.     while(1){
  86.         printf("1 for insertion, 2 for deletion & any to key just print the link list : ");
  87.         scanf("%d",&choice);
  88.         if(choice == 1){
  89.             printf("Enter the position where you want to insert the element : ");
  90.             scanf("%d",&pos);
  91.             printf("Enter the element : ");
  92.             scanf("%d",&ele);
  93.             Insertion(pos,ele);
  94.         }
  95.         else if (choice == 2){
  96.             printf("\nEnter the position want to delete ");
  97.             scanf("%d",&pos);
  98.             Deletion(pos);
  99.         }
  100.         else Print();
  101.         printf("1 to repeat and any key to exit ");
  102.         scanf("%d",&key);
  103.         if(key != 1) break;
  104.     }
  105.     return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment