Advertisement
Robert_JR

One Way Linked List

Oct 21st, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. /*
  2.  * Rijoanul Hasan *
  3.  * One Way Linked List *
  4.  * All Operations ( Almost :v ) *
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. struct Node
  10. {
  11.     int data;
  12.     struct Node *next;
  13. };
  14.  
  15. typedef struct Node node;
  16.  
  17. node *head = 0;
  18.  
  19. void append(int data)
  20. {
  21.     if(head == 0)
  22.     {
  23.         head = (node *)malloc(sizeof(node));
  24.         head->data = data;
  25.         head->next = 0;
  26.     }
  27.     else
  28.     {
  29.         node *temp = (node *)malloc(sizeof(node));
  30.         temp = head;
  31.         while(temp->next != 0)
  32.         {
  33.             temp = temp->next;
  34.         }
  35.         node *new_node = (node *)malloc(sizeof(node));
  36.         new_node->data = data;
  37.         new_node->next = 0;
  38.         temp->next = new_node;
  39.     }
  40. }
  41. void insert_tail(int data)
  42. {
  43.     node *temp = (node *)malloc(sizeof(node));
  44.     temp = head;
  45.     while(temp->next != 0)
  46.     {
  47.         temp = temp->next;
  48.     }
  49.     node *tail = (node *)malloc(sizeof(node));
  50.     tail->data = data;
  51.     tail->next = 0;
  52.     temp->next = tail;
  53. }
  54.  
  55. void count()
  56. {
  57.     int count = 1;
  58.     node *temp = (node *)malloc(sizeof(node));
  59.     temp = head;
  60.     while(temp->next != 0)
  61.     {
  62.         temp = temp->next;
  63.         count++;
  64.     }
  65.     printf("Total Number = %d\n", count);
  66. }
  67.  
  68. void modify_num(int pos, int data)
  69. {
  70.     int i;
  71.     node *temp = (node *)malloc(sizeof(node));
  72.     temp = head;
  73.     for(i = 0; i < pos - 1; i++)
  74.     {
  75.         temp = temp->next;
  76.     }
  77.     temp->data = data;
  78. }
  79.  
  80. void delete_node(int pos)
  81. {
  82.     int i;
  83.     node *temp = (node *)malloc(sizeof(node));
  84.     node *prev = (node *)malloc(sizeof(node));
  85.     temp = head;
  86.     for(i = 0; i < pos - 1; i++)
  87.     {
  88.         prev = temp;
  89.         temp = temp->next;
  90.     }
  91.     prev->next = temp->next;
  92.     free(temp);
  93. }
  94.  
  95. void search(node *head, int data)
  96. {
  97.     node *temp = (node *)malloc(sizeof(node));
  98.     temp = head;
  99.     while(temp->next != 0)
  100.     {
  101.         temp = temp->next;
  102.         if(temp->data == data)
  103.         {
  104.             printf("Found!\n");
  105.             return;
  106.         }
  107.     }
  108.     printf("Not Found!\n");
  109. }
  110.  
  111. void display()
  112. {
  113.     node *temp = (node *)malloc(sizeof(node));
  114.     temp = head;
  115.     while(temp->next != 0)
  116.     {
  117.         printf("%d ", temp->data);
  118.         temp = temp->next;
  119.     }
  120.     printf("%d\n", temp->data);
  121. }
  122.  
  123. int main()
  124. {
  125.     append(12);
  126.     append(19);
  127.     append(19);
  128.     append(18);
  129.     append(15);
  130.     display();
  131.     delete_node(3);
  132.     display();
  133.     printf("\n80 : ");
  134.     search(head, 80);
  135.     printf("\n");
  136.     display();
  137.     printf("\n");
  138.     count();
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement