Linked List

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

#include<iostream.

h>
#include<iomanip.h>
#include<conio.h>
class list
{
private :
int num,ioc;
struct Node
{
int data;
Node *link;
}*p;
public :

list();
void insend();
void insbeg();
void display();
void del();
int count();

};
list::list()
{
p=NULL;
}
void list::insend()
{
cout<<"\n enter the number :\t";
cin>>num;
Node *q,*r;
if(p==NULL)
{
q =new Node;
q->data=num;
q->link=NULL;
}
else
{
q=p;
while(q->link!=NULL)
q=q->link;
r=new Node;
r->data=num;
r->link=NULL;
q->link=r;
}
}
void list::insbeg()
{
cout<<"\n enter the number :\t";
cin>>num;
Node*q;
q= new Node;

q->data=num;
q->link=p;
p=q;

}
void list::del()
{
cout<<"\n enter the numbers to be delected :\t";
cin>>num;
Node *q,*old;
q=p;
while(q!=NULL)
{
if(q==p)
p=q->link;
else
old->link=q->link;
delete q;
return;
}
cout<<" \n\n elements :\t"<< num <<"\n\n not found ";
}
void list::display()
{
Node *q;
cout<<endl;
for(q=p;q!=NULL;q=q->link)
cout<<"\n"<<q->data;
}
int list::count()
{
Node *q;
int count=0;
for(q=p;q!=NULL;q=q->link)
count++;
return(count);
}
void main()
{
list i;
cout<<"\n"<<"\t \t Enter the elements \n";
i.insbeg();
i.insbeg();
i.insend();
i.insend();
i.insend();
i.display();
cout<<"\n"<<"\n number of elements in linked list =\t
"<<i.count();
i.del();
i.del();

i.del();
i.display();
cout<<"\n"<<"\n number of elements in linked list =\t
"<<i.count();
}

You might also like