You are on page 1of 4

#include<iostream.

h>
#include<conio.h>
struct node{
int num;
node * next;
};
class list{
node* head;
node* current;
int size;
public:
list(){
head=NULL;
current=NULL;
size=0;
}
void addnode(int a){
if(head==NULL){
head=new node;
head->num=a;
head->next=NULL;
current=head;
size++;
}
else
{
current=head;
while(current->next!=NULL){
current=current->next;
}
node* temp;
temp=new node;
temp->num=a;
temp->next=NULL;
current->next=temp;
current=temp;
size++;
}
}
void searchnode(int a){
int c=0;
current=head;
while(current->next!=NULL){
if(current->num==a){
c=1;
break;
}
current=current->next;
}
if(c==1){
cout<<"\n Found:";
}
else
cout<<"Not Found:";
}
void insertatstart(int a){
node* temp;
temp=new node;
temp->num=a;
temp->next=head;
head=temp;
current=head;
size++;
}
void insertatlast(int a){
current=head;
while(current->next!= NULL){
current=current->next;
}
node* temp;
temp=new node;
temp->num=a;
temp->next=NULL;
current->next=temp;
current=temp;
size++;
}
void insertanywhere(int a){
int b;
int c=0;
cout<<"\nEnter the num after which you want to insert:";
cin>>b;
current=head;
while(current->next!=NULL){
if(current->num==b ){
node* temp;
temp=new node;
temp->num=a;
temp->next=current->next;
current->next=temp;
current=temp;
size++;
c=1;
break;
}
current=current->next;
}
if(c==0)
{
cout<<"not found";
}
}
void deletenode(int a){
node* lastcurrent;
int c=0;
current=head;
lastcurrent=head;
while(current->next!=NULL){
if(current->num==a){
c=1;
break;}
lastcurrent=current;
current=current->next;
}
if(c==1){
if(current==head){
head=head->next;
current=head;
delete lastcurrent;
lastcurrent=current;
}
if(current->next==NULL){
lastcurrent->next=NULL;
delete current;
current=head;
}
else{
lastcurrent->next=current->next;
delete current;
current=lastcurrent->next;
}
size--;
}
else
cout<<"Not Found:";
}
void display(){
current=head;
while(current!=NULL){
cout<<"\nData Part:"<<current->num;
current=current->next;
}
}
void getsize(){
cout<<"\nThe Size Is:"<<size;
}};
void main(){
int a,b,c,d,e,f,g,h;
list obj;
cout<<"\nEnter 1st num";
cin>>a;
cout<<"\nEnter 2nd Num";
cin>>b;
cout<<"\nEnter 3rd Num";
cin>>c;
obj.addnode(a);
obj.addnode(b);
obj.addnode(c);
obj.display();
cout<<"\nEnter 4rth Num";
cin>>d;
obj.insertatstart(d);
obj.display();
cout<<"\nEnter 5th Num";
cin>>e;
obj.insertatlast(e);
obj.display();
obj.getsize();
cout<<"\nEnter 6th Num";
cin>>f;
obj.insertanywhere(f);
obj.display();
obj.getsize();
cout<<"\nEnter The Number Which You Want To Delete:";
cin>>g;
obj.deletenode(g);
obj.display();
obj.getsize();
cout<<"\nEnter The Number Which You Want To Search:";
cin>>h;
obj.searchnode(h);
}

You might also like