3

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?

6
  • 3
    Sounds like your real question is "how do I iterate through a linked list" - is that accurate? Commented Dec 7, 2010 at 2:55
  • 3
    The followup to Jefromi's question is in two parts: 1) Can you print the contents of the first node and 2) can you obtain a link to the second node? Commented Dec 7, 2010 at 2:57
  • 1
    Really depends on the data structure you use for your linked list, can you show us your code so far? Btw, if this is homework, you should tag is as such. Commented Dec 7, 2010 at 2:57
  • 1
    Rolling back the homework tag. I consider it polite to give the OP a chance to either change it themselves or indicate that it's not homework (there have been plenty of cases where that has happened). By all means homework-tag it after a suitable delay, i.e., something more than six minutes :-) OP has indicated they just want a nudge (rather than a solution) so the homework tag is superfluous anyway. Commented Dec 7, 2010 at 3:03
  • "I can't find enough good info on the net to help me with that task" Where you read about LinkedList, there should show you how to access a node, just repeat one by one, basically. Commented Dec 7, 2010 at 3:13

4 Answers 4

13

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 :-)

Sign up to request clarification or add additional context in comments.

Comments

5

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;
}

Comments

1

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)
}

Comments

0

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

You do this until the next address is NULL (which means you've reached your tail).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.