Singly-Linked Lists: Ultimate

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2005
Posts: 21
Reputation: bsdpowa is an unknown quantity at this point 
Solved Threads: 0
bsdpowa's Avatar
bsdpowa bsdpowa is offline Offline
Newbie Poster

Singly-Linked Lists: Ultimate

 
0
  #1
Aug 11th, 2005
Hello all,
I'd like, very much, to understand linked-lists.I've read many texts on the subject and tried many source codes but none of them seemed to fit my needs (one from daniweb is pretty close but it's in C) so I, as desperate as I can be, beg you for mercy.
I'd like to tell you not to suggest me to look for STL lists or to search the forums or even to buy a book on data structures.I've seen it all and I've got it all, belive me.I wouldn't come here if I'd have another option, but I don't.
I have a C++ exam next month and I understand pretty much everything except these damned linked-lists.Many slepless nights and even more headaches so I've decided to try this forum (since they all refused/were too lazy or there was some unnatural power that forbid them to help me on other forums).

I'm going for C++ singly-linked lists with structures and basic operations (add beginning, add end, display, remove node, remove list).I'll provide some code and I'd like you to continue with adding code and explanation (I'd really appreciate a good and detailed explanation because I need to understand them, not just to get the source code) so some other time maybe others can learn from this thread, that's why I called it Ultimate singly-linked lists.

STARTING
We need a structure with data ofcourse.
  1. struct node
  2. {
  3. char name[10]; //this is a char array where we will store the name user will input
  4. int age; //we are going to store the age here
  5.  
  6. node* p_next; //this is a pointer to the next node
  7. }

Now we make a main and menu functions.I separated these two because when a user will finish his adding/deleting he might want to return to the menu for some more options.
  1. int main()
  2. {
  3. menu(); //we call the menu function
  4. return 0;
  5. }

  1. void menu()
  2. {
  3. node* p_beginning = new node; //we create a new pointer which will point to the beginning of the list, that is first node
  4. p_beginning = NULL; // and we set it to point to nothing at the momment
  5.  
  6. int input;
  7. do
  8. {
  9. cout << "MENU" << endl << endl;
  10. cout << "[1] Add to the beginning" << endl;
  11. cout << "[2] Add to the end" << endl;
  12. cout << "[3] Display" << endl;
  13. cout << "[4] Delete a node" << endl;
  14. cout << "[0] Exit" << endl << endl;
  15. cout << "Option: ";
  16. cin >> input;
  17.  
  18. switch (input)
  19. {
  20. case 1:
  21. {
  22. beginning(); //we call a function where we will add a node to the beginning
  23. }
  24. case 2:
  25. {
  26. }
  27. case 3:
  28. {
  29. }
  30. case 4:
  31. {
  32. }
  33. case 0:
  34. {
  35. return 0;
  36. }
  37. }
  38. } while (input!= 0);
  39. }

That's everything I understand.Now questions:

1.In the menu we created a new pointer called p_beginning which will (in the future) point to the first node or to the beginning of the node but now we set it to point to nothing -> why is that?
2.Is that everything or should we add some more lines to the code (stick to the stuff related to linked-lists)?

OK, can anyone now make similar stuff like I did.
Now our next job is to start adding the nodes, right? At the beginning.Now we will make a function "void beginning()" and I'm sure we'll have to add some parameters to the menu call function beginning().

So...
  1. void beginning()
  2. {
  3. }

It doesen't matter if 15 people reply to this post about the beginning() function, more the better.It's good to see how one would make this by himself.So, can you help?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Singly-Linked Lists: Ultimate

 
0
  #2
Aug 11th, 2005
1. Think of it like this: The pointer to the beginning is the anchor, and what you use it for is to point to the first entry in your singly-linked list (SLL). Until you use it, however, it's a good idea to set it to point to NULL just so you don't accidentally use an uninitalized pointer later on, which could point to an area of memory you don't want it to.

2. If you're using C++, adding nodes is easy.
  1. struct node
  2. {
  3. node *next;//Points to the next node
  4. };

  1. int main()
  2. {
  3. node* head;//Pointer to be used to point to first element
  4. node* trans;//Pointer to currently-viewed node (Optional, but I like it.)
  5. head = NULL;
  6. trans = NULL;
  7.  
  8. //To build the first item, it's like this:
  9. head = new node;
  10. trans = head;
  11. trans->next = NULL; //For safety purposes
  12.  
  13. //To add a node:
  14. trans->next = new node;
  15. trans = trans->next; //Advance to point at new node.
  16. trans->next = NULL; //Now that you're in the new node, set the pointer for safety.
  17.  
  18. //To delete entire list:
  19. trans = head; //Go back to start.
  20. while(trans->next !=NULL) //As long as there's another node...
  21. {
  22. head = trans;
  23. trans=trans->next;
  24. delete head;
  25. head = NULL;
  26. }
  27. //Cleanup last node: head is NULL, trans is at last node.
  28. delete trans;
  29. trans = NULL;
  30. return 0;
  31. }

Does that help?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 21
Reputation: bsdpowa is an unknown quantity at this point 
Solved Threads: 0
bsdpowa's Avatar
bsdpowa bsdpowa is offline Offline
Newbie Poster

Re: Singly-Linked Lists: Ultimate

 
0
  #3
Aug 11th, 2005
I didn't understand that.There isn't any input data so I don't know how would I put data into the list.
If I understood correctly we must first put some data into the list and don't care about it just so we know next time where to start?
I'm sorry but I didn't get a thing what you were saying, that's why I wanted to make SSL together with you because everyone do the lists by their own way and I get even more confused.
Could you try to implement your way into the beginning() function or at least try to explain what you were doing there and why?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Singly-Linked Lists: Ultimate

 
0
  #4
Aug 11th, 2005
S'okay. Let's expand the node I wrote.

  1. struct node
  2. {
  3. int blar[3];
  4. node *next;
  5. }

To put data in:

  1. int main()
  2. {
  3. node* head;//Pointer to be used to point to first element
  4. node* trans; /*Pointer to currently-viewed node (Optional, but I like
  5.  it.)*/
  6. head = NULL;
  7. trans = NULL;
  8. /*Let's make a node! Declare an instance of node with keyword new.
  9.  That allocates the amount of memory needed to hold the struct.*/
  10.  
  11. head = new node;
  12.  
  13. /*The head pointer now refers to the first node in the list, which has
  14. the elements of int blar[3] and node*next.*/
  15.  
  16. trans = head;
  17. /*The second pointer, the TRANSverse, is used to, well, transverse the
  18.  linked list more simply, and aids in cleanup. Think of it like as a
  19. bookmark, to help you keep your place. By assigning it to 'head', the
  20. trans pointer now also points to the first node. Let's do some things to
  21. the array now.*/
  22.  
  23.  
  24. /* To change values in the node, you can use the arrow operator (->)
  25. to access the elements like so:*/
  26. trans->blar[0] = 10;
  27. trans->blar[1] = 23;
  28. trans->blar[2] = -9;
  29. /*These values are now stored in the blar array of the first node.
  30. Notice that it's exactly the same sort of code you're used to with array
  31. assignments. Just remember that you need to use the arrow operator if
  32. you're working with a pointer to a struct, and a dot operator if you're
  33. working with the struct directly (i.e., if you just declared a node called
  34. 'nyaaaw' in main and wanted to work on blar, you'd do nyaaaw.blar[0] =
  35.  15;).*/
  36.  
  37. trans->next = NULL;
  38. /*Generally, you'd do this immediately after creating the node, but I
  39. don't want to rewrite this again. What you're doing here is not only safe
  40.  coding practice (you're not pointing to some random location in memory
  41.  that you might screw up if you make a mistake with your pointers), but
  42.  it's also helpful when the time comes to locate the end of the SLL,
  43. because only the final 'next' pointer will be NULL.*/
  44.  
  45. /*Moving on, make a node on the end of the SLL.
  46. Since we're already pointing trans to the current node, we can declare
  47. another instance of node and hook it to the existing node by using
  48. trans->next, which is equivalent to head->next, because they point to
  49. the same place.*/
  50.  
  51. trans->next = new node;
  52. trans=trans->next;
  53.  
  54. /*trans->next was assigned a new value to a valid piece of memory,
  55. and is no longer null. So, we can tell trans to give itself the reference
  56. for this new piece of memory, moving it along the linked list. At this
  57. point, head and trans are no longer equal, but head->next and trans
  58. ARE equal (that is, they point to the same location in memory).*/
  59.  
  60. /*Remember to be a good clean coder, set the pointer to the next node (which doesn't exist) to NULL.*/
  61. trans->next = NULL.
  62.  
  63. //Poppa's got a brand new blar! Let's fill it.
  64. trans->blar[0]=1;
  65. trans->blar[1]=2;
  66. trans->blar[2]=1000;
  67.  
  68. /*At this point, check the value of trans->blar[2] to the value of
  69. head->next->blar[2]. They are the same, because they point to the
  70. same area in memory.*/
  71.  
  72. /*Make one more node for good measure.*/
  73. trans->next = new node;
  74. trans=trans->next;
  75. trans->next =NULL;
  76. trans->blar[0]=0;
  77. trans->blar[1]=1;
  78. trans->blar[2]=2;
  79. /*Whee! Now, trans->blar[2] is equal to head->next->next->blar[2]!
  80. OMGEUREKAPWNZED!*/
  81.  
  82. /*Now, you can get fancier with this, but let's go through the deletion
  83. process. If you don't clean up after yourself, you create a memory leak,
  84.  which can constrain system resources until reboot if your compiler isn't
  85.  smart enough to pick up after you. Because you keep setting
  86. trans->next to NULL in the last node of the SLL, you have a great way
  87. to tell if you are at the end of the list, so you can automate the
  88. deletion process.*/
  89.  
  90. //To delete entire list:
  91. trans = head; /*Go back to start, trans and head both point to the
  92. first node.*/
  93.  
  94. /*As long as there's another node, run the loop below.*/
  95. while(trans->next !=NULL)
  96.  
  97. {
  98. head = trans; /*Move the head to refrence the node currently referenced by trans8?
  99.   trans=trans->next; /*Move trans to the next node (guaranteed to be valid by the while loop!) */
  100.  
  101. delete head; /*Free the memory that head points to. Head is now
  102. not pointing to valid memory.*/
  103. head = NULL; /*Just to be safe, NULL head. It's not necessary at
  104. all, but I want to make a point that you never let your pointers dangle.*/
  105. }
  106. /*The above loop will systematically delete all nodes except for the
  107. last. The reason for this is that the while loop checks for trans->next to
  108.  be NULL, terminating the loop before the code inside runs. So, there's a
  109.  little more cleanup to do. head is already nulled, so no need to worry
  110. about that.*/
  111.  
  112. //Cleanup last node: head is NULL, trans is at last node.
  113. delete trans; //Free the last piece of memory
  114. trans = NULL; //make trans safe.
  115. return 0; //get the heck outta dodge.
  116. }

Is that clearer? I don't think I can be any more clear without pictures.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 21
Reputation: bsdpowa is an unknown quantity at this point 
Solved Threads: 0
bsdpowa's Avatar
bsdpowa bsdpowa is offline Offline
Newbie Poster

Re: Singly-Linked Lists: Ultimate

 
0
  #5
Aug 11th, 2005
I didn't look at the explanation yet, just wanted to run it first and....what the heck is "test" since it's not initialized? Thank you very much for your time.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: Singly-Linked Lists: Ultimate

 
0
  #6
Aug 11th, 2005
i just wanted to say i loved the line..

/*Whee! Now, trans->blar[2] is equal to head->next->next->blar[2]!
OMGEUREKAPWNZED!*/

thank you for that Drowzee
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Singly-Linked Lists: Ultimate

 
0
  #7
Aug 11th, 2005
Whoops. Test is supposed to be 'trans'. I wrote this all without a compiler, so I didn't catch the typo.

Fixed it.

And I also started to smile at 'OMGEUREKAPWNZD!'. I think I'll start using that on another forum.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 21
Reputation: bsdpowa is an unknown quantity at this point 
Solved Threads: 0
bsdpowa's Avatar
bsdpowa bsdpowa is offline Offline
Newbie Poster

Re: Singly-Linked Lists: Ultimate

 
0
  #8
Aug 11th, 2005
I think I understand, thank you very much.
I'm sure I'll have a question or two tommorow.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC