| devonrevenge (668) | |||
|
im new to the keyword new, i got the impression there would be an easier more dynamic way of creating new nodes and linking them, while trying to create my own linked list i feel i have just created a struct then pointer to pointer struct and so on rather than little nodes of info. either way somethings missing in there(thas what my mummah always said )
| |||
|
Last edited on
|
|||
| devonrevenge (668) | |||
|
please tell me i havnt been struggling to grasp this, its the most simple example of a linnked list isnt it. is there a more elegant way to link the lists and initialize the ints or do i go;
am i grasping it? where do i go from here?? | |||
|
|
|||
| Aceix (455) | |||
|
Yeah this is a simple example of a linked list but i think you linked it the wrong way. Try:
HTH, Aceix. | |||
|
|
|||
| Aceix (455) | |
|
Also, in your second post, It is right to do: node *base; base = new node;It assigns memory to the new node pointer. Thanks, Aceix. | |
|
|
|
| devonrevenge (668) | |||
|
oh yeah is backwards strange, why does this mixed up naive Frankencode not work? the debugger thinksnode is no longer a pointer!?
| |||
|
Last edited on
|
|||
| devonrevenge (668) | |||
this made my compiler flip out...makes sense to me, why it not work?
| |||
|
Last edited on
|
|||
| devonrevenge (668) | |
| lasthing should be base | |
|
|
|
| devonrevenge (668) | |
| oh thank you devonrevenge you are so amazing could you help me further with linked lists, how could i dynamicly create nodes and iterate through them, the internet doesnt realy explain in a way i understand. | |
|
|
|
| devonrevenge (668) | |||
what about this, why cant i have instances of base?? and why does it compile??
| |||
|
|
|||
| fun2code (1063) | |||||
|
OK. It seems like you are getting close to seeing what the link member is for. You have it working in your 4th previous post right? The one that starts with:
Your observation that all lastthings should be bases was spot on, but you only changed it where the assignments are made, not where the cout-ing is being done. You have asked:
Doing this would be the logical next step. I'll try guiding you here. Let's start with the simplest function I can think of for adding nodes. Lets call this function push_front because we will be using it to push new nodes onto the front of the list. Each new node becomes the first in the list. The node which was first becomes second (ie newFirst->link = oldFirst) and so on. Here's some code:
The only thing new to you here should be the push_front(). Play around with this code and ask if you don't see how the function works. I'll save a function to iterate through the list and display the words for next time (ie, if we don't get stuck here - at push_front ). | |||||
|
Last edited on
|
|||||
| devonrevenge (668) | |
| thank you so much fun2code, ive been trying soo hard to grasp this my concentration started waning (thas why i wind up chatting on this) fiddling with my compiler then getting distracted looking at the tuts online then getting distracted looking at facebook and th news, i have plenty to get on with now:) | |
|
Last edited on
|
|
| devonrevenge (668) | |
| kay i understood that and played with it, i added num in the struct, so now they have values, ive just started thinking about sorting them. | |
|
|
|
| fun2code (1063) | |
|
Good. Slow down though. Can you iterate through the list from the front (base) to the end in a while loop? How about a displayList() function first? Sorting the list will require this and other skills. Keep it simple. | |
|
|
|
| devonrevenge (668) | |||
Ive made a primitive bubble sorting algorithm function, is there a more elegant way to cycle through the code.
| |||
|
|
|||
| Catfish2 (666) | |||
|
Where's your list class? Create a list class that holds your nodes.Incomplete example:
You can make a decent List data structure if you know how to use destructors and constructors. Hint: Nodes can have them too. | |||
|
|
|||
| devonrevenge (668) | |
| hrmm kay will look | |
|
|
|
| devonrevenge (668) | |||
i created a display list function, but i havnt seen the benefit of a struct being in class yet.
| |||
|
Last edited on
|
|||
| devonrevenge (668) | |
|
i see i have to create the list in the class so i can manage it from there... | |
|
Last edited on
|
|
| Catfish2 (666) | |
|
I will give you a bad analogy. A list is a car, a node is its engine. What you do now is, you use the engine without a car! Put the engine in a car. Put the nodes into a list. Then you can write the add_node() and display() functions internally to the list. Otherwise you might as well be using C instead of C++. And then you can also use a destructor, so that you don't have memory leaks (for every new there must be a delete). You work a bit more to code your list but you work less to actually use it. | |
|
|
|
| fun2code (1063) | |||
|
Good job on the displayList(). It will work. There is 1 obvious problem with it though, it works for lists of 6 nodes only. I suppose you could correct for this by passing the # of nodes to the function, but you shouldn't be relying on that. There is a special condition which applies only at the end of the list. What is the value of next for the last node in the list? ie. If last = pointer to the last node in the list, what is: last->next = ? It's NULL !! So you want to keep going until you get to the node with next == NULL OK? As in:
| |||
|
|
|||