Guest User

Untitled

a guest
Jun 6th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. struct node
  5. {
  6.     int data;
  7.     struct node *next;
  8. };
  9.  
  10. struct node *link = NULL;
  11. void create_ll();
  12. void display();
  13. void insert_beginning();
  14. void insert_end();
  15. void insert_before();
  16. void insert_after();
  17. void delete_beginning();
  18. void delete_end();
  19. void delete_target();
  20. void delete_after();
  21. void delete_all();
  22.  
  23. int main(){
  24.     int option;
  25.  
  26.     do{
  27.         printf("\n1. Create a list");
  28.         printf("\n2. Display a list");
  29.         printf("\n3. Add node at beginning");
  30.         printf("\n4. Add node at end");
  31.         printf("\n5. Add node before a given node");
  32.         printf("\n6. Add node after a given node");
  33.         printf("\n7. Delete node at beginning");
  34.         printf("\n8. Delete node at end");
  35.         printf("\n9. Delete a given node");
  36.         printf("\n10. Delete a node after a given node");
  37.         printf("\n11. Delete all");
  38.         printf("\n12. Sort list");
  39.         printf("\n13. EXIT");
  40.  
  41.         printf("\n\n Enter your option : ");
  42.         scanf("%d", &option);
  43.  
  44.         switch(option){
  45.             case 1 : create_ll();
  46.             printf("Linked List created");
  47.             break;
  48.             case 2 : display();
  49.             break;
  50.             case 3 : insert_beginning();
  51.             break;
  52.             case 4 : insert_end();
  53.             break;
  54.             case 5 : insert_before();
  55.             break;
  56.             case 6 : insert_after();
  57.             break;
  58.             case 7 : delete_beginning();
  59.             break;
  60.             case 8 : delete_end();
  61.             break;
  62.             case 9 : delete_target();
  63.             break;
  64.             case 10 : delete_after();
  65.             break;
  66.             case 11 : delete_all();
  67.             break;
  68.             case 12 :
  69.             //TODO bubble sort di buku
  70.             break;
  71.         }
  72.     }while(option != 13);
  73.  
  74.     getchar();
  75.     return 0;
  76. }
  77.  
  78. void create_ll(){
  79.     struct node *new_node, *ptr;
  80.     int num;
  81.  
  82.     do{
  83.         printf("\n Enter the data : ");
  84.         scanf("%d", &num);
  85.         //fflush(stdin);
  86.  
  87.         new_node = (struct node*)malloc(sizeof(struct node));
  88.         new_node->data = num;
  89.  
  90.         if(link == NULL){
  91.             new_node->next = NULL;
  92.             link = new_node;
  93.         }
  94.         else{
  95.             ptr = link;
  96.  
  97.             while(ptr->next != NULL){
  98.                 ptr = ptr->next;
  99.             }
  100.  
  101.             ptr->next = new_node;
  102.             new_node->next = NULL;
  103.         }
  104.     }while(num != -1);
  105. }
  106.  
  107. void display(){
  108.     struct node *ptr;
  109.     ptr = link;
  110.  
  111.     while(ptr->next != NULL){
  112.         printf("\t %d", ptr->data);
  113.         ptr = ptr->next;
  114.     }
  115. }
  116.  
  117. void insert_beginning(){
  118.     struct node *new_node;
  119.     new_node = (struct node*)malloc(sizeof(struct node));
  120.  
  121.     printf("\n Enter the data : ");
  122.     scanf("%d", &new_node->data);
  123.  
  124.     new_node->next = link;
  125.     link = new_node;
  126. }
  127.  
  128. //TODO bug input jd -1
  129. void insert_end(){
  130.     struct node *new_node, *ptr;
  131.     new_node = (struct node*)malloc(sizeof(struct node));
  132.     ptr = link;
  133.  
  134.     printf("\n Enter the data : ");
  135.     scanf("%d", &new_node->data);
  136.     //fflush(stdin);
  137.  
  138.     while(ptr->next != NULL){
  139.         ptr = ptr->next;
  140.     }
  141.  
  142.     ptr->next = new_node;
  143.     new_node->next = NULL;
  144. }
  145.  
  146. void insert_before(){
  147.     struct node *new_node, *ptr, *preptr;
  148.     int pivot;
  149.  
  150.     new_node = (struct node*)malloc(sizeof(struct node));
  151.     ptr = link;
  152.     preptr = link;
  153.  
  154.     printf("\n Enter the data : ");
  155.     scanf("%d", &new_node->data);
  156.  
  157.     printf("\n Enter the value before which the data has to be inserted : ");
  158.     scanf("%d", &pivot);
  159.  
  160.     while(ptr->data != pivot){
  161.         preptr = ptr;
  162.         ptr = ptr->next;
  163.     }
  164.  
  165.     preptr->next = new_node;
  166.     new_node->next = ptr;
  167. }
  168.  
  169. //TODO kalo void (gk return) selalu crash, pdhl insert_before bisa
  170. void insert_after(){
  171.     struct node *new_node, *ptr, *preptr;
  172.     int pivot;
  173.  
  174.     new_node = (struct node*)malloc(sizeof(struct node));
  175.     ptr = link;
  176.     preptr = link;
  177.  
  178.     printf("\n Enter the data : ");
  179.     scanf("%d", &new_node->data);
  180.  
  181.     printf("\n Enter the value after which the data has to be inserted : ");
  182.     scanf("%d", &pivot);
  183.  
  184.     while(preptr->data != pivot){
  185.         preptr = ptr;
  186.         ptr = ptr->next;
  187.     }
  188.  
  189.     preptr->next = new_node;
  190.     new_node->next = ptr;
  191. }
  192.  
  193. void delete_beginning(){
  194.     struct node *ptr;
  195.  
  196.     ptr = link;
  197.     link = link->next;
  198.     free(ptr);
  199. }
  200.  
  201. void delete_end(){
  202.     struct node *ptr, *preptr;
  203.     ptr = link;
  204.     preptr = link;
  205.  
  206.     while(ptr->next != NULL){
  207.         preptr = ptr;
  208.         ptr = ptr->next;
  209.     }
  210.  
  211.     free(ptr);
  212.     preptr->next = NULL;
  213. }
  214.  
  215. void delete_target(){
  216.     struct node *ptr, *preptr;
  217.     int target;
  218.     ptr = link;
  219.     preptr = link;
  220.  
  221.     printf("\n Enter the data : ");
  222.     scanf("%d", &target);
  223.  
  224.     if(ptr->data == target){
  225.         delete_beginning(ptr);
  226.     }
  227.     else{
  228.         while(ptr->data != target){
  229.             preptr = ptr;
  230.             ptr = ptr->next;
  231.         }
  232.  
  233.         preptr->next = ptr->next;
  234.         free(ptr);
  235.     }
  236. }
  237.  
  238. void delete_after(){
  239.     struct node *ptr, *preptr;
  240.     int pivot;
  241.     ptr = link;
  242.     preptr = link;
  243.  
  244.     printf("\n Enter the data : ");
  245.     scanf("%d", &pivot);
  246.  
  247.     if(preptr->data == pivot){
  248.         ptr = ptr->next;
  249.     }
  250.     //kalo delete pertama msh salah, karena ptr = ptr->next; cmn ada di dalam while
  251.     while(preptr->data != pivot){
  252.         preptr = ptr;
  253.         ptr = ptr->next;
  254.     }
  255.  
  256.     preptr->next = ptr->next;
  257.     free(ptr);
  258. }
  259.  
  260. void delete_all(){
  261.     struct node *ptr;
  262.     ptr = link;
  263.  
  264.     while(ptr->next != NULL){
  265.         delete_beginning(ptr);
  266.         ptr = ptr->next;
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment