LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2003, 07:28 AM   #1
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Rep: Reputation: 15
Exclamation 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
 
Old 10-17-2003, 07:55 AM   #2
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 32
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.
 
Old 10-17-2003, 11:27 AM   #3
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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 ???? Don't hv time to write comments for each line so if you hv any doubts feel free to ask me...

Welcome to LQ

Last edited by SaTaN; 10-17-2003 at 11:30 AM.
 
Old 10-17-2003, 10:33 PM   #4
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
hi satan, ur main code worked. thanks. it is easy to understand ur main code. thanks a lot.
 
Old 10-18-2003, 01:33 AM   #5
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
hi guys will u be kind enoughe to tell me how to destroy a list using the program i sent above
 
Old 10-18-2003, 01:38 AM   #6
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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);
}

Last edited by SaTaN; 10-18-2003 at 01:42 AM.
 
Old 10-18-2003, 01:52 AM   #7
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
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;

}
 
Old 10-18-2003, 01:57 AM   #8
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
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?????????
 
Old 10-18-2003, 02:00 AM   #9
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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);
 
Old 10-18-2003, 02:04 AM   #10
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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...
 
Old 10-18-2003, 02:43 AM   #11
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
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]
 
Old 10-18-2003, 03:13 AM   #12
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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.
 
Old 10-18-2003, 02:30 PM   #13
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
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.
 
Old 10-18-2003, 02:33 PM   #14
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
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.
 
Old 10-19-2003, 01:45 AM   #15
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
concepts a linked list program in c ssg14j Programming 4 08-19-2005 10:10 AM
C++ Linked List question lowpro2k3 Programming 3 06-16-2005 10:15 AM
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM
C program problem on delete duplicated nodes in linked list ! antony_csf Programming 2 10-28-2004 10:42 AM
Winxp linked to Linux Linked to home network OverboardKiller Linux - Networking 2 06-09-2003 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration