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.
hi friends am new to this group. i encountered a program on linked list in the book "Mastreing Algorithms in C" by Kyle Loudon. i didnt know how to create the main program for it. please help me. i have pasted the program here for ur reference
But I don't think list_destroy has been written correctly coz I am getting a seg fault if I put list_destroy(new) in the code...
Let me check as to what is wrong with those functions.
You aren't destroying the whole list I suppose only data is getting freed...i.e,
i didnt understand u said abt list_destroy. but pls look at the code given below for list_destroy. my question is how to call this function in the main program.
hi if u r free pls try my program. only the main program is present here. the other fnctions are above in the previuos message i have sent under /*list.c*/
note: pls dont try the destroy list option under menu as it is not working(while running the program).
/*--------------------------------------------------main-----------------------------------------*/
main()
{
int num,pos,i,j,size,choice, check;
char option;
List *new;
ListElmt *elm,*buffer;
new =malloc(sizeof(ListElmt));
list_init(new,data_destroy);
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(ListElmt));
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);
}
elm=new->head;
goto start;
case 2:
printf("Enter the new element : ");
scanf("%d",&num);
printf("Insert the new element after : ");
scanf("%d", &pos);
elm=new->head;
size=list_size(new);
for(i=0;i<size;i++)
{
if(list_data(elm)==pos)
{
buffer=list_next(elm);
list_ins_next(new,elm,num);
break;
}
elm=list_next(elm);
}
goto start;
case 3:
printf("Delete the element present after: ");
scanf("%d", &pos);
num=0; 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_rem_next(new,elm,&num);
break;
}
elm=list_next(elm);
}
if(check==1)
{
printf("The element is deleted.\n");
goto start;
}
else
{
printf("The element you entered is not in the list.\n");
goto start;
}
case 4:
printf("The current list is :\n");
display(new);
printf("\n");
goto start;
case 5:
printf("Destroy the list? :");
dele: scanf("%s", &option);
if(strcmp(&option, "no")==0)
goto start;
else
if(strcmp(&option, "yes")==0)
{
printf("HI");
list_destroy(new);
printf("The list is destroyed\n");
goto start;
}
else
{
printf("No such option. Enter either yes or no : ");
goto dele;
}
Run the new program now and check the output...Only test1 will be printed and then seg.fault .
Which means the problem is with the line in data_destroy function .
while running the program, when i select destroy list option it is coming as:
****************MENU**************
1. Initialize and create a new list
2. Insert an element in the list
3. Delete an element from the list
4. Display the list
5. Destroy the list
6. Exit
Enter your choice : 5
Destroy the list? (enter yes/no):yes
Segmentation fault
problem doesnt lie in the data_destry loop . i think it lies under case 5 of main program or in the list_destroy function. please help me.
ur suggestions r nice but i dont know how to use regarding [CODE] blah blah blah...[\CODE]
hi friend. i tried as u told. but it still doesnt work. i alos a sample print statement before calling list_destroy(new) in the main function. even that print statement is not executed. please check whts the problem. the list_destroy(List *list) cannot be changed coz it should be as in the book. please help.
also please send me more details or tell me what books can be reffered for CODE.
i have a doubt that whether the program i presented will work in the windiws platform. if ur reply is no, will u tell me the reason(or) will u tell me the differences between the c compliler for windows and linux.
Originally posted by bprasanth_20 Even that print statement is not executed. please
Well the first print gets printed i.e, test1 gets printed , test2 doesn't get printed.So, I feel the problem is with the free function
free(data);
I am not sure as to how it can be done without changing those functions.I'll however give it an another try today.
Abt, compilation on windows I never actually did any programming on windows . I dun't think u'll have problems coz these are the still the basics of programming..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.