Sunday, 6 May 2012

Circular Linked List in C


#include<stdio.h>
#include<conio.h>


typedef struct nodetype
{
        int info;
        struct nodetype *link;
}node;
node *head;


int main()
{
    void addnode(int);
    void display();
    void del();
    int value;
    char choice;
    head=(node*)malloc(sizeof(node));
    head->link=head;
    while(1)
    {
            printf("\nDo you wish to insert the node (Y/N) ?");
            scanf("%c",&choice);
            if(choice=='y'||choice=='Y')
            {
printf("Enter the value of the node\n");
scanf("%d",&value);
addnode(value);
fflush(stdin);
}
else
{
break;
}
}
printf("\nThe Circular Linked List is: ");
display();
del();
getch();
main();
return 0;
}


void addnode(int data)
{
node *newptr=NULL;
newptr=(node*)malloc(sizeof(node));
newptr->info=data;
newptr->link=head->link;
head->link=newptr;
}


void display()
{
node *ptr=head->link;
if(ptr==head)
{
printf("\nEmpty");
return;
}
printf("\n\n->");
while(ptr!=NULL)
{
printf("%d->",ptr->info);
ptr=ptr->link;
}
printf("\n");
}


void del()
{
node *ptr,*temp;
temp=head->link;
while(temp!=head)
{
ptr=temp;
temp=temp->link;
free(ptr);
}
free(head);
}


No comments:

Post a Comment