immuntasir

Linked List - Assignment

Sep 8th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.86 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Node
  4. {
  5.     int Value;
  6.     struct Node *Next;
  7. };
  8.  
  9. struct Node *Head, *LL, *Temp, *Temp2;
  10.  
  11. void Create_A_Node(int V)
  12. {
  13.     Temp = (struct Node *)malloc(sizeof(struct Node));
  14.  
  15.     if(!Temp)
  16.     {
  17.         printf("Something Wrong with Node Creation\n");
  18.         exit(0);
  19.     }
  20.     else
  21.     {
  22.         Temp->Value = V;
  23.         Temp->Next = NULL;
  24.     }
  25. }
  26.  
  27. void Free_A_Node(struct Node *A_Node)
  28. {
  29.     free(A_Node);
  30. }
  31. void Menu()
  32. {
  33.     printf("\n 1. Create a Linked List");
  34.     printf("\n 2. Insert a value into a Linked List (at the end)");
  35.     printf("\n 3. Insert a value into a Linked List (after a specific value)");
  36.     printf("\n 4. Insert a value into a Linked List (before a specific value)");
  37.     printf("\n 5. Update a value with another value");
  38.     printf("\n 6. Delete a value from the Linked List");
  39.     printf("\n 7. Delete a value which is before a specific value");
  40.     printf("\n 8. Delete a value which is after a specific value");
  41.     printf("\n 9. Print the Linked List");
  42.     printf("\n10. Print the Linked List in Reverse Order (For Bonus Marks)");
  43.     printf("\nEnter your Choice : (1-10, 0 to exit):");
  44. }
  45.  
  46. void Option1()
  47. {
  48.     int V;
  49.     printf("You have selection Option1\n");
  50.  
  51.  
  52.     if(Head==NULL) // That is, the Linked list is yet to create
  53.     {
  54.         printf("Enter an integer to create the Linked List ; ");
  55.         scanf("%d",&V);
  56.         Create_A_Node(V); // Calling this function with V and the new Node will be created as Temp;
  57.         Head = Temp;    // Now the Head will Point to the Temp;
  58.         printf("The Linked List is Created\n");
  59.     }
  60.     else
  61.     {
  62.         printf("The Linked List is already created\n");
  63.     }
  64.  
  65.  
  66. }
  67.  
  68. void Option2()
  69. {
  70.     int V;
  71.     printf("You have selection Option2 (Insert a value into a Linked List (at the end))\n");
  72.  
  73.     LL = Head;
  74.  
  75.     printf("Enter an integer to insert into the Linked List ; ");
  76.     scanf("%d",&V);
  77.  
  78.     if(LL==NULL)
  79.     {
  80.         printf("There is No Linked List. So, the Linked list will be created First\n");
  81.         Create_A_Node(V); // Calling this function with V and the new Node will be created as Temp;
  82.         Head = Temp;    // Now the Head will Point to the Temp;
  83.     }
  84.     else
  85.     {
  86.         while(LL->Next) // LL will traverse till the end of the Linked List or the Node that has Null in it's next
  87.         {
  88.             LL = LL->Next;
  89.         }
  90.  
  91.         Create_A_Node(V); // Calling this function with V and the new Node will be created as Temp;
  92.         LL->Next = Temp; // That newly created node will be added to the end of the linked list (as the LL is now at the last node after traversing Linked List
  93.         printf("Data Inserted at the end of the Linked List\n");
  94.     }
  95.  
  96. }
  97.  
  98. void Option3()
  99. {
  100.     printf("You have selection Option3\n");
  101.     printf("(Insert a value into a Linked List (after a specific value))");
  102.     printf("Where do you want to insert the integer. After : ");
  103.     int u, v;
  104.     scanf("%d", &u);
  105.     printf("Enter the integer: ");
  106.     scanf("%d", &v);
  107.     LL = Head;
  108.     if(LL==NULL)
  109.     {
  110.         printf("There is No Linked List. So, the Linked list will be created First\n");
  111.         Create_A_Node(v);
  112.         Head = Temp;
  113.     }
  114.     else {
  115.         while(LL->Value != u){
  116.             LL = LL->Next;
  117.             if (LL->Next == NULL) break;
  118.         }
  119.         if (LL->Value == u) {
  120.             Temp2 = LL->Next;
  121.             Create_A_Node(v);
  122.             LL->Next = Temp;
  123.             Temp->Next = Temp2;
  124.             printf("\n\n%d %d %d\n\n", LL->Value, Temp->Value , Temp2->Value);
  125.             printf("Data Inserted successfully!\n");
  126.         }
  127.         else {
  128.             printf("Not found.\n");
  129.         }
  130.     }
  131. }
  132.  
  133. void Option4()
  134. {
  135.     printf("You have selection Option4\n");
  136.     printf("\n 4. Insert a value into a Linked List (before a specific value)");
  137.     printf("Where do you want to insert the integer. After : ");
  138.     int u, v;
  139.     scanf("%d", &u);
  140.     printf("Enter the integer: ");
  141.     scanf("%d", &v);
  142.     LL = Head;
  143.     if(LL==NULL)
  144.     {
  145.         printf("There is No Linked List. So, the Linked list will be created First\n");
  146.         Create_A_Node(v); // Calling this function with V and the new Node will be created as Temp;
  147.         Head = Temp;    // Now the Head will Point to the Temp;
  148.     }
  149.     else {
  150.         if (LL->Value == u) {
  151.             Create_A_Node(v);
  152.             Head = Temp;
  153.             Temp->Next = LL;
  154.         }
  155.         else {
  156.             while(LL->Next->Value != u){
  157.                 LL = LL->Next;
  158.                 if (LL->Next->Next == NULL) break;
  159.             }
  160.             if (LL->Next->Value == u) {
  161.                 Create_A_Node(v);
  162.                 Temp->Next = LL->Next;
  163.                 LL->Next = Temp;
  164.                 printf("Added successfully!\n");
  165.             }
  166.             else printf("Not found!\n");
  167.         }
  168.     }
  169.  
  170. }
  171.  
  172. void Option5()
  173. {
  174.     printf("You have selection Option5\n");
  175.     printf("\n 5. Update a value with another value");
  176.  
  177.     printf("Which value do you want to update?");
  178.     int u,v;
  179.     scanf("%d", &u);
  180.     printf("Enter the integer: ");
  181.     scanf("%d", &v);
  182.     LL = Head;
  183.     if(LL==NULL)
  184.     {
  185.         printf("There is No Linked List. So, the Linked list will be created First\n");
  186.         Create_A_Node(v); // Calling this function with V and the new Node will be created as Temp;
  187.         Head = Temp;    // Now the Head will Point to the Temp;
  188.     }
  189.     else {
  190.         while (LL->Value != u) {
  191.             LL = LL->Next;
  192.             if (LL->Next == NULL) break;
  193.         }
  194.         if (LL->Value == u) LL->Value = v;
  195.         else printf("Not found!");
  196.     }
  197. }
  198.  
  199. void Option6()
  200. {
  201.     printf("You have selection Option6\n");
  202.     printf("\n 6. Delete a value from the Linked List");
  203.     printf("Which value do you want to delete?");
  204.     int u;
  205.     int found = 0;
  206.     scanf("%d", &u);
  207.     LL = Head;
  208.     if(LL==NULL)
  209.     {
  210.         printf("Sorry, there is No Linked List.\n");
  211.     }
  212.     else {
  213.         if (LL->Value == u) {
  214.             Head = LL->Next;
  215.             free(LL);
  216.             printf("Deleted!\n");
  217.         }
  218.         else {
  219.             while (LL->Next->Value != u) {
  220.                 LL = LL->Next;
  221.                 if (LL->Next->Next == NULL) break;
  222.             }
  223.             if (LL->Next->Value == u) found = 1;
  224.             if (found) {
  225.                 Temp = LL->Next;
  226.                 LL->Next = LL->Next->Next;
  227.                 free(Temp);
  228.                 printf("Deleted!\n");
  229.             }
  230.             else printf("Not found!\n");
  231.         }
  232.     }
  233.  
  234. }
  235.  
  236. void Option7()
  237. {
  238.     printf("You have selection Option7\n");
  239.     printf("\n 7. Delete a value which is before a specific value");
  240.     printf("Enter the value: ");
  241.     int u;
  242.     scanf("%d", &u);
  243.     LL = Head;
  244.     if (LL == NULL) printf("Sorry, there is no linked list.\n");
  245.     else if (LL->Value == u) printf("This is the first element!\n");
  246.     else if (LL->Next->Value == u) {
  247.         Head = LL->Next;
  248.         free(LL);
  249.         printf("Deleted!\n");
  250.     }
  251.     else {
  252.         int found = 0;
  253.         while (LL->Next->Next->Value != u) {
  254.             LL = LL->Next;
  255.             if (LL->Next->Next->Next == NULL) break;
  256.         }
  257.         if (LL->Next->Next->Value == u) found = 1;
  258.         if (found) {
  259.             Temp = LL->Next;
  260.             LL->Next = LL->Next->Next;
  261.             free(Temp);
  262.             printf("Deleted!\n");
  263.         }
  264.         else printf("Not found!\n");
  265.     }
  266.  
  267. }
  268.  
  269. void Option8()
  270. {
  271.     printf("You have selection Option8\n");
  272.     printf("\n 8. Delete a value which is after a specific value");
  273.     printf("Enter the integer: ");
  274.     int u;
  275.     scanf("%d", &u);
  276.     LL = Head;
  277.     if (!LL) printf("Sorry, there is no linked list!\n");
  278.     else {
  279.         while (LL->Value!=u) {
  280.             LL = LL->Next;
  281.             if (LL->Next == NULL) break;
  282.         }
  283.         if (!LL->Next && LL->Value == u) printf("This is the last element!\n");
  284.         else if (LL->Value != u) printf("The element was not found!\n");
  285.         else {
  286.             Temp2 = LL->Next;
  287.             LL->Next = LL->Next->Next;
  288.             free(Temp2);
  289.             printf("Deleted!\n");
  290.         }
  291.     }
  292. }
  293.  
  294. void Option9()
  295. {
  296.     printf("\nYou have selection Option9\n");
  297.     /*
  298.     printf("\n 9. Print the Linked List");
  299.     */
  300.  
  301.     LL = Head;
  302.     while(LL!=NULL) // Or you can write while(LL)
  303.     {
  304.         printf("%d\n",LL->Value);
  305.         LL= LL->Next;
  306.     }
  307.  
  308. }
  309. void printReverese (struct Node *head) {
  310.     if (head == NULL) return;
  311.     printReverese(head->Next);
  312.     printf("%d\n", head->Value);
  313.     return;
  314. }
  315. void Option10()
  316. {
  317.     printf("You have selection Option10\n");
  318.     printf("\n10. Print the Linked List in Reverse Order (For Bonus Marks)\n");
  319.     printReverese(Head);
  320. }
  321.  
  322.  
  323. void SelectOptions(int Ch)
  324. {
  325.     if(Ch==1)       Option1();
  326.     else if(Ch==2)  Option2();
  327.     else if(Ch==3)  Option3();
  328.     else if(Ch==4)  Option4();
  329.     else if(Ch==5)  Option5();
  330.     else if(Ch==6)  Option6();
  331.     else if(Ch==7)  Option7();
  332.     else if(Ch==8)  Option8();
  333.     else if(Ch==9)  Option9();
  334.     else if(Ch==10) Option10();
  335. }
  336.  
  337. void main()
  338. {
  339.     freopen("in.txt", "r", stdin);
  340.     int Choice;
  341.     while(1)
  342.     {
  343.         Menu();
  344.         scanf("%d", &Choice);
  345.         if(Choice==0)
  346.             break;
  347.         else if(Choice<0 || Choice>10)
  348.             printf("Wrong input\n");
  349.         else
  350.             SelectOptions(Choice);
  351.     }
  352.  
  353. }
Advertisement
Add Comment
Please, Sign In to add comment