XIIB-CS(LINK LIST - Create a list, ADD ELEMENTS AT BEGINNING/END & Display it)
.................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.............................................
...PROGRAM 1....... ADD ELEMENT BEFORE THE FIRST ELEMENT(BEGINNING)................
#include<conio.h>
#include<iostream.h>
struct node {
int data;
node * next;
}* start=NULL,* end,* p,*newptr,*tmp;
node * create(int d)
{ p=new node;
p->data=d;
p->next=NULL;
return p;
}
void insertend(node * p)
{
end->next=p;
end=p;
if(start==NULL) start=p;
}
void insertbeg(node * p)
{
if(start==NULL)
start=p;
else
p->next=start;
start=p;
}
void display(node * p)
{
while(p)
{ cout<<p->data<<" -> "; p=p->next;
getch();
}
}
void main()
{
char ch='y';int d;
while(ch=='y')
{
cout<<"\n Enter a data : ";
cin>>d;
newptr= create(d);
insertbeg(newptr);
display(start);
cout<<"\n press y to continue .....";
cin>>ch;
}
getch();
}
.................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.............................................
................PROGRAM 2............ ADD ELEMENT AFTER THE LAST ELEMENT(END).....
#include<conio.h>
#include<iostream.h>
struct node {
int data;
node * next;
}* start=NULL,* end,* p,*newptr,*tmp;
node * create(int d)
{ p=new node;
p->data=d;
p->next=NULL;
return p;
}
void insertend(node * p)
{ if( start== NULL)
{
end=start=p;
}
else
{
end->next=p;
end=p;
}
}
void insertbeg(node * p)
{
if(start==NULL)
start=p;
else
p->next=start;
start=p;
}
void display(node * p)
{
while(p)
{ cout<<p->data<<" -> "; p=p->next;
getch();
}
}
void main()
{
char ch='y';int d;
while(ch=='y')
{
cout<<"\n Enter a data : ";
cin>>d;
newptr= create(d);
insertend(newptr);
display(start);
cout<<"\n press y to continue ";
cin>>ch;
}
getch();
}
.............................................................................................................................................
...PROGRAM 1....... ADD ELEMENT BEFORE THE FIRST ELEMENT(BEGINNING)................
#include<conio.h>
#include<iostream.h>
struct node {
int data;
node * next;
}* start=NULL,* end,* p,*newptr,*tmp;
node * create(int d)
{ p=new node;
p->data=d;
p->next=NULL;
return p;
}
void insertend(node * p)
{
end->next=p;
end=p;
if(start==NULL) start=p;
}
void insertbeg(node * p)
{
if(start==NULL)
start=p;
else
p->next=start;
start=p;
}
void display(node * p)
{
while(p)
{ cout<<p->data<<" -> "; p=p->next;
getch();
}
}
void main()
{
char ch='y';int d;
while(ch=='y')
{
cout<<"\n Enter a data : ";
cin>>d;
newptr= create(d);
insertbeg(newptr);
display(start);
cout<<"\n press y to continue .....";
cin>>ch;
}
getch();
}
.................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.............................................
................PROGRAM 2............ ADD ELEMENT AFTER THE LAST ELEMENT(END).....
#include<conio.h>
#include<iostream.h>
struct node {
int data;
node * next;
}* start=NULL,* end,* p,*newptr,*tmp;
node * create(int d)
{ p=new node;
p->data=d;
p->next=NULL;
return p;
}
void insertend(node * p)
{ if( start== NULL)
{
end=start=p;
}
else
{
end->next=p;
end=p;
}
}
void insertbeg(node * p)
{
if(start==NULL)
start=p;
else
p->next=start;
start=p;
}
void display(node * p)
{
while(p)
{ cout<<p->data<<" -> "; p=p->next;
getch();
}
}
void main()
{
char ch='y';int d;
while(ch=='y')
{
cout<<"\n Enter a data : ";
cin>>d;
newptr= create(d);
insertend(newptr);
display(start);
cout<<"\n press y to continue ";
cin>>ch;
}
getch();
}
.............................................................................................................................................