Advertisement
kkricardokaka95

Complete Linked List

Nov 1st, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.21 KB | None | 0 0
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5.  
  6. typedef struct  
  7. {
  8.     struct node* next;
  9.     int data;
  10. }node;
  11.  
  12. typedef struct  
  13. {
  14.     node* start;
  15. }head;
  16.  
  17. void insertbeg(head *t, int ele)
  18. {
  19.     node *p;
  20.     p = (node*)malloc(sizeof(node));
  21.     p->data = ele;
  22.     p->next = t->start;
  23.     t->start = p;
  24. }
  25.  
  26. void insertend(head *t, int ele)
  27. {
  28.     node *p,*q;
  29.     p = (node*)malloc(sizeof(node));
  30.     p->data = ele;
  31.     p->next = NULL;
  32.     q = t->start;
  33.     if (t->start == NULL)
  34.         t->start = p;
  35.     else
  36.     {
  37.         while (q->next != NULL)
  38.         {
  39.             q = q->next;
  40.         }
  41.         q->next = p;
  42.     }
  43. }
  44.  
  45.  
  46.  
  47. int count(head *t)
  48. {
  49.     int c = 0;
  50.     node* q;
  51.     q = t->start;
  52.     while (q)
  53.     {
  54.         c++;
  55.         q = q->next;
  56.     }
  57.     return c;
  58. }
  59.  
  60. int findmax(head *t)
  61. {
  62.     int max;
  63.     node* q;
  64.     q = t->start;
  65.     if (q == NULL)
  66.         printf("\nLinked List Empty\n");
  67.     else
  68.     {
  69.         max = q->data;
  70.         while (q!=NULL)
  71.         {
  72.             if (q->data > max)
  73.                 max = q->data;
  74.             q = q->next;
  75.         }
  76.         return max;
  77.     }
  78.     return 0;
  79. }
  80.  
  81. void display(head *t)
  82. {
  83.     node* q;
  84.     q = t->start;
  85.     if (!q)
  86.     {
  87.         printf("\nLinked List Empty\n");
  88.         return;
  89.     }
  90.     while (q)
  91.     {
  92.         printf("%d\t", q->data);
  93.         q = q->next;
  94.     }
  95.     printf("\n");
  96. }
  97.  
  98. void sort(head *t)
  99. {
  100.     node *p, *q = t->start;
  101.     int temp;
  102.     p = q->next;
  103.     while (q->next)
  104.     {
  105.         if (p->data > q->data)
  106.         {
  107.             temp = p->data;
  108.             p->data = q->data;
  109.             q->data = temp;
  110.         }
  111.         p = p->next;
  112.         q = q->next;
  113.     }
  114.     display(t);
  115. }
  116.  
  117. void insertpos(head *t, int ele, int pos)
  118. {
  119.     node *p, *q;
  120.     int i;
  121.     p = (node*)malloc(sizeof(node));
  122.     p->data = ele;
  123.     q = t->start;
  124.     for (i = 0; i < pos - 2; ++i)
  125.         q = q->next;
  126.     p->next = q->next;
  127.     q->next = p;
  128. }
  129.  
  130. int search(head *t, int ele)
  131. {
  132.     node *q;
  133.     q = t->start;
  134.     while (q)
  135.     {
  136.         if (q->data == ele)
  137.             return 1;
  138.         else
  139.             q = q->next;
  140.     }
  141.     return 0;
  142. }
  143.  
  144. int mean(head *t)
  145. {
  146.     int avg=0;
  147.     node *q;
  148.     q = t->start;
  149.     while (q->next)
  150.     {
  151.         avg += q->data;
  152.         q = q->next;
  153.     }
  154.     avg /= count(t);
  155.     return avg;
  156. }
  157.  
  158. int deletebeg(head *t)
  159. {
  160.     node* q = t->start;
  161.     if (!q)
  162.     {
  163.         printf("\nLinked List empty.\n");
  164.         return -1;
  165.     }
  166.     else
  167.         t->start = q->next;
  168.     return q->data;
  169. }
  170.  
  171. void deleteele(head* t, int ele)
  172. {
  173.     node *q,*p;
  174.     if (!search(t, ele))
  175.     {
  176.         printf("\nElement not found\n");
  177.         return;
  178.     }
  179.     q = t->start;
  180.     p = q->next;
  181.     while (q->next)
  182.     {
  183.         if (p->data = ele)
  184.         {
  185.             q->next = p->next;
  186.             return;
  187.         }
  188.         q = q->next;
  189.         p = p->next;
  190.     }
  191. }
  192.  
  193. void main()
  194. {
  195.     head x;
  196.     int ch, ele,pos;
  197.     x.start = NULL;
  198.     while (1)
  199.     {
  200.         printf("\nEnter your choice.\n1.Insertbeg\n2.Insertend\n3.Display\n4.Sort\n5.Count\n6.Deletebeg\n7.Deleteele\n8.InsertPos\n9.Findmax\n10.Search\n11.Mean\n12.Exit\n\n");
  201.         scanf("%d", &ch);
  202.         if (ch == 12)
  203.             break;
  204.         switch (ch)
  205.         {
  206.         case 1:
  207.             printf("\nEnter the element to be inserted\n");
  208.             scanf("%d", &ele);
  209.             insertbeg(&x, ele);
  210.             display(&x);
  211.             break;
  212.         case 2:
  213.             printf("\nEnter the element to be inserted\n");
  214.             scanf("%d", &ele);
  215.             insertend(&x, ele);
  216.             display(&x);
  217.             break;
  218.         case 3:
  219.             display(&x);
  220.             break;
  221.         case 4:
  222.             sort(&x);
  223.             break;
  224.         case 5:
  225.             printf("\nThe number of elements in the Linked List is %d", count(&x));
  226.             break;
  227.         case 6:
  228.             if (x.start == NULL)
  229.                 printf("\nLinked List empty\n");
  230.             else
  231.                 printf("\nThe deleted element is %d", deletebeg(&x));
  232.             break;
  233.         case 7:
  234.             printf("\nEnter the element to be deleted\n");
  235.             scanf("%d", &ele);
  236.             deleteele(&x, ele);
  237.             display(&x);
  238.             break;
  239.         case 8:
  240.             printf("\nenter the element to be inserted and its position\n");
  241.             scanf("%d%d", &ele, &pos);
  242.             insertpos(&x, ele, pos);
  243.             display(&x);
  244.             break;
  245.         case 9:
  246.             if (x.start == NULL)
  247.                 printf("\nLinked List Empty\n");
  248.             else
  249.                 printf("\nMaximum of the Linked List is %d", findmax(&x));
  250.             break;
  251.         case 10:
  252.             printf("\nEnter element to be searched\n");
  253.             scanf("%d", &ele);
  254.             if (search(&x, ele))
  255.             {
  256.                 printf("\nElement is found\n");
  257.             }
  258.             else
  259.                 printf("\nNot Found\n");
  260.        
  261.             break;
  262.         case 11:
  263.             if (x.start == NULL)
  264.                 printf("\nLinked List Empty\n");
  265.             else
  266.                 printf("\nAverage of the elements of the Linked List is %d", mean(&x));
  267.             break;
  268.         default:
  269.             printf("\nInvalid Choice\n");
  270.         }
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement