Tuesday, May 22, 2012

Traversing link list


  • Get pointer of the list head to local ptr, and traverse while ptr is not NULL.
  • At least following are necessary:


#include <stdio.h>
#include <stdlib.h>

typedef struct node {
  int data;
  struct node *next;
  struct node *prev;
} NodeType;

NodeType *listHead=0, *listTail=0;

int listShow(void) {
    NodeType *ptr;
        
        ptr = listHead;

        if(ptr == 0) {
           // print "empty" and return 0;
        } 

        while (ptr) {
               printf("data = %d\n", ptr->data);
               ptr = ptr->next;
        }

    return 0;
}

No comments:

Post a Comment