![]() |
|
#1
|
|||
|
|||
Converting array to linear linked listI'm fairly new to programming. I have an assignment to create a structure, then convert it using a seperate function to a linear linked list. I have the structure created, and can fill it with data, but I have no idea how to even start converting it to a linked list. The textbook only has a few pages covering linked lists, and nowhere does it talk about array/structures to lists. I've included what I have so far. Any nudge in the right direction to help me get started would be greatly appreciated.
in list.h CPP / C++ / C Code:
in create_structure.cpp CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
|
You have the right notion in your code that linked list nodes have a pointer to the following node. If it was doubly linked list you would have also a pointer to the previous node.
Good explanation about doubly linked lists here: Doubly-linked List especially the part "Doubly Linked List Explained" is good for you. Doesn't matter if it is about doubly linked list since if you understand it you can without difficulty downgrade it to singly linked list. |
|
#3
|
|||
|
|||
|
Why use the typedef statements if you dont utilize the results?
i.e. CPP / C++ / C Code:
To create the linked list, simply create CPP / C++ / C Code:
assign node to node->next. malloc memory...assign from list[1] etc... The above would be best accomplished in a for loop with the index into list being i. To print the list, node = linked_list; CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
|
Here's what I have so far....
in list.h CPP / C++ / C Code:
in create_structure.cpp CPP / C++ / C Code:
I think it looks OK by the book, but I get: error C2440: '=' : cannot convert from 'void *' to 'LINK'. If i remove the stdlib.h from the header, it says it can't locate malloc. Now I'm just getting frustrated. Can anyone point out what I'm doing wrong? |
|
#5
|
|||
|
|||
|
First, get rid of that LINK typedef. When reading your code, I was wondering why you tried to allocate memory for a non-pointer but then realised that it was actually typedef'ed. This is a very very very bad idea. It makes your code hard to read. Second, the answer to your problem is that you need to cast the result of malloc( ). Here is your code, with some minor corrections:
CPP / C++ / C Code:
in create_structure.cpp CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
|
Thanks, your wisdom has helped immensely.
|
|
#7
|
|||
|
|||
|
Quote:
Quote:
In the above there are problems.
so here is my corrected version: list.h: CPP / C++ / C Code:
create_structure.c: CPP / C++ / C Code:
|
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| linked list error message | Krandygrl00 | C++ Forum | 4 | 22-Jun-2005 14:13 |
| search linked list | itsmecathys | C++ Forum | 20 | 18-Apr-2005 01:34 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The