C, Print Linked List of Strings

43,714

Solution 1

There are no stupid questions1. Here's some pseudo-code to get you started:

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

printAll (head)

That's it really, just start at the head node, printing out the payload and moving to the next node in the list.

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


1 Well, actually, there probably are, but this isn't one of them :-)

Solution 2

You can use a pointer to iterate through the link list. Pseudo code:

tempPointer = head

while(tempPointer not null) {
  print tempPointer->value;
  tempPointer = tempPointer->next;
}

Solution 3

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)
}
Share:
43,714
JC Leyba
Author by

JC Leyba

I like computers, but I don't like them THAT much. That's why I'm here, to get help when things don't work out so well for me, or to help when I can. If my answers have helped you out, maybe drop by my website and have a look around? printf("Wazzup! \n");

Updated on May 22, 2020

Comments

  • JC Leyba
    JC Leyba about 4 years

    I have to write a C program that uses a linked list. I have created a list and added elements to the list. But I don't know how to print all the elements in the list. The list is a list of strings. I figured I'd somehow increment through the list, printing every string that's there, but I can't figure out a way to do this.

    Short: How to I print a linked list?