Kawsar_Hossain

Untitled

Oct 25th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.04 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4. #include<process.h>
  5.  
  6. struct node
  7. {
  8.     int info;
  9.     struct node *link;
  10. }*start;
  11.  
  12. void Create_List(int data)
  13. {
  14.     struct node *q,*tmp;
  15.  
  16.     tmp= (struct node*)malloc(sizeof(struct node));
  17.     tmp->info=data;
  18.     tmp->link=NULL;
  19.     if(start==NULL)
  20.         start=tmp;
  21.     else
  22.     {
  23.         q=start;
  24.         while(q->link!=NULL)
  25.             q=q->link;
  26.         q->link=tmp;
  27.     }
  28. }
  29.  
  30. void AddatBegin(int data)
  31. {
  32.     struct node *tmp;
  33.     tmp=(struct node*)malloc(sizeof(struct node));
  34.     tmp->info=data;
  35.     tmp->link=start;
  36.     start=tmp;
  37. }
  38.  
  39. void insertLast(int data)
  40. {
  41.     struct node *q=start,*tmp;
  42.     while(q!=NULL && q->link!=NULL)
  43.     {
  44.         q=q->link;
  45.     }
  46.     tmp=(struct node*)malloc(sizeof(struct node));
  47.     tmp->info=data;
  48.     tmp->link=NULL;
  49.     if(q!=NULL)
  50.     {
  51.         q->link=tmp;
  52.     }
  53.     else
  54.     {
  55.         start=tmp;
  56.     }
  57. }
  58.  
  59. void insertBefore(int data,int pos)
  60. {
  61.     struct node *q;
  62.     q=(struct node*)malloc(sizeof(struct node));
  63.     q->info=data;
  64.     int i;
  65.     struct node *tmp=start;
  66.     if(pos==1)
  67.     {
  68.         AddatBegin(data);
  69.         return;
  70.     }
  71.     for(i=2;i<pos;i++)
  72.     {
  73.         tmp=tmp->link;
  74.     }
  75.     q->link=tmp->link;
  76.     tmp->link=q;
  77.  
  78. }
  79.  
  80. int getFirst()
  81. {
  82.     struct node *q=start;
  83.     if(q!=NULL)
  84.     {
  85.         return q->info;
  86.     }
  87.     else
  88.     {
  89.         printf("\nNot Found!!!\n");
  90.     }
  91. }
  92.  
  93. int getLast()
  94. {
  95.     struct node *q=start;
  96.     if(q==NULL)
  97.     {
  98.         printf("\nList is empty!!!\n");
  99.     }
  100.     else
  101.     {
  102.         while(q->link!=NULL)
  103.         {
  104.             q=q->link;
  105.         }
  106.         return q->info;
  107.     }
  108. }
  109.  
  110. int get(int pos)
  111. {
  112.     struct node *q=start;
  113.     int c=1;
  114.     while(q!=NULL)
  115.     {
  116.         if(c==pos)
  117.         {
  118.             printf("\nData: %d\n", q->info);
  119.         }
  120.         q=q->link;
  121.         c++;
  122.     }
  123.     printf("\nThere's no element in that position!!!\n");
  124. }
  125.  
  126. void DelfirstNode()
  127. {
  128.     struct node *tmp,*q;
  129.         tmp=start;
  130.         start=start->link;
  131.         free(tmp);
  132.         return;
  133.  
  134. }
  135.  
  136. void delLast()
  137. {
  138.     struct node *q=start,*tmp;
  139.     if(start->info!=NULL)
  140.     {
  141.         while(q->link->link!=NULL)
  142.         {
  143.             q=q->link;
  144.         }
  145.         tmp=q->link;
  146.         free(tmp);
  147.         q->link=NULL;
  148.     }
  149.  
  150. }
  151.  
  152. void delAt(int pos)
  153. {
  154.     struct node *q,*tmp;
  155.     q=start;
  156.     int c=0;
  157.     if(start->info!=NULL)
  158.     {
  159.         if(pos==1)
  160.         {
  161.             DelfirstNode();
  162.             return;
  163.         }
  164.         while(q->link!=NULL)
  165.         {
  166.             if(c==pos-2)
  167.             {
  168.                 tmp=q->link;
  169.                 q->link=tmp->link;
  170.                 free(tmp);
  171.                 return;
  172.             }
  173.             q=q->link;
  174.             c++;
  175.         }
  176.     }
  177. }
  178. void Display()
  179. {
  180.     struct node *q;
  181.     if(start == NULL)
  182.     {
  183.         printf ("\n\nList is empty");
  184.         return;
  185.     }
  186.     q=start;
  187.     printf("\n\nList is : ");
  188.     while(q!=NULL)
  189.     {
  190.         printf ("%d ", q->info);
  191.         q=q->link;
  192.     }
  193.     printf ("\n");
  194.     getch();
  195. }
  196.  
  197. int search_list(int data)
  198. {
  199.     struct node *q=start;
  200.     int pos=1;
  201.     while(q!=NULL)
  202.     {
  203.         if(q->info==data)
  204.         {
  205.             printf("\nFound at position = %d\n", pos);
  206.             getch();
  207.         }
  208.         q=q->link;
  209.         pos++;
  210.     }
  211.     if(q==NULL)
  212.     {
  213.         printf("\nNot found!");
  214.         getch();
  215.     }
  216. }
  217.  
  218. void Rev()
  219. {
  220.     struct node *p1,*p2,*p3;
  221.     if(start->link==NULL)
  222.         return;
  223.     p1=start;
  224.     p2=p1->link;
  225.     p3=p2->link;
  226.     p1->link=NULL;
  227.     p2->link=p1;
  228.     while(p3!=NULL)
  229.     {
  230.         p1=p2;
  231.         p2=p3;
  232.         p3=p3->link;
  233.         p2->link=p1;
  234.     }
  235.     start=p2;
  236. }
  237.  
  238. void main()
  239. {
  240.     int choice,n,m,position,i;
  241.     start=NULL;
  242.     while(1)
  243.     {
  244.         printf("============================================\n");
  245.         printf("SINGLY LINKED LIST PROGRAM\n");
  246.         printf("============================================\n");
  247.         printf ("1.Create List\n");
  248.         printf ("2.Insert an element at the beginning\n");
  249.         printf ("3.Insert an element to the end of the list\n");
  250.         printf ("4.Insert an element at a specific position\n");
  251.         printf ("5.Display the complete list\n");
  252.         printf ("6.Display first element\n");
  253.         printf ("7.Display last element\n");
  254.         printf ("8.Display from a specific position\n");
  255.         printf ("9.Delete First Node\n");
  256.         printf ("10.Delete last node\n");
  257.         printf ("11.Delete node from specific position\n");
  258.         printf ("12.Search\n");
  259.         printf ("13.Reverse\n");
  260.         printf ("14.Quit\n");
  261.         printf ("\nEnter your choice:");
  262.         scanf ("%d",&choice);
  263.         switch (choice)
  264.         {
  265.         case 1:
  266.             printf ("\n\nHow many nodes you want:");
  267.             scanf ("%d",&n);
  268.             for(i = 0; i<n; i++)
  269.             {
  270.                 printf ("\nEnter element %d: ", i+1);
  271.                 scanf ("%d",&m);
  272.                 Create_List(m);
  273.             }
  274.             printf("\n");
  275.             break;
  276.  
  277.         case 2:
  278.             printf("\nEnter data: ");
  279.             scanf("%d", &n);
  280.             AddatBegin(n);
  281.             printf("\nData inserted successfully!!!\n");
  282.             break;
  283.  
  284.         case 3:
  285.             printf("\nEnter the data to insert: ");
  286.             scanf("%d", &n);
  287.             insertLast(n);
  288.             printf("\nData inserted successfully!!!\n");
  289.             break;
  290.  
  291.         case 4:
  292.             printf("\nEnter data: ");
  293.             scanf("%d", &m);
  294.             printf("\nEnter position: ");
  295.             scanf("%d", &n);
  296.             insertBefore(m,n);
  297.             printf("\nData inserted successfully!!!\n");
  298.             break;
  299.  
  300.         case 5:
  301.             Display();
  302.             break;
  303.  
  304.         case 6:
  305.             printf("\nFirst element of the list: %d\n", getFirst());
  306.             break;
  307.  
  308.         case 7:
  309.             {
  310.                 printf("\nLast element of the list: %d", getLast());
  311.                 break;
  312.             }
  313.  
  314.         case 8:
  315.             printf("\nEnter the position: ");
  316.             scanf("%d", &m);
  317.             get(m);
  318.             break;
  319.  
  320.         case 9:
  321.             DelfirstNode();
  322.             printf("\nNode deleted successfully!!!\n");
  323.             break;
  324.  
  325.         case 10:
  326.             delLast();
  327.             printf("\nNode deleted successfully!!!\n");
  328.             break;
  329.  
  330.         case 11:
  331.             printf("\nEnter position: ");
  332.             scanf("%d", &n);
  333.             delAt(n);
  334.             printf("\nNode deleted successfully!!!\n");
  335.             break;
  336.  
  337.         case 12:
  338.             printf("\nEnter the element to search: ");
  339.             scanf("%d", &n);
  340.             search_list(n);
  341.             break;
  342.  
  343.         case 13:
  344.             Rev();
  345.             printf("\nList reversed successfully!!!\n");
  346.             break;
  347.  
  348.         case 14:
  349.             exit(0);
  350.         default:
  351.             printf ("\n\nWrong choice");
  352.         }
  353.     }
  354. }
Add Comment
Please, Sign In to add comment