My Profile

I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.

Monday, 15 August 2011

Create a Circular Linked List

Write a Program in C to Create a Circular Linked List by Accepting Numbers from User and Display it
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void create(int num);
void display();
struct node
{
int data;
struct node *addr_next;
}*end,*q,*tmp;
void main()
{
int n;
char choice='y';
end=NULL;
clrscr();
printf("\t\t\tCreating a Circular Linked List\n");
while(choice=='y'||choice=='Y')
{
printf("\nEnter a number : ");
scanf("%d",&n);
create(n);
printf("\n");
display();
printf("\t\t\t\t\t\t\t\tContinue ? ");
scanf(" %c",&choice);
}
getch();
}
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
if(end==NULL) //check if last
{
end=tmp; //point start at tmp
tmp->addr_next=end;
}
else //not 1st element
{
tmp->addr_next=end->addr_next;
end->addr_next=tmp;
end=tmp;
}
}
void display()
{
if(end==NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=end->addr_next;
while(q!=end) //go to end of linked list
{
printf("| %d | %d | --> ", q->data,q->addr_next);
q=q->addr_next;
}
printf("| %d | %d | ",end->data,end->addr_next);
printf("\n\n");
}
}

No comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls