d1i2p3a4k5

circular linked list with ascending order insertion

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