Advertisement
varungurnaney

DSA: Circular LinkedList

Jul 31st, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. }rear=NULL;
  10. void insert_behind(int x)
  11. {
  12.  
  13.     struct node *p=(struct node *)malloc(sizeof(struct node));
  14.     p->data=x;
  15.     p->next=NULL;
  16.    
  17.     if(rear==NULL)
  18.         {
  19.                 rear=p;
  20.                 p->next=p;
  21.                 return;
  22.            
  23.         }
  24.        
  25.     p->next=rear->next;
  26.     rear->next=p;
  27.     rear=p;
  28.  
  29. }
  30.  
  31. void insert_front(int x)
  32. {
  33. struct node *p=(struct node *)malloc(sizeof(struct node));
  34.     p->data=x;
  35.     p->next=NULL;
  36.     if(rear==NULL)
  37.         {
  38.                 rear=p;
  39.                 p->next=p;
  40.                 return;
  41.            
  42.         }
  43.        
  44.     p->next=rear->next;
  45.     rear->next=p; //rear is not moved
  46. }
  47.  
  48. void traverse_print()
  49. {
  50.  
  51. struct node *temp;
  52. if(rear!=NULL)
  53.     {
  54.     temp=temp->next; //traverse from front node
  55.         while(temp!=rear->next)
  56.         {
  57.             printf("%d\t"p->data);
  58.             p=p->next;
  59.         }
  60.     }
  61. }
  62.  
  63.  
  64. }
  65.  
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement