C, Print Related String List

I need to write a C program that uses a linked list. I created a list and added items to the list. But I do not know how to print all the elements in the list. A list is a list of strings. I decided that somehow I am increasing the number in the list by printing each line, but I cannot figure out how to do this.

Short: How to print a linked list ?

+3
source share
4 answers

No silly questions 1 . Here are a few pseudo codes to get you started:

 def printAll (node): while node is not null: print node->payload node = node->next printAll (head) 

To make this true, just start from the head of the node, print the payload and move on to the next node in the list.

Once the next node is the end of the list, stop.


1 Well, actually, probably there is, but this is not one of them :-)

+10
source

You can use the pointer to iterate over the list of links. Pseudocode:

 tempPointer = head while(tempPointer not null) { print tempPointer->value; tempPointer = tempPointer->next; } 
+3
source

pseudo code:

 struct list { type value; struct list* pNext; } void function() { struct list L; // .. element to L // Iterate each node and print struct list* node = &L; do { print(node->value) node = node->next; } while(node != NULL) } 
+1
source

I'm not quite sure that this is what you are looking for, but usually you store in your DS, pHead (that is, a pointer to the first element) and implement a function that fetches the next address of the string - node.

You do this until the next NULL address (this means that you have reached your tail).

0
source

All Articles