Tuesday 2 May 2017

A simple C program for traversal of a linked list

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

struct node
{
int data;
struct node *next;
};

// This function prints contents of linked list starting from
// the given node
void printList(struct node *n)
{
while (n != NULL)
{
    printf(" %d ", n->data);
    n = n->next;
}
}

int main()
{
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;

// allocate 3 nodes in the heap
head = (struct node*)malloc(sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node*)malloc(sizeof(struct node));

head->data = 181; //assign data in first node
head->next = second; // Link first node with second

second->data = 772; //assign data to second node
second->next = third;

third->data = 389; //assign data to third node
third->next = NULL;

printList(head);

return 0;
}


Output:

 181  772  389 

No comments:

Post a Comment