Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i have to use malloc?

Status
Not open for further replies.

rolfjf

Programmer
Jun 13, 2000
8
BR
I need to create a simple linked list, with 16 elements.
The struct is:

struct processo {
int pid;
struct processo *prox;
};

I need to create a list, with a pointer poiting to the first element, and the elements have the values 0-15.
How do I create the list?
I have to use malloc to allocate all the elements, with a for?
I tried to do this without malloc, and it doesn't work.
When I try to access the first element, it returns not the value 0, that it sould...
Can someone help me with the creation of this list?
thank you.
Rolf

 
Yes, you need to use malloc or one of its friends. To create the list initially, something like this:

#include <stdlib.h>

/* ... */

struct processo *
createList(int pid)
{
struct processo *list=malloc(sizeof *list);

if (list!=NULL) {
list->pid=pid;
list->prox=NULL;
}
return list;
}

To add an item to the back of the list, you just have to iterate through the list to the end and attach the item once the next item is NULL:

int
appendItem(struct processo *list,int pid)
{
struct processo *idx;
int error=0;

/* Create the new item */
struct processo *item=createList(pid);

if (NULL==item) {
error=1; /* couldn't get memory */
} else {
/* Advance to the end of the list */
for (idx=list;idx->prox!=NULL;idx=idx->prox)
; /* Empty loop */
/* Attach the new item */
idx->prox=item;
}
return error;
}

I'll leave it to you put it together so that it matches your specs.

Russ
bobbitts@hotmail.com
 
Ofcourse to create Linked list u'll have to use malloc;

struct emp {
char *emp;
int age;
struct *next_record;
}
//Adding Records
main()
{
continue = 'y';
struct emp *start, // for keeping track of BOF
*current; //for keeping track of current record referred.

*start = (struct emp *) malloc(sizeof(struct emp));
current = start;

while(current != NULL)
{
//accept data

printf(&quot;Want to continue >>&quot;);
continue = getche();
if(continue == 'Y' || continue == 'y')
{
current->next = (struct emp*) malloc(sizeof(struct emp));
current = current->next;
}
else
{
current->next = null;
current = current->next;
}
}


I hope the code is not very tough to understand

Regards,
SwapSawe.
 
It doesn't compile:

$ gcc -W -Wall foo.c
foo.c:4: parse error before `*'
foo.c:4: warning: no semicolon at end of struct or union
foo.c:8: warning: return-type defaults to `int'
foo.c: In function `main':
foo.c:9: parse error before `='
foo.c:13: dereferencing pointer to incomplete type
foo.c:13: warning: implicit declaration of function `malloc'
foo.c:13: sizeof applied to an incomplete type
foo.c:16: `NULL' undeclared (first use in this function)
foo.c:16: (Each undeclared identifier is reported only once
foo.c:16: for each function it appears in.)
foo.c:20: warning: implicit declaration of function `printf'
foo.c:21: parse error before `='
foo.c:22: parse error before `continue'
foo.c:25: dereferencing pointer to incomplete type
foo.c:27: parse error before `else'
foo.c:30: dereferencing pointer to incomplete type
foo.c:31: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:32: parse error before `}'


Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top