Showing posts with label Program : Grounded Header Linked List – Create – Insertion. Show all posts
Showing posts with label Program : Grounded Header Linked List – Create – Insertion. Show all posts

Monday 2 April 2012

Program : Grounded Header Linked List – Create – Insertion


Program : Grounded Header Linked List – Create – Insertion

// Insertion in  grounded Header Linked List

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

struct link
{
          int no;
          struct link *next;
};

struct link *node,*start,*pre,*new1;
int i=0;

void create_link(struct link *node);
void display(struct link *node);
void insert(struct link *node);

void main()
{
          clrscr();

          node=(struct link *)malloc(sizeof(struct link));
          create_link(node);

          printf("\n Output\n");

          display(node);

          insert(node);
          printf("\n list after insertion ");
          display(node);

          getch();
}

void create_link(struct link *node)
{
          char ans;

          start->next=NULL;
          node=start;
          node->next= (struct link *)malloc(sizeof(struct link));

          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;
                   printf("\n Enter data for node:-");
                   scanf("%d",&node->no);
                   node->next=NULL;
                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
          printf("\n Total node in header linked list : %d",i);
          node=start;
          node->no=i;// save total no. of node in header node
}

void display(struct link *node)
{
          int count;
          node=start;
          count=node->no;
          node=node->next;
          while(count>0)
          {
                   printf("\n  %d",node->no);
                   node=node->next;
                   count--;
          }
}

void insert(struct link *node)
{
          int node_no=1,insert_no,flag=0,count;

          node=start;
          count=node->no;

          node=node->next;
          pre=start;

          printf("\n Enter position where you want to insert new node:- ");
          scanf("%d",&insert_no);

          while(count>0)
          {
                   if(node_no==insert_no)
                   {
                             new1=(struct link *)malloc(sizeof(struct link));
                             printf("\n Insert data for new node : ");
                             scanf("%d",&new1->no);
                             pre->next=new1;
                             new1->next=node;
                             flag=1;

                             break;
                   }
                   else
                   {
                             pre=pre->next;
                             node=node->next;
                   }
                   node_no++;
                   count--;
          }

          if(flag==0)
          {
                   printf("\n Position not found");
          }
          else
          {
          node=start;
          node->no=node->no+1;
      }
}


Posted By : Ruchita Pandya