ALL IN ONE PROGRAM FOR LINKED LIST

#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *next;
}*start=NULL;
int disp();
int insert()
{
node *ptr=new node;
cout<<"\nEnter info part : ";
cin>>ptr->info;
cout<<endl<<"+----------------------+\n| 1 | At end of list   |\n| 2 | At begin of list |\n| 3 | Before a node    |\n+----------------------+\nChoice : ";
int ch1;
cin>>ch1;
switch(ch1){
case 1:
if(start==NULL)
{
start=ptr;
ptr->next=NULL;
}
else
{
node *ntr=new node;
for(ntr=start;ntr->next!=NULL;)
{
ntr=ntr->next;
}
ntr->next=ptr;
ptr->next=NULL;
}
break;
case 2:
if(start==NULL)
{
start=ptr;
ptr->next=NULL;
}
else
{
ptr->next=start;
start=ptr;
}
break;
case 3:
if(start==NULL)
{
cout<<"List is empty";
start=ptr;
ptr->next=NULL;
}
else
{
cout<<"Enter which node : ";
disp();
int x;
cin>>x;
node *ntr=new node;
node *perent=new node;

for(ntr=start;ntr->next!=NULL;ntr=ntr->next)
{
if(ntr->info==x)
{
perent->next=ptr;
ptr->next=ntr;
cout<<"Item inserted : ";
disp();
break;
}
perent=ntr;
}
}
default:
cout<<"Invalid choice!!";
}
}
int del()
{
if(start==NULL)
{
cout<<"List is empty!!\n";
return 0;
}
node *child=new node;
node *parent=new node;
cout<<"\n+------------------+\n| 1 | From Begin   |\n| 2 | From End     |\n| 3 | A given node |\n+------------------+\n choice: ";
int a;
cin>>a;
switch(a)
{
case 1:
start=start->next;
cout<<"Ist node deleted!!\n List : ";
disp();
break;
case 2:
for(child=start;child->next!=NULL;child=child->next)
{
parent=child;
}
parent->next=NULL;
cout<<"Last Node deleted!!\nList :";
disp();
break;
case 3:
cout<<"List : ";
disp();
cout<<"\nWhich one you want to delete: ";
int x;
cin>>x;
for(child=start;child->next!=NULL;child=child->next)
{
if(child->info==x)
{
parent->next=child->next;
cout<<"Given Node is deleted!!\nList : ";
disp();
return 0;
}
parent=child;
}
cout<<x<<"  is not present in list!! Please read list carefully and try again\n";
break;
default:
cout<<"Invalid choice!!Try again!!";
}
cout<<endl;
}
int search()
{
if(start==NULL)
{
cout<<"List is empty!!";
return false;
}
cout<<"Searching for : ";
int x;
cin>>x;
node *ntr=new node;
for(ntr=start;ntr!=NULL;ntr=ntr->next)
{
if(ntr->info==x)
{
cout<<"Item found\n";
return true;
}
}
cout<<"Item node found!!\n";
}
int disp()
{
if(start==NULL)
{
cout<<" empty!!\n";
return 0;
}
node *ntr=new node;
for(ntr=start;ntr!=NULL;ntr=ntr->next)
{
cout<<"  "<<ntr->info;
}
cout<<endl;
}
int main()
{
cout<<"+-----------------------------------------------------------------------------+\n|\t\t\tALL IN ONE PROGRAM FOR LINKED LIST\t\t      |\n|\t\t\t        SMART PROGRAMER ON\t\t\t      |\n+-----------------------------------------------------------------------------+\n";
int ch;
while(1)
{
cout<<"+-------------+\n| 1 | Insert  |\n| 2 | Display |\n| 3 | Delete  |\n| 4 | Search  |\n| 5 | Exit    |\n+-------------+\nChoice : ";
cin>>ch;
switch(ch)
{
case 1:
insert();
cout<<endl;
break;
case 2:
cout<<"Linked list is: ";
disp();
break;
case 3:
del();
break;
case 4:
search();
break;
case 5:
return 0;
default:
cout<<"Invalid choice!!";
}
}
getch();
}

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons