Saturday, 31 March 2012

Program : Singly linked list - Create


Program  - Create Singly linked list 

// Create Linked List  and traverse node in linked list

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

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

struct link *node,*start;
int i;
void create_link(struct link *node);
void display(struct link *node);

void main()
{
          int ch;
          clrscr();
          node=(struct link *)malloc(sizeof(struct link));
          printf(“\n Create linked list “);
          create_link(node);
          printf("\n Output\n");
          display(node);
          getch();
}
void create_link(struct link *node)
{
          char ans;
          i=0;
          start->next=NULL;
          node=start;
          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++;
          }
}
void display(struct link *node)
{
          node=start->next;
          while(node)
          {
                   printf("\n  %d",node->no);
                   node=node->next;
          }

}

No comments:

Post a Comment