Circular Linked
List (Singly)
#include<conio.h>
#include<alloc.h>
struct list{
int data;
struct list *next;
}*head=NULL,*temp,*node;
void infirst(void), inpos(void), inlast(void), delfront(void), delpos(void), dellast(void), display(void), count(void);
int num,pos;
char con;
void main()
{
printf("\nMain Menu\n1.Insert at first\n2.Insert at specified position\n3.Insert at last\n4.Delete first\n5.Delete specified position\n6.Delete from last\n7.Display\n8.Count number of nodes\n9.Exit\nEnter choice:");
scanf("%d",&num);
switch(num)
{
case 1:
infirst();
break;
case 2:
inpos();
break;
case 3:
inlast();
break;
case 4:
delfront();
break;
case 5:
delpos();
break;
case 6:
dellast();
break;
case 7:
display();
break;
case 8:
count();
break;
case 9:
clrscr();
exit();
default:
clrscr();
puts("\nWRONG INPUT!!");
}
main();
}
void infirst(void)
{
do
{
printf("\nEnter number:");
scanf("%d",&num);
temp=head;
node=(struct list*)malloc(sizeof(struct list));
node->data=num;
node->next=head;
temp
head=temp;
printf("Continue [y/n]?:");
con=getche();
}while(con=='y'||con=='Y');
}
void inpos(void)
{
do
{
num=0;
temp=head;
printf("Enter position to insert at:");
scanf("%d",&pos);
while(temp->next!=NULL)
{
num++;
if(num==pos-1)
break;
temp=temp->next;
}
printf("Enter number:");
scanf("%d",&num);
node=(struct list*)malloc(sizeof(struct list));
node->data=num;
node->next=temp->next;
temp->next=node;
printf("Continue [y/n]?:");
con=getche();
} while(con=='y'||con=='Y');
}
void inlast(void)
{
do
{
printf("\nEnter number:");
scanf("%d",&num);
temp=head;
while(temp->next!=NULL)
temp=temp->next;
node=(struct list *)malloc(sizeof(struct list));
node->data=num;
node->next=NULL;
temp->next=node;
printf("\nContinue [y/n]?:");
con=getche();
} while(con=='y'||con=='Y');
}
void delfront(void)
{
do{
if(head==NULL)
{
clrscr();
printf("List is empty!!");
break;
}
else
{
temp=head;
head=temp->next;
free(temp);
printf("Continue [y/n]?:");
con=getche();
}
} while(con=='y'||con=='Y');
}
void delpos(void)
{
do
{
if(head==NULL)
{
clrscr();
printf("List is empty!!");
break;
}
else
{
num=0;
temp=head;
printf("Enter position:");
scanf("%d",&pos);
while(temp->next!=NULL)
{
num++;
if(num==pos-1)
break;
temp=temp->next;
}
node=temp->next->next;
free(temp->next);
temp->next=node;
printf("Continue [y/n]?:");
con=getche();
}
} while(con=='y'||con=='Y');
}
void dellast(void)
{
do{
if(head==NULL)
{
clrscr();
printf("List is empty!!");
break;
}
else
{
temp=head;
while(temp->next!=NULL)
{
node=temp;
temp=temp->next;
}
node->next=NULL;
free(temp);
printf("Continue [y/n]?:");
con=getche();
}
} while(con=='y'||con=='Y');
}
void display(void)
{
if(head==NULL)
{
clrscr();
printf("List is empty!!");
}
else
{
clrscr();
for(temp=head;temp!=NULL;temp=temp->next)
printf("%d ",temp->data);
}
}
void count(void)
{
num=0;
temp=head;
while(temp!=NULL)
{
num++;
temp=temp->next;
}
clrscr();
printf("Total number of nodes=%d",num);
}
struct list{
int data;
struct list *next;
}*head=NULL,*temp,*node;
void infirst(void), inpos(void), inlast(void), delfront(void), delpos(void), dellast(void), display(void), count(void);
int num,pos;
char con;
void main()
{
printf("\nMain Menu\n1.Insert at first\n2.Insert at specified position\n3.Insert at last\n4.Delete first\n5.Delete specified position\n6.Delete from last\n7.Display\n8.Count number of nodes\n9.Exit\nEnter choice:");
scanf("%d",&num);
switch(num)
{
case 1:
infirst();
break;
case 2:
inpos();
break;
case 3:
inlast();
break;
case 4:
delfront();
break;
case 5:
delpos();
break;
case 6:
dellast();
break;
case 7:
display();
break;
case 8:
count();
break;
case 9:
clrscr();
exit();
default:
clrscr();
puts("\nWRONG INPUT!!");
}
main();
}
void infirst(void)
{
do
{
printf("\nEnter number:");
scanf("%d",&num);
temp=head;
node=(struct list*)malloc(sizeof(struct list));
node->data=num;
node->next=head;
temp
head=temp;
printf("Continue [y/n]?:");
con=getche();
}while(con=='y'||con=='Y');
}
void inpos(void)
{
do
{
num=0;
temp=head;
printf("Enter position to insert at:");
scanf("%d",&pos);
while(temp->next!=NULL)
{
num++;
if(num==pos-1)
break;
temp=temp->next;
}
printf("Enter number:");
scanf("%d",&num);
node=(struct list*)malloc(sizeof(struct list));
node->data=num;
node->next=temp->next;
temp->next=node;
printf("Continue [y/n]?:");
con=getche();
} while(con=='y'||con=='Y');
}
void inlast(void)
{
do
{
printf("\nEnter number:");
scanf("%d",&num);
temp=head;
while(temp->next!=NULL)
temp=temp->next;
node=(struct list *)malloc(sizeof(struct list));
node->data=num;
node->next=NULL;
temp->next=node;
printf("\nContinue [y/n]?:");
con=getche();
} while(con=='y'||con=='Y');
}
void delfront(void)
{
do{
if(head==NULL)
{
clrscr();
printf("List is empty!!");
break;
}
else
{
temp=head;
head=temp->next;
free(temp);
printf("Continue [y/n]?:");
con=getche();
}
} while(con=='y'||con=='Y');
}
void delpos(void)
{
do
{
if(head==NULL)
{
clrscr();
printf("List is empty!!");
break;
}
else
{
num=0;
temp=head;
printf("Enter position:");
scanf("%d",&pos);
while(temp->next!=NULL)
{
num++;
if(num==pos-1)
break;
temp=temp->next;
}
node=temp->next->next;
free(temp->next);
temp->next=node;
printf("Continue [y/n]?:");
con=getche();
}
} while(con=='y'||con=='Y');
}
void dellast(void)
{
do{
if(head==NULL)
{
clrscr();
printf("List is empty!!");
break;
}
else
{
temp=head;
while(temp->next!=NULL)
{
node=temp;
temp=temp->next;
}
node->next=NULL;
free(temp);
printf("Continue [y/n]?:");
con=getche();
}
} while(con=='y'||con=='Y');
}
void display(void)
{
if(head==NULL)
{
clrscr();
printf("List is empty!!");
}
else
{
clrscr();
for(temp=head;temp!=NULL;temp=temp->next)
printf("%d ",temp->data);
}
}
void count(void)
{
num=0;
temp=head;
while(temp!=NULL)
{
num++;
temp=temp->next;
}
clrscr();
printf("Total number of nodes=%d",num);
}
No comments:
Post a Comment