/*menu program to illustrate circular linked list*/
#include<stdio.h>
struct llist{
int info;
struct llist *next;
};
struct llist *start;
void insrtbeg() {
struct llist *nnode,*temp;
nnode=(struct llist *)malloc(sizeof(struct llist));
printf("\nEnter data to insert: ");
scanf("%d",&nnode->info);
if(start==NULL) {
nnode->next=nnode;
start=nnode;
return;
}
temp=start;
while(temp->next!=start)
temp=temp->next;
temp->next=nnode;
nnode->next=start;
start=nnode;
}
void insrtend() {
struct llist *nnode,*temp;
nnode=(struct llist *)malloc(sizeof(struct llist));
printf("\nEnter data to insert: ");
scanf("%d",&nnode->info);
nnode->next=start;
if(start==NULL) {
start=nnode;
return;
}
temp=start;
while(temp->next!=start) {
temp=temp->next;
}
temp->next=nnode;
}
int getcounter() {
struct llist *temp;
int counter;
temp=start;
for(counter=0;temp->next!=start;counter++) {
temp=temp->next;
}
return counter+1;
}
void insrtnth() {
int pos,i;
struct llist *nnode,*temp;
if(start==NULL) {
insrtbeg();
return;
}
printf("\nEnter the position to insert data: ");
scanf("%d",&pos);
if((pos>getcounter()+1)||(pos==0)) {
printf("\nInvalid position!");
return;
}
if(pos==(getcounter()+1)) {
insrtend();
return;
}
if(pos==1) {
insrtbeg();
return;
}
nnode=(struct llist *)malloc(sizeof(struct llist));
printf("\nEnter data to insert: ");
scanf("%d",&nnode->info);
temp=start;
for(i=1;i<pos-1;i++) {
temp=temp->next;
}
nnode->next=temp->next;
temp->next=nnode;
}
void deltfirst() {
struct llist *temp;
if(start==NULL) {
printf("\nList is empty!");
return;
}
printf("\nThe deleted item is: %d",start->info);
temp=start;
while(temp->next!=start)
temp=temp->next;
temp->next=start->next;
start=start->next;
}
void deltlast() {
struct llist *temp;
if(start==NULL) {
printf("\nList is empty!");
return;
}
if(start->next==NULL) {
start=NULL;
return;
}
temp=start;
while(temp->next->next!=start) {
temp=temp->next;
}
printf("\nThe deleted item is: %d",temp->next->info);
temp->next=start;
}
void deltnth() {
int pos,i;
struct llist *temp;
if(start==NULL) {
printf("\nList is empty!");
return;
}
printf("\nEnter the position to delete data: ");
scanf("%d",&pos);
if((pos>getcounter())||(pos==0)) {
printf("\nInvalid position!");
return;
}
if(pos==getcounter()) {
deltlast();
return;
}
if(pos==1) {
deltfirst();
return;
}
temp=start;
for(i=1;i<pos-1;i++) {
temp=temp->next;
}
printf("\nThe deleted item is: %d",temp->next->info);
temp->next=temp->next->next;
}
void traverse() {
struct llist *temp;
if(start==NULL) {
printf("\nList is empty!");
return;
}
printf("\nThe List items are:\n");
temp=start;
while(temp->next!=start) {
printf("%5d",temp->info);
temp=temp->next;
}
printf("%5d",temp->info);
}
void search() {
int s;
struct llist *temp;
if(start==NULL) {
printf("\nList is empty!");
return;
}
temp=start;
printf("\nEnter data to search: ");
scanf("%d",&s);
while(temp->next!=start) {
if(temp->info==s) {
printf("\nThe given data is found in the list");
return;
}
temp=temp->next;
}
if(temp->info==s) {
printf("\nThe given data is found in the list");
return;
}
printf("\nThe given data is not found in the list");
}
void main() {
int a;
start=NULL;
while(1) {
printf("\nMENU\n1.Insert at the beginning\n2.Insert at end
\n3.Insert at the nth position\n4.Delete first node\n5.Delete last node
\n6.Delete nth node\n7.Traverse all nodes\n8.Search any value
\n9.Exit\n\n\tEnter your choice: ");
scanf("%d",&a);
switch(a) {
case 1:
insrtbeg();
break;
case 2:
insrtend();
break;
case 3:
insrtnth();
break;
case 4:
deltfirst();
break;
case 5:
deltlast();
break;
case 6:
deltnth();
break;
case 7:
traverse();
break;
case 8:
search();
break;
case 9:
exit(1);
default:
printf("\nInvalid Choice!\n");
}
}
}
0 comments:
Post a Comment