Showing posts with label Linked List Insertion. Show all posts
Showing posts with label Linked List Insertion. Show all posts

Tuesday, August 21, 2012

Inserting an element into Linked List in C Programming Language


#include<stdlib.h>
typedef struct Node
{
        int data;
        struct Node *next;
}node;


void insert(node *pointer, int data)
{
       while(pointer->next!=NULL)
        {
                pointer = pointer -> next;
        }
        pointer->next = (node *)malloc(sizeof(node));
        pointer = pointer->next;
        pointer->data = data;
        pointer->next = NULL;
}

//Printing the elements in linkedlist.
void print(node *pointer)
{
        if(pointer==NULL)
        {
                return;
        }
        printf("%d ",pointer->data);
        print(pointer->next);
}

int main()
{
        node *start,*temp;
        start = (node *)malloc(sizeof(node));
        temp = start;
        temp -> next = NULL;

         // Inserting elements into linked List
          insert(start,2);
          insert(start,3);
          insert(start,4);

        // Printing the elements in linked list
         print(start->next);


}