LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   imple,menting a linked list in c program in linux (http://www.linuxquestions.org/questions/programming-9/imple-menting-a-linked-list-in-c-program-in-linux-105080/)

bprasanth_20 10-17-2003 07:28 AM

imple,menting a linked list in c program in linux
 
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

/*list.h*/

#ifndef LIST_H
#define LIST_H

#include<stdlib.h>

typedef struct ListElmt_ {

void *data;
struct ListElmt_ *next;

} ListElmt;

typedef struct List_ {

int size;

int (*match) (const void *key1, const void key2);
void (*destroy) (void *data);

ListElmt *head;
ListElmt *tail;

}List;

void list_init(List *list, void(*destroy) (void *data));

void list_destroy(List *list);

int list_ins_next(List *list, ListElmt *element, const void *data);

int list_rem_next(List *list, ListElmt *element, void **data);

#define list_size(list) ((list)->size)

#define list_head(list) ((list)->head)

#define list_tail(list) ((list)->tail)

#define list_is_head(list,element) ((element) == (list)->head ? 1 : 0)

#define list_is_tail(element) ((element) == NULL ? 1 : 0)

#define list_data(element) ((element)->data)

#define list_next(element) ((element)->next)

#endif



/*list.c*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "list.h"

void data_destroy(void *data)
{
free(data);
}

void list_init(List *list, void (*destroy)(void *data)) {

list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;

return;

}

void list_destroy(List *list) {

void *data;

while (list_size(list) > 0) {

if(list_rem_next(list, NULL, (void **)&data) == 0 && list->destroy != NULL) {

list->destroy(data);

}
}

memset(list, 0, sizeof(List));

return;

}

int list_ins_next(List *list, ListElmt *element, const void *data) {

ListElmt *new_element;

if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL)
return -1;

new_element->data = (void *)data;

if (element == NULL) {

if (list_size(list) == 0)
list->tail = new_element;

new_element->next = list->head;
list->head = new_element;

}
else {

if (element->next == NULL)
list->tail = new_element;

new_element->next = element->next;
element->next = new_element;


}

list->size++;

return 0;

}

int list_rem_next(List *list, ListElmt *element, void **data) {

ListElmt *old_element;

if (list_size(list) == 0)
return -1;

if (element == NULL) {

*data = list->head->data;
old_element = list->head;
list->head = list->head->next;

if (list_size(list) == 1)
list->tail = NULL;

}

else {

if (element->next == NULL)
return -1;

*data = element->next->data;
old_element = element->next;
element->next = element->next->next;

if (element->next == NULL)
list->tail = element;

}

free(old_element);

list->size--;

return 0;

}


void display(List *list)

{

ListElmt *element;

for(element=list_head(list); element!=NULL; element=list_next(element))
printf("->%d",element->data);
}


the problem how to insert a new element aftre a specified element(specified by the user). please help me

UltimaGuy 10-17-2003 07:55 AM

Just type this program in a editor like emacs or vi or kedit, for starters. Then save it and then compile it using

gcc -o list list.c

Now, just type ./list in the console and you have run it.

SaTaN 10-17-2003 11:27 AM

Code:

main()
{
        int num,pos,i,j,size;
        List *new;
        ListElmt *elm,*buffer;
        new =malloc(sizeof(ListElmt));
        list_init(new,data_destroy);
        while(1)
        {
                scanf("%d",&num);
                if(num==-1)
                        break;
                list_ins_next(new,list_tail(new),num);
        }
        elm=new->head;
        display(new);
        printf("\n");
        scanf("%d %d",&pos,&num);
        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);
                        display(new);
                        break;
                }
                elm=list_next(elm);
        }

}

<edit> Am I doing ur homework ???? :tisk: Don't hv time to write comments for each line so if you hv any doubts feel free to ask me...

Welcome to LQ

bprasanth_20 10-17-2003 10:33 PM

hi satan, ur main code worked. thanks. it is easy to understand ur main code. thanks a lot.

bprasanth_20 10-18-2003 01:33 AM

hi guys will u be kind enoughe to tell me how to destroy a list using the program i sent above

SaTaN 10-18-2003 01:38 AM

There is a function in your list.h / list.c
Quote:

void list_destroy(List *list)
I think if you add
list_destroy(new);

before the break; then the list will be destroyed

<edit>

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,
Code:

void data_destroy(void *data)
{
        free(data);
}


bprasanth_20 10-18-2003 01:52 AM

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.

void list_destroy(List *list) {

void *data;

while (list_size(list) > 0) {

if(list_rem_next(list, NULL, (void **)&data) == 0 && list->destroy != NULL) {

list->destroy(data);

}
}

memset(list, 0, sizeof(List));

return;

}

bprasanth_20 10-18-2003 01:57 AM

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;
}

goto start;

}

}

ur comments pls?????????

SaTaN 10-18-2003 02:00 AM

Replace void data_destroy(void *data) function with the following code
Code:

void data_destroy(void *data)
{
        printf("Test1 \n");
        free(data);
        printf("Test2 \n");
}

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 .
Quote:

free(data);

SaTaN 10-18-2003 02:04 AM

Regarding the program you sent....

Nice work . But I don't think using goto is a good option. I never use it. Instead you can use a while loop n break when user wants you to quit.

Enclose your entire code in
[KODE]
ur code.....
blah blah blah
[/KODE]

use CODE not KODE...

bprasanth_20 10-18-2003 02:43 AM

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]

SaTaN 10-18-2003 03:13 AM

Hey did you change the data_destroy function as I had told then u'll know.....
It is /CODE not \CODE
Code:

void data_destroy(void *data)
{
        printf("Test1 \n");
        free(data);
        printf("Test2 \n");
}

This is wat will happen.
Even indentation will be taken care of.

bprasanth_20 10-18-2003 02:30 PM

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.

bprasanth_20 10-18-2003 02:33 PM

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.

SaTaN 10-19-2003 01:45 AM

Quote:

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..


All times are GMT -5. The time now is 06:49 PM.