Thursday, 15 March 2012

Programme to create a Link List in c


/*this programme create three nodes in link list then you can
  perform any further action*/
#include<stdio.h>
#include<stdlib.h>

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

struct node* CreateLinkList()
   {
    struct node* head = NULL;
    struct node* second = NULL;
    struct node* third = NULL;
    head = malloc(sizeof(struct node)); // declare 3 nodes in the heap
    second = malloc(sizeof(struct node));
    third = malloc(sizeof(struct node));
       head->data = 10; //  first node
    head->next = second; //  pointer assignment rule
    second->data = 20; //  second node
    second->next = third;
    third->data = 30; //  third link
    third->next = NULL;
   // At this point, the linked list referenced by "head"
   // matches the list in the drawing.
    return head;
}
void main()
 {
   struct node* Mylist = CreateLinkList();
 }//end of main

No comments:

Post a Comment

Compiler for C,C++ and Python {paste your programme to see the output}