Chelpneeded
Chelpneeded

Reputation: 11

How to print every element in a linked list except for the last one

I have a linked list where I want to print every element except the last one.

while (node != NULL) 
  { 
     printf(" (%d,%d) ", node->x, node->y); 
     node = node->next; 
  } 

This is the loop im using to print the whole thing, but I can't figure out how to make it exclude the last element. I tried replacing 'node != NULL' with 'node->next != NULL' but it didn't work. Any ideas would be greatly appreciated, cheers

Upvotes: 0

Views: 433

Answers (1)

jmq
jmq

Reputation: 1591

while (node != NULL) 
  { 
     if (node->next != NULL)
     {
         printf(" (%d,%d) ", node->x, node->y); 
         node = node->next; 
     }
  } 

Upvotes: 1

Related Questions