Traversing a Linked List - C Data Structure

C examples for Data Structure:Linked List

Description

Traversing a Linked List

Demo Code

#include <stdio.h>
struct Node{//ww  w. j  av a2s  .c  o  m
        int value;
        struct Node *next;
};
int main (void){
    struct Node n1, n2, n3;
    struct Node *list_pointer = &n1;

    n1.value = 1;
    n1.next = &n2;

    n2.value = 2;
    n2.next = &n3;

    n3.value = 3;
    n3.next = (struct Node *) 0;   // mark list end with null pointer

    while ( list_pointer != (struct Node *) 0 ){
        printf ("%i\n", list_pointer->value);
        list_pointer = list_pointer->next;
    }

    return 0;
}

Result


Related Tutorials