linked list question

Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

linked list question

 
0
  #1
Apr 4th, 2006
I wrote a program up with linked lists that creates a circular list containing 9 nodes. Each node contains a number (ranging from 1-9) as an item.

My question is...in the while loops, how does

  1. x = x->next

cause the pointer to move across the list?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define FLAG 1
  5.  
  6. typedef struct node* link;
  7.  
  8. struct node {
  9. int item;
  10. link next;
  11. }; // node containing an item and a link
  12.  
  13. int main(void)
  14. {
  15. int i = 2;
  16. int cnt = 1;
  17. int N = 9;
  18. int num;
  19.  
  20. link t = (link)malloc(sizeof *t);
  21. link x = t;
  22.  
  23. t->item = 1;
  24. t->next = t;
  25.  
  26. for (;i <= N; i++)
  27. {
  28. x = (x->next = (link)malloc(sizeof *x));
  29. x->item = i;
  30. x->next = t;
  31. }
  32.  
  33. puts("Enter a number between 1-9");
  34. scanf("%d", &num);
  35. while (x->item != num)
  36. x = x->next;
  37.  
  38. t = x;
  39. while (FLAG == 1)
  40. {
  41. if (t == x->next)
  42. {
  43. printf("\nTotal # of %d nodes\n", cnt);
  44. break;
  45. }
  46. x = x->next;
  47. printf("%d ", x->item);
  48. cnt++;
  49. }
  50.  
  51. return 0;
  52. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 217
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 22
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz in Training

Re: linked list question

 
0
  #2
Apr 5th, 2006
I think the theory behind using while loops in linked list traversal.. goes something like this:


1. check the head pointer... if(head_ptr) then x = head_ptr ;
2. now.. assign the head_ptr to x, x = head_ptr;
3. x now points to the first node of the list.
4. traverse the list, while(x){ x = x->next; };

by continously re-assigning 'x' with the address of the next node.. you can traverse the list as desired.. until you find what you are looking for.. or until you have displayed all nodes.. or whatever you are wanting to do.

Also, by using while(x) you know the loop is going to execute until x = NULL.. which (at least in a singly linked list) emphasizes the importance of keeping the last node pointing to NULL. This might be a problem with a circularly linked list though.. because your last node will probably hold the address of the head node (thus the circular nature of the linked list) With this in mind, you would probably want to lean towards using a for(;;) loop instead.. and traverse the list until node_count < max_number_of_nodes ; for example.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: linked list question

 
0
  #3
Apr 5th, 2006
when we write
  1. while(x=x->next)
what happens
x starts pointing to what x->next was pointing
after that it is checked wheather while(x) is a true conition , it is true if x is not pointing to null, this is how it traverse the list.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: linked list question

 
0
  #4
Apr 5th, 2006
I think I addressed the question incorrectly and I failed to mention that this was a circular linked list.

After the list is generated (a circular list with 9 numbers) and there is a link between each node in the list, how does

  1. x = x->next;

know where to point to next? Can we just assume that x->next points to the next node in the list? I really don't "see" where it is pointing.

In the very first for loop, it was made clear with

  1. x=x->next=(malloc(sizeof(*x));

which directs x and x->next to point to a new node. Also,

  1. x->next = t;

in the same loop, causes x->next to point to where t is pointing.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: linked list question

 
0
  #5
Apr 5th, 2006
Each address for each node in the circle is (usually) obtained from malloc() or new or some similar process. Each node contains the address of the next node in the circle.

If "a points to b points to c points to a" it doesn't matter where the circle starts or stops you're only going to have the same three addresses in the same sequence-- a can only point to b and b can only point to c and c can only point to a. You do need to arbitrarily have some access into the circle but once you have a single address you can get all the others until you get back to the address you started with.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: linked list question

 
0
  #6
Apr 5th, 2006
So, after each node is created and linked with other nodes, the links in each node will be pointing to the next one no matter what?

I stepped through the debugger after reading your post and each node had a link (in this case named next) that led to the next node so erm I think it makes sense. :cry:
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: linked list question

 
0
  #7
Apr 7th, 2006
That's right. Typically, a linked list is presented pictorally as a box with an arrow coming out of it pointing to another box with an arrow pointing out of it, which in turn points to yet another box.

The box is the struct or class that contains the pointer to the address of the next box. These boxes may contain other information as well.

So, once you create a node, it has a 'next' pointer (or whatever you've called it) that you direct to point to the address in memory of the next node. Then, if x is a pointer to the first node, x->next points to the second.
Typically, you should do this in the form of
  1. x->next = new <structname>;
Which creates the next node and hooks it into the list directly after the node pointed to by x.

If you use the line
  1. x=x->next;
You move the pointer x to refer to the second node, and now, x->next refers to the third.

You can also call something like x->next->next->next, which refers to the third node in the list after the node pointed to by x.
Explainer of control logic and some basics.
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: linked list question

 
0
  #8
Apr 7th, 2006
Message deleted.
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: linked list question

 
0
  #9
Apr 7th, 2006
In response to the deleted message:
True enough. You don't have to do node creation using
  1. x->next = new <structname>;

I just prefer to do so here as an example, though my statement is a little misleading. You can create a new node without attaching it to an existing linked list, but I like to make sure that I'm not losing the pointers by implementing it this way to begin with.

Of course, for node insertion I'd do things slightly differently, but I'd still be using x->next = new struct in most cases.
Explainer of control logic and some basics.
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: linked list question

 
0
  #10
Apr 7th, 2006
I agree that any node added to a list is likely to originate as a result of a call to the new operator (assuming C++ instead of C) and I agree that any node added to the list will eventually be added by assignment to an existing pointer (except of course the initial node added to a list). I initially had interpreted the node->next = new <structname> line to mean always adding a new node to the end of a list when I realized it could be interpretted more generally, so I "deleted" the message. I have to admit that I haven't seen the process of assigning a new address directly to a list before. I have always seen a new address assigned to a temp holding node, then initializing the temp holding node member(s) with the desired value(s) and then adding the temp holding node in the desired location of the list; but that may be more my lack of worldly sophistication than any feature of reality. Sorry about the confusion.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC