473,503 Members | 6,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linked list of structures

Are there any problems with creating a linked list of structures in
the following sample? Or is the preferred way to create the data
structure then create a seperate "node" style structure to hold the
data structure and then create a list of the nodes?

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

struct _listitem {

int a;
int b;
int c;
int d;
int e;
struct _listitem *next;

};

struct _list {
struct _listitem *head;
};

typedef struct _listitem ListItem;
typedef struct _list List;

void Push(List *, ListItem **);
void Destroy(List *);

int main() {

List l;
l.head = 0;

for(int i = 0; i < 10; i++) {

ListItem *newItem;
newItem = (ListItem *)malloc(sizeof(ListItem));

newItem->a = i+1;
newItem->b = i+2;
newItem->c = i+3;
newItem->d = i+4;
newItem->e = i+5;

Push(&l, &newItem);

}

ListItem *tmp;

tmp = l.head;

while(tmp->next) {
tmp = tmp->next;
printf("%i,%i,%i,%i,%i\n",tmp->a,tmp->b,tmp->c,tmp->d,tmp->e);
}

Destroy(&l);

free(&l);

return 0;
}
void Push(List *l, ListItem **n) {

(*n)->next = l->head;
l->head = *n;

}

void Destroy(List *l) {

ListItem *ptr1, *ptr2;

if(!l->head) return;

ptr1 = l->head;
while(ptr1) {
ptr2 = ptr1;
ptr1 = ptr1->next;
free(ptr2);
}

l->head = 0;

}
Nov 13 '05 #1
0 2662

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4805
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
3
2950
by: Francis Bell | last post by:
Hello, I'm trying to read data from a file and then insert that into a linked list. The way I have it, the program compiles, however, I'm getting a segmentation fault error message when I run...
5
22959
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
5
6034
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
2597
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
6
4582
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
7
1314
by: Nikos Mitas | last post by:
I have the following structure: struct DIRECTORY{ char nameDir; struct DIRECTORY *parent; int numSubDir; struct DIRECTORY *subDir; };
19
7800
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
2
5605
by: Subodh | last post by:
Hi All, I want to use a C++ API in a static lib that returns a linked List in C# I am planning to P/Invoke to perform the Interop, I would like to know which way will be better to interop a...
8
718
by: dmp | last post by:
What are Linked list? Please somebody show some ready made programs of linked list
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.