Advertisement
d1i2p3a4k5

11.Circular Linked list (DS)

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