Circular linked list

Program to perform operation on circular linked lidt....
Operations:-
1 : Insert element from end
2 :Delete element from rear


#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
struct node
{
int info;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr getnode()
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
p->next=NULL;
return p;
}
void freenode(nodeptr p)
{
free(p);
}
nodeptr ins_end(nodeptr list,int ele)
{
nodeptr p,q;
p=getnode();
p->info=ele;
if(list==NULL)
{
list=p;
list->next=list;
}
else
{
q=list->next;
p->next=q;
list->next=p;
list=p;
}
clrscr();
printf("%d element inserted\n",ele);
return list;
}
void display(nodeptr list)
{
nodeptr p;
if(list==NULL)
{
printf("List empty..!");
return;
}
else
{
printf("\nCIRCULAR LIST ELEMENTS\n----------------------");
for(p=list->next;p!=list;p=p->next)
{
printf("\n%d",p->info);
}
printf("\n%d\n",list->info);
}
}
nodeptr del_f(nodeptr list)
{
nodeptr p,q;
int x;
if(list==NULL)
{
printf("\nInvalid operation..!");
return 0;
}
else
{
p=list->next;
q=p->next;
list->next=q;
x=p->info;
freenode(p);
printf("%d deleted form list..!\n",x);
}
return list;
}
main()
{
int ele,choice;
nodeptr list;
list=NULL;
clrscr();
while(1)
{
printf("\n1 : Insert from the end\n2 : Delete from the front\n3 : Display\n4 : Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element to be inserted :");scanf("%d",&ele);
list=ins_end(list,ele);
break;
case 2:clrscr();
list=del_f(list);
break;
case 3:clrscr();
display(list);
break;
default:exit(0);
break;
}
}
getch();
return 0;
}

Category: ,

1 comments:

Ashwin said...

Thanx alot......