![]() |
|
#1
|
|||
|
|||
Link List In CHello respective expert programmer,
i truly a noob in programming. I have create a program that use linked list data structures but haven't successful. Basically, my idea is like this, let the user choose from the menu and insert linked what ever they like until they have satisfied.After that, choose again from the menu to display the list or to something else but i think my list has scope problem where in display the list become NULL again. My malloc memory allocation is done at insert function and display function is in another function. Therefore, the list become NULL in display. How to solve this problem ? I try to use global variable in some cases. Please help me to solve this problem. Below is my program: CPP / C++ / C Code:
Thanks for your help. EDIT: What wrong with this code ? CPP / C++ / C Code:
Your help is greatly appreciated by me and others. Last edited by LuciWiz : 07-Nov-2007 at 04:09.
Reason: Please insert your C++ code between [cpp] & [/cpp] tags
|
|
#2
|
|||
|
|||
Re: Link List In CThe edit section is all in main.
Thanks. |
|
#3
|
|||
|
|||
Re: Link List In COne of the biggest problem i can see is the pointer "head" is floating.
u malloc "head" in the beginnint which gives you an address to a memory location. CPP / C++ / C Code:
You THE assign it to point to another address which the global pointer "list" is pointing. CPP / C++ / C Code:
AND inside your function CPP / C++ / C Code:
list is already your argument for your list!!!!!!! Why are you assigning it to another memory location. I'll try and work a simple Linked List and post in a later post. Hope this helps!! |
|
#4
|
|||
|
|||
Re: Link List In COne more thing that you may think about changing is that you have your linked-list code embedded with your input/output statements. This is very bad for creating reusable code which, especially with a linked list, should be a goal. This way you could put the same linked list into a GUI program or a non-DOS command line.
|
|
#5
|
|||
|
|||
Re: Link List In CHere's a good start for you:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
Re: Link List In CI wonder why you create another struct called Link_list.
I create the first list called list and second also called list. This is my purpose. How about my first program ? The head is dangling but i have point it to first list in the next few statement. Why it is still NULL ? Thanks for your help. |
|
#7
|
|||
|
|||
Re: Link List In CThe reason for the second struct is for future functionality. If I were to extend this to be a doubly-linked list, my Link_List structure would also contain a Tail member.
I don't know what your code is trying to do in InsertBehind(), but if you pass a node * to it, then you should not malloc() within it. Did you see the way that I implemented LL_Append()? That should give you a little help on how you should implement InsertBehind(). Have you separated your linked list logic from the I/O yet? Go ahead and post your most recent code. |
|
#8
|
|||
|
|||
Re: Link List In CAs far as i know, the struct node to represent a single node and struc linked list is to represent a whole linked list.
Am i correct ? The head pointer inside struct LList is pointing to first node. Thanks for your help. |
|
#9
|
|||
|
|||
Re: Link List In CBefore this, i study myself, my semester has started few weeks ago for data structures.
CPP / C++ / C Code:
CPP / C++ / C Code:
I cannot debug this program. As far as i know, char ** is same as char *[] but char *[] is limited to constant solution. I ask user to enter number of node and malloc it by dynamicNode = malloc(sizeof(struct node *) * numberNode); I don't know whether i correct or not. Please help me. Thanks for your help. |
|
#10
|
|||
|
|||
Re: Link List In CQuote:
For one thing your program does not compile. I tried to compile (me using gcc) and got : test.c: In function ‘InsertBehind’: test.c:134: error: invalid lvalue in assignment (line number not same as in your posting) This refers to: CPP / C++ / C Code:
The problem is that head is (struct node *) type and dynamicNode is (struct node **) Solutions are many: 1. maybe you should change the type of "type" 2. maybe you should change the type of "dynamicNode" 3. maybe you could write something like "type = *(dynamicNode+offset);" 4. most likely re-arrange logic and datatypes in many places. Everything depends on what each and every variable exists for in the first place. e.g. What kind of creature is dynamicNode supposed to be ? Quote:
Notice that you just allocated memory for specific amount of pointers. Each pointer will still be undefined.. !! But then again normal memory management concept of linked lists do not allocate memory this way. What is it that you try to do ? |
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| game problem using linkedListType.h and random function | eureka360 | C++ Forum | 5 | 16-Feb-2007 10:48 |
| C++ class -- Please help | vnca_1 | C++ Forum | 3 | 14-Jun-2006 12:31 |
| [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 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The