Advertisement
d1i2p3a4k5

7.Linked List (DS)

Nov 1st, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #define max 20
  5. typedef struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. }node;
  10. typedef struct
  11. {
  12.      node *start;
  13. }head;
  14. void insertend(head *t,int ele)
  15. {
  16.     node *p,*q;
  17.     p = (node*)malloc(sizeof(node));
  18.     p->data=ele;
  19.     p->next=NULL;
  20.     if(t->start==NULL)
  21.     {
  22.         t->start=p;
  23.         return;
  24.     }
  25.     q=t->start;
  26.     while(q->next!=NULL)
  27.     {
  28.         q = q->next;
  29.     }
  30.     q->next = p;
  31.     return ;
  32. }
  33. void count(head *t)
  34. {
  35.     node *q;
  36.     int c=0;
  37.     if(t->start==NULL)
  38.     {
  39.         printf("Linked List is empty");
  40.         return;
  41.     }
  42.     q=t->start;
  43.     while(q!=NULL)
  44.     {
  45.         c=c+1;
  46.         q=q->next;
  47.     }
  48.     printf("Total no. of elements in Linked list is %d",c);
  49.     return;
  50. }
  51. void findmax(head *t)
  52. {
  53.     int z;
  54.     node *p;
  55.     if(t->start==NULL)
  56.     {
  57.         printf("Linked list is Empty");
  58.         return;
  59.     }
  60.     z = t->start->data;
  61.     p = t->start->next;
  62.     while(p->next!=NULL)
  63.     {
  64.         if(z<p->data)
  65.         {
  66.             z = p->data;
  67.         }
  68.         p = p->next;
  69.     }
  70.     printf("Maximum element in Linked list %d",z);
  71.     return;
  72. }
  73. void display(head *t)
  74. {
  75.     node *q;
  76.     if(t->start==NULL)
  77.     {
  78.         printf("\nLinked list is empty");
  79.         return;
  80.     }
  81.     q = t->start;
  82.     printf("\nElement of Linked list is given below\n");
  83.     while(q!=NULL)
  84.     {
  85.         printf("%d\t",q->data);
  86.         q=q->next;
  87.     }
  88.     return;
  89. }
  90. int main()
  91. {
  92.     head x;
  93.     int ch,ele;
  94.     printf("\nImplementation of Linked list with function insertend,display,findmax,count\n");
  95.     while(1)
  96.     {
  97.         printf("\nenter choice \n1.InsertEnd 2.Findmax 3.Count 4.display 5.Exit\n");
  98.         scanf_s("%d",&ch);
  99.         if(ch==5)
  100.         {
  101.             break;
  102.         }
  103.         else
  104.         {
  105.             switch(ch)
  106.             {
  107.             case 1:printf("\nenter element to insert");
  108.                 scanf_s("%d",&ele);
  109.                 insertend(&x,ele);
  110.                 break;
  111.             case 2:findmax(&x);
  112.                 break;
  113.             case 3:count(&x);
  114.                 break;
  115.             case 4:display(&x);
  116.                 break;
  117.             default:printf("\ninvalid input");
  118.             }
  119.         }
  120.     }
  121.     _getch();
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement