You are on page 1of 3

Circular Linked List

#include <stdio.h>

#include <stdlib.h>

struct Node

int data;

struct Node *next,*prev;

};

#define node struct Node

node *root;

void insert(int v)

node* nn=(node*)malloc(sizeof(Node));

nn->data=v;

nn->next=NULL;

nn->prev=NULL;

if(root==NULL)

root=nn;

return;

if(root->data>=v)

nn->next=root;

nn->next->prev=nn;

root=nn;

return;

node* cur=root;
while(cur->next!=NULL && cur->next->data<v)

cur=cur->next;

nn->next=cur->next;

cur->next=nn;

nn->prev=cur;

void print()

if(root==NULL)

puts("List is empty");

return;

printf("List :");

Node *cur;

cur=root;

while(cur!=NULL)

printf(" %d",cur->data);

cur=cur->next;

puts("");

int main()

root=NULL;

int n,i,k;

scanf("%d",&n);
while(n--)

scanf("%d",&k);

insert(k);

print();

You might also like