A program to Create a Link List.

 

#include<iostream.h>
#include<conio.h>

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

node *start,*x,*y;

void create()
{
if(start==NULL)
{
start=new node;
cout<<"\nEnter data: ";
cin>>start->data;
start->link=NULL;
x=start;
}
else
{
y=new node;
cout<<"\nEnter data: ";
cin>>y->data;
y->link=NULL;
x->link=y;
x=y;
}
}

void display()
{
cout<<"\nLink list as follows: ";
y=start;
while(y!=NULL)
{
cout<<y->data<<" ";
y=y->link;
}
cout<<endl;
}

void main()
{
 clrscr();

 start=NULL;

 int ch;

 do
  {
   cout<<"1. Create a node."<<endl;
   cout<<"2. Display Link List."<<endl;
   cout<<"3. Exit."<<endl;
   cout<<"Enter your choice: ";
   cin>>ch;

   switch(ch)
    {
     case 1: {
                  create();
                  break;
                 }
     case 2: {
                  display();
                  break;
                 }
     case 3: {
                  break;
                 }
     default: {
                  cout<<"\nWrong choice entered.\n";
                 }
     }
    cout<<endl;
   }
 while(ch!=3);
}

 

< Back