Advertisement
Arnab_Manna

CircularSIngleLLALL

Oct 18th, 2022
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.66 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node_tag
  5. {
  6.     int Data;
  7.     struct node_tag *link;
  8. }node;
  9.  
  10. node *GetNode()
  11. {
  12.     node *New;
  13.     New=(node*)malloc(sizeof(node));
  14.     New->link=NULL;
  15.     return New;    
  16. }
  17.  
  18. void Display(node *Last)
  19. {
  20.     node *x;
  21.     x=Last->link;
  22.     if(x==NULL){
  23.         printf("empty");
  24.     }
  25.     while(x!=Last)
  26.     {
  27.         printf("%d,",x->Data);
  28.         x=x->link;
  29.     }
  30.     printf("%d,",Last->Data);
  31. }
  32.  
  33. node *InsertAtBegin(node *Last,int x)
  34. {
  35.     node *New;
  36.     New=GetNode();
  37.     New->Data=x;
  38.     if(Last==NULL)
  39.     {
  40.         Last=New;
  41.         Last->link=Last;
  42.     }
  43.         New->link=Last->link;
  44.         Last->link=New;
  45.     return Last;
  46. }
  47.  
  48. node *InsertAtEnd(node *Last,int x)
  49. {
  50.     node *New;
  51.     New=GetNode();
  52.     New->Data=x;
  53.     if(Last==NULL)
  54.     {
  55.         Last=New;
  56.         Last->link=Last;
  57.     }
  58.         New->link=Last->link;
  59.         Last->link=New;
  60.         Last=New;
  61.     return Last;
  62. }
  63.  
  64. int Search(node *last,int y)
  65. {
  66.     node *x,*H;
  67.     x=last->link;
  68.     if(x->Data==y)
  69.     {
  70.         return 1;
  71.     }
  72.     else
  73.     {
  74.         H=x;
  75.         x=x->link;
  76.         while(x!=H)
  77.         {
  78.             if(x->Data==y)
  79.             {
  80.                 return 1;
  81.                 break;
  82.             }
  83.             x=x->link;
  84.         }
  85.     }
  86. }
  87.  
  88. node *DelFront(node *last)
  89. {
  90.     node *x,*y;
  91.     if(last->link==last)
  92.     {
  93.         last=NULL;
  94.     }
  95.     else{
  96.         x=last->link;
  97.         y=x->link;
  98.         last->link=y;
  99.         x->link=NULL;
  100.         free(x);
  101.     }
  102.     printf("deleted");
  103.     return last;
  104. }
  105.  
  106. node *DelRear(node *last)
  107. {
  108.     node *y,*x;
  109.     if(last->link==last)
  110.     {
  111.         x=last;
  112.         last=NULL;
  113.         free(x);
  114.     }
  115.     else{
  116.         y=last;
  117.         while(1)
  118.         {
  119.             x=y;
  120.             y=y->link;
  121.             if(y==last)
  122.             {
  123.                 break;
  124.             }
  125.         }
  126.         last=x;
  127.         last=DelFront(last);
  128.     }
  129.     return last;
  130. }
  131. node *InsertAtAnyPos(node *last,int a,int p1)
  132. {
  133.     node *y,*Head;
  134.     int i;
  135.     y=last;
  136.     Head=last;
  137.     for(i=0;i<p1;i++)
  138.     {
  139.         Head=Head->link;
  140.     }
  141. //  printf("%d\n",Head->Data);
  142.     last=InsertAtBegin(Head,a);
  143.     last=y;
  144.     return last;
  145. }
  146. node *DelAtAnyPos(node *last,int p1)
  147. {
  148.     node *y,*head;int cnt=0;
  149.     if(last==NULL)
  150.     {
  151.          printf("Deletion not possible\n");
  152.          return last;
  153.     }
  154.     else
  155.     {
  156.         head=last;
  157.         while(1)
  158.         {
  159.             y=head;
  160.             head=head->link;
  161.             cnt++;
  162.             if(cnt==p1)
  163.             {
  164.                 break;
  165.             }
  166.         }
  167.         if(head==last)
  168.         {
  169.             last=DelRear(last);
  170.         }
  171.         else if(p1==1)
  172.         {
  173.             last=DelFront(last);
  174.         }
  175.         else
  176.         {
  177.             y->link=head->link;
  178.             free(head);
  179.         }
  180.         return last;
  181.     }
  182. }
  183.  
  184. node *InsertBeforeData(node *last,int data,int value)
  185. {
  186.     node *y,*head;
  187.     y=last;int c=0;
  188.     head=last;
  189.     while(head->Data!=value)
  190.     {
  191.         head=head->link;
  192.         c++;
  193.     }
  194.     last=InsertAtAnyPos(head,data,c);
  195.     last=y;
  196.     return last;
  197. }
  198.  
  199. node *InsertAfterData(node *last,int data,int value)
  200. {
  201.     node *y,*head;
  202.     y=last;int c=1;
  203.     head=last;
  204.     while(head->Data!=value)
  205.     {
  206.         head=head->link;
  207.         c++;
  208.     }
  209.     last=InsertAtAnyPos(head,data,c);
  210.     last=y;
  211.     return last;
  212. }
  213.  
  214. node *DelBeforeData(node *last,int x)
  215. {
  216.     node *y,*head;
  217.     y=last;int c=1;
  218.     head=last->link;
  219.         while(head->Data!=x)
  220.         {
  221.             head=head->link;
  222.             c++;
  223.         }
  224.         printf("%d\n",c);
  225.         if(c==1)
  226.         {
  227.             last=DelRear(last);
  228.         }
  229.         else
  230.         {
  231.         last=DelAtAnyPos(last,(c-1));
  232.         }
  233.     return last;
  234. }
  235.  
  236.  
  237. node *DelAfterData(node *last,int x)
  238. {
  239.     node *y,*head;
  240.     y=last;int c=1;
  241.     head=last->link;
  242.         while(head->Data!=x)
  243.         {
  244.             head=head->link;
  245.             c++;
  246.         }
  247.         printf("%d\n",c);
  248.         if(head==last)
  249.         {
  250.             last=DelFront(last);
  251.         }
  252.         else
  253.         {
  254.         last=DelAtAnyPos(last,(c+1));
  255.         }
  256.     return last;
  257. }
  258.  
  259. node *CreateList()
  260. {
  261.     int ch,x,s,p1;
  262.     node *Last=NULL;
  263.     while(1)
  264.     {
  265.         ax:
  266.         printf("______________________________\n");
  267.         printf("1.display\n2.Search\n3.Insert Begin\n4.Insert End\n5.Delete Front\n6.Delete Rear\n7.Insert at any Position\n8.Delete from any position\n9.Insert before a given data\n10.Insert after a given data\n11. Delete before a given data\n12. Delete after a given data\n13.Exit :");
  268.         scanf("%d",&ch);
  269.             switch(ch)
  270.             {
  271.                 case 1:
  272.                     printf("______________________________\n");
  273.                     if(Last==NULL)
  274.                     {
  275.                         printf("empty\n");
  276.                         goto ax;
  277.                     }
  278.                     else
  279.                     {
  280.                     Display(Last);
  281.                     printf("\n");
  282.                     }
  283.                 break;
  284.                 case 2:
  285.                     if(Last==NULL)
  286.                     {
  287.                         printf("list is Empty\n");
  288.                         goto ax;
  289.                     }
  290.                     else{
  291.                     printf("enter the data to be searched :");
  292.                     scanf("%d",&x);
  293.                     s=Search(Last,x);
  294.                     if(s==1)
  295.                         printf("found\n");
  296.                     else
  297.                         printf("not Found\n");
  298.                     }
  299.                    
  300.                 break;
  301.                 case 3:
  302.                     printf("enter the data:");
  303.                     scanf("%d",&x);
  304.                     Last=InsertAtBegin(Last,x);
  305.                 break;
  306.                 case 4:
  307.                     printf("enter the data:");
  308.                     scanf("%d",&x);
  309.                     Last=InsertAtEnd(Last,x);
  310.                 break;
  311.                 case 5:
  312.                     if(Last==NULL)
  313.                     {
  314.                         printf("list is Empty\n");
  315.                         goto ax;
  316.                     }
  317.                     else
  318.                         Last=DelFront(Last);
  319.                 break;
  320.                 case 6:
  321.                     if(Last==NULL)
  322.                     {
  323.                         printf("list is Empty\n");
  324.                         goto ax;
  325.                     }
  326.                     else
  327.                         Last=DelRear(Last);
  328.                 break;
  329.                 case 7:
  330.                     if(Last==NULL)
  331.                     {
  332.                         printf("list is Empty\n");
  333.                         goto ax;
  334.                     }
  335.                     printf("enter the data:");
  336.                     scanf("%d",&x);
  337.                     printf("enter the position:");
  338.                     scanf("%d",&p1);
  339.                     Last=InsertAtAnyPos(Last,x,p1);
  340.                 break;
  341.                 case 8:
  342.                     if(Last==NULL)
  343.                     {
  344.                         printf("list is Empty\n");
  345.                         goto ax;
  346.                     }
  347.                     printf("enter the position:");
  348.                     scanf("%d",&p1);
  349.                     Last=DelAtAnyPos(Last,p1);
  350.                 break;
  351.                 case 9:
  352.                     if(Last==NULL)
  353.                     {
  354.                         printf("list is Empty\n");
  355.                         goto ax;
  356.                     }
  357.                     printf("enter the data:");
  358.                     scanf("%d",&x);
  359.                     printf("enter the data where you want to insert before:");
  360.                     scanf("%d",&p1);
  361.                     Last=InsertBeforeData(Last,x,p1);
  362.                 break;
  363.                 case 10:
  364.                     if(Last==NULL)
  365.                     {
  366.                         printf("list is Empty\n");
  367.                         goto ax;
  368.                     }
  369.                     printf("enter the data:");
  370.                     scanf("%d",&x);
  371.                     printf("enter the data where you want to insert before:");
  372.                     scanf("%d",&p1);
  373.                     Last=InsertAfterData(Last,x,p1);
  374.                 break;
  375.                 case 11:
  376.                     if(Last==NULL)
  377.                     {
  378.                         printf("list is Empty\n");
  379.                         goto ax;
  380.                     }
  381.                     else if(Last->link==Last)
  382.                     {
  383.                         printf("only one node is present enter some nodes to perform this function\n");
  384.                         goto ax;
  385.                     }
  386.                     printf("enter the data where you want to delete before:");
  387.                     scanf("%d",&x);
  388.                     Last=DelBeforeData(Last,x);
  389.                 break;
  390.                 case 12:
  391.                     if(Last==NULL)
  392.                     {
  393.                         printf("list is Empty\n");
  394.                         goto ax;
  395.                     }
  396.                     else if(Last->link==Last)
  397.                     {
  398.                         printf("only one node is present enter some nodes to perform this function\n");
  399.                         goto ax;
  400.                     }
  401.                     printf("enter the data where you want to delete after:");
  402.                     scanf("%d",&x);
  403.                     Last=DelAfterData(Last,x);
  404.                 break;
  405.                 case 13:
  406.                     exit(1);
  407.             }
  408.    
  409.         }
  410.     }
  411.  
  412. int main()
  413. {
  414.     node *Last1;
  415.     Last1=CreateList();
  416.     return 0;  
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement