ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
main()
{
int num,pos,i,j,size,choice, check;
char option;
List *new;
ListElmt *elm,*buffer;
list_size(new) = 0;
printf("\nIMPLEMENTATION OF A SINGLY - LINKED LIST\n\n");
start: printf("\n****************MENU**************\n");
printf("\n1. Initialize and create a new list");
printf("\n2. Insert an element in the list");
printf("\n3. Delete an element from the list");
printf("\n4. Display the list");
printf("\n5. Destroy the list");
printf("\n6. Exit");
printf("\n\n Enter your choice : ");
scanf("%d", &choice);
printf("\n");
if(choice>=6)
return -1;
if(choice!=1 && list_size(new)==0)
{
printf("Unless you create a list you cannot perform this operation.\n");
goto start;
}
switch(choice)
{
case 1:
if(list_size(new)>0)
{
printf("A list is currently under operation. You have to destroy it before creating a new list. To destroy a list select Destroy the list option in the menu.\n");
goto start;
}
new =malloc(sizeof(List));
list_init(new,data_destroy);
printf("Enter the head element : ");
scanf("%d",&num);
list_ins_next(new, NULL, num);
printf("The elements added next will be included at the tail of list until you enter -1.");
while(1)
{
printf("Enter the element : ");
scanf("%d",&num);
if(num==-1)
break;
list_ins_next(new,list_tail(new),num);
}
goto start;
case 2:
printf("Enter the new element : ");
scanf("%d",&num);
printf("Insert as head? (enter yes/no) : ");
scanf("%s", &option);
if(strcmp(&option, "yes")==0)
{
list_ins_next(new, NULL, num);
printf("The element is inserted as head.\n");
goto start;
}
printf("Insert the new element after : ");
scanf("%d", &pos);
check = 0;
elm=new->head;
size=list_size(new);
for(i=0;i<size;i++)
{
if(list_data(elm)==pos)
{
check = 1;
buffer=list_next(elm);
list_ins_next(new,elm,num);
break;
}
elm=list_next(elm);
}
if(check == 0)
printf("The specified element is not found in the list.\n");
else
printf("The element is inserted as specified.");
goto start;
case 3:
printf("Delete the head element? (enter yes/no) : ");
scanf("%s", &option);
if(strcmp(&option, "yes")==0)
{
list_rem_next(new, NULL, &num);
printf("The head element is deleted.\n");
goto start;
}
printf("\nIMPLEMENTATION OF A DOUBLY - LINKED LIST\n\n");
start: printf("\n****************MENU**************\n");
printf("\n1. Initialize and create a new list");
printf("\n2. Insert an element in the list");
printf("\n3. Delete an element from the list");
printf("\n4. Display the list");
printf("\n5. Destroy the list");
printf("\n6. Exit");
printf("\n\n Enter your choice : ");
scanf("%d", &choice);
printf("\n");
if(choice>=6)
return -1;
if(choice!=1 && dlist_size(new)==0)
{
printf("Unless you create a list you cannot perform this operation.\n");
goto start;
}
switch(choice)
{
case 1:
if(dlist_size(new)>0)
{
printf("A list is currently under operation. You have to destroy it before creating a new list. To destroy a list select Destroy the list option in the menu.\n");
goto start;
}
new =malloc(sizeof(DList));
dlist_init(new,data_destroy);
printf("Enter the head element : ");
scanf("%d",&num);
dlist_ins_prev(new, NULL, num);
printf("The elements added next will be included at the tail of list until you enter -1.");
while(1)
{
printf("Enter the element : ");
scanf("%d",&num);
if(num==-1)
break;
dlist_ins_next(new,dlist_tail(new),num);
}
elm=new->head;
goto start;
case 2:
check = 0;
printf("Enter the new element : ");
scanf("%d",&num);
printf("1. Insert the new element 1/2 (1=after 2=before)\n");
printf(" Enter your choice : ");
inse: scanf("%d", &choice);
if(choice == 1)
printf("Insert %d after : ",num);
else
if(choice == 2)
printf("Insert %d before : ",num);
else
{
printf("No such choice. Enter choice again : ");
goto inse;
if(check == 0)
printf("The specified element is not found in the list.\n");
else
printf("The element is inserted as specified.\n");
goto start;
case 3:
printf("Delete the element : ");
scanf("%d", &num);
check=0;
elm=new->head;
size=dlist_size(new);
for(i=0;i<size;i++)
{
if(dlist_data(elm)==num)
{
check=1;
buffer=dlist_next(elm);
dlist_remove(new,elm,&num);
break;
}
elm=dlist_next(elm);
}
if(check==0)
printf("The specified element is not found in the list.\n");
else
printf("The element is deleted as specified.\n");
goto start;
case 4:
printf("The current list is :\n");
display(new);
printf("\n");
goto start;
case 5:
dlist_destroy(new);
printf("The list is destroyed");
goto start;
printf("\nIMPLEMENTATION OF A CIRCULAR - LINKED LIST\n\n");
start: printf("\n****************MENU**************\n");
printf("\n1. Initialize and create a new list");
printf("\n2. Insert an element in the list");
printf("\n3. Delete an element from the list");
printf("\n4. Display the list");
printf("\n5. Destroy the list");
printf("\n6. Exit");
printf("\n\n Enter your choice : ");
scanf("%d", &choice);
printf("\n");
if(choice>=6)
return -1;
if(choice!=1 && clist_size(new)==0)
{
printf("Unless you create a list you cannot perform this operation.\n");
goto start;
}
switch(choice)
{
case 1:
if(clist_size(new)>0)
{
printf("A list is currently under operation. You have to destroy it before creating a new list. To destroy a list select Destroy the list option in the menu.\n");
goto start;
}
new =malloc(sizeof(CList));
clist_init(new,data_destroy);
printf("Enter the head element : ");
scanf("%d",&num);
clist_ins_next(new, NULL, num);
printf("The elements added next will be included next to the previous element until you enter -1.\n");
elm=new->head;
while(1)
{
printf("Enter the element : ");
scanf("%d",&num);
if(num==-1)
break;
clist_ins_next(new,elm,num);
elm = elm->next;
}
goto start;
case 2:
printf("Enter the new element : ");
scanf("%d",&num);
printf("Insert the new element after : ");
scanf("%d", &pos);
check = 0;
elm=new->head;
size=clist_size(new);
for(i=0;i<size;i++)
{
if(clist_data(elm)==pos)
{
check = 1;
buffer=clist_next(elm);
clist_ins_next(new,elm,num);
break;
}
elm=clist_next(elm);
}
if(check == 0)
printf("The specified element is not found in the list.\n");
else
printf("The element is inserted as specified.");
goto start;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.