How to traverse linked list in c++?

Help in linked list in c programming?

  • im new in linked list and i really dont understand this topic all i know is that in making the first node you need to do this: struct nodetag { int points; struct nodetag pnext; } int main() { struct nodetag *pfirst; pfirst=NULL; } after that i dont know what to do next on how to add another list how to traverse please help me :( or maybe message me [email protected] i really need help

  • Answer:

    You made a mistake. Second member should be of pointer type. This is most important in linked list. Linked lists are used to take care dynamic situations. Main theme is as follows. Next objects or nodes address is stored in the current objects second member. In this fashion we can mainatain any number of nodes and remove them as and when needed. Let me take a simple example to explain. struct nodetag { int n; struct nodetag *pnext; } int main() { struct nodetag *H, A, B, C; A.n=10; B.n=20; C.n=18; A.pnext=&B; B.pnext=&C; C.pnext=0; H=&A; while(H) { printf("%d\n", H->n); H=H->pnext; } } } Consider allocated memories for A, B, C are 2000, 2028, and 2120.After the execution of statements A.n=10; B.n=20; C.n=18; A.pnext=&B; B.pnext=&C; C.pnext=0; A, B, C nodes contains the following. You may verify that B address in A, C address is in B, and vice versa. A(add=2000) B(add=2028) C(add=2120) 10, 2028 20, 2120 17, 0 Now, last while loop is used to traverse the linked list. Do remember after executing H=&A; statement H value becomes 2000. As it is positive, while condition becomes true. So, loop block will be executed once. Thus, 10 we get as output and H value becomes 2028. Again loop condition is true as H value is 2028. Thus, 20 value is printed and H value becomes 2120. Again loop condition is true as H value is 2120. Thus, 17 is printed and H becomes 0. Loop condition now becomes false as H is 0. Thus, we come out of the while loop. Cheers

S_leo at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

It is easier to understand with an example. Let's say you make a list of city names that you visit. And for whatever reason, you want to keep that list in the order you visit, so each new visited city just gets added to the list. Since it is a list, you can jump to the nth place on the list very easily in s/w. You can therefore index through the list directly to the nth city you visited. But you also want the list to be available in alphabetical order. You want the information about alphabetical order to be built into the list so you don't ever have to sort it. A working list would have two fields per entry, one is the name of the city you visited. The other is the list position of the alphabetically next city. This is the "linked" bit, because it is essentially a link to the next alphabetical place. Thus you can go through the list in index order (visit order), or jump around the list in alphabetical order by following each successive link and finding what the city name is when you go there. The first entry on the list has no place name (or you could call it the 0th position), it is just the start of the list. But it does have a link to the alphabetically first city in the list. The cunning part is that when a new city is added onto the list, it takes minimal effort to insert the new entry into the linked part of the list. A program can easily follow the alphabetical links to see at what point a new city should be inserted. Then it only needs to alter the link in the city immediately alphabetically before the new entry (to make it point at the new entry), and the new entry takes on that cities old link. This is actually easier to write in s/w than to explain in words.

Chris Pitchr

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.