#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
int display(void);
int single(int d,int count);
int doubly(int d,int cnt);
void out(int c);
void outy(int cn);
struct single_ll
{
int data;
struct single_ll *e;
}*f,*l,*t,*q,*n;
struct double_ll
{
int entry;
struct double_ll *nxt;
struct double_ll *prev;
}*first,*last,*temp,*m,*r;
int ch,p=0,z;
int main()
{ f=NULL;
first=NULL;
int dec,count=0,cnt=0;
char c='Y';
while(c=='Y' || c=='y')
{ system("cls");
cout<<"\n"<<"Choose:";
cout<<"\n"<<"1==Single Linked List";
cout<<"\n"<<"2==Doubly Linked List";
cout<<"\n"<<"Your choice (1 or 2) : ";
cin>>ch;
if(ch==1)
{
dec=display();
count=single(dec,count);
}
else if(ch==2)
{
dec=display();
cnt=doubly(dec,cnt);
}
else
cout<<"\n"<<"You entered a wrong choice!!";
cout<<"\n"<<"Want to continue by re-entering your choice?(Y/N): ";
cin>>c;
}
cout<<"\n"<<"ThankYou!! Have A Nice Day :) "<<"\n"<<"\n";
getch();
}
int display(void)
{ int d;
cout<<"\n"<<"Choose:";
cout<<"\n"<<"1>Adding node"<<"\n"<<"2>Deleting node"<<"\n"<<"3>Displaying the contents"<<"\n";
cout<<"\n"<<"Your choice : ";
cin>>d;
return d;
}
int single(int d,int count)
{
switch(d)
{
case 1:{
int flag=0;
if(count<0)
count=0;
if(count==0)
{
cout<<"\n"<<"Link List is empty"<<"\n";
cout<<"You can only add first node"<<endl;
f=new single_ll;
cout<<"\n"<<"Enter the data in the node:";
cin>>f->data;
f->e=NULL;
l=f;
cout<<"\n"<<"Node added at position 1";
count++;
}
else
{
cout<<"\n"<<"The list has "<<count<<" nodes";
cout<<"\n"<<"Where you want to add the node?(Enter position.,eg.1 or 2...)";
cin>>p;
z=p;
if(p<0 || p>(count+1))
{
cout<<"\n"<<"Wrong Entry!!";
flag=1;
}
else if(p>1 && p<(count+1))
{
t=new single_ll;
n=f;
//Logic for inserting in b//
while((p--)!=1)
{
q=n;
n=n->e;
}
q->e=t;
t->e=n;
q=NULL;
n=NULL;
cout<<"\n"<<"Enter the data for the node:";
cin>>t->data;
}
else if(p==1)
{
t=new single_ll;
cout<<"\n"<<"Enter the data for the node:";
cin>>t->data;
t->e=f;
f=t;
t=NULL;
}
else if(p==(count+1))
{
t=new single_ll;
cout<<"\n"<<"Enter the data for the node:";
cin>>t->data;
l->e=t;
t->e=NULL;
l=t;
t=NULL;
}
count++;
if(flag==0)
cout<<"\n"<<"Node added at position "<<z;
}
return count;
}
break;
case 2: {
if(count<0)
count=0;
int flag=0;
if(count==0)
{
cout<<"\n"<<"List is empty!";
flag=1;
}
else
{
if(count>0)
cout<<"\n"<<"The list has "<<count<<" node(s)";
cout<<"\n"<<"Which node to delete(Enter position.,eg.1 or 2...):";
cin>>p;
z=p;
//Logic for deletion
if(p<=count)
{
if(p>1 && p<(count+1))
{
t=f;
while(p--!=1)
{
q=t;
t=t->e;
}
q->e=t->e;
t=NULL;
}
else if(p==1)
{
t=f;
f=f->e;
t=NULL;
}
if(flag==0)
{
cout<<"\n"<<"Deleted node is node"<<z;
return --count;
}
}
else
{
cout<<"\n"<<"Wrong entry!!";
return count;
}
}
}
break;
case 3: {
out(count);
return count;
}
break;
default:cout<<"\n"<<"Wrong Choice!";
}
}
void out(int c)
{
if(c==0)
cout<<"\n"<<"List is empty!";
else
{ cout<<"\n";
t=f;
do
{
cout<<t->data<<"-->>";
t=t->e;
}
while(t!=NULL);
}
}
//***********************************************//
int doubly(int d,int cnt)
{
switch(d)
{
case 1:{
int flag=0;
if(cnt<0)
cnt=0;
if(cnt==0)
{
cout<<"\n"<<"Link List is empty"<<"\n";
cout<<"You can only add first node"<<endl;
first=new double_ll;
cout<<"\n"<<"Enter the data in the node:";
cin>>first->entry;
first->nxt=NULL;
first->prev=NULL;
last=first;
cout<<"\n"<<"Node added at position 1";
cnt++;
}
else
{
cout<<"\n"<<"The list has "<<cnt<<" nodes";
cout<<"\n"<<"Where you want to add the node?(Enter position.,eg.1 or 2...)";
cin>>p;
z=p;
if(p<0 || p>(cnt+1))
{
cout<<"\n"<<"Wrong Entry!!";
flag=1;
return cnt;
}
else if(p>1 && p<(cnt+1))
{
temp=new double_ll;
m=first;
//Logic for inserting in b//
while((p--)!=1)
{
r=m;
m=m->nxt;
}
r->nxt=temp;
temp->nxt=m;
m->prev=temp;
temp->prev=r;
r=NULL;
m=NULL;
cout<<"\n"<<"Enter the data for the node:";
cin>>temp->entry;
}
else if(p==1)
{
temp=new double_ll;
cout<<"\n"<<"Enter the data for the node:";
cin>>temp->entry;
temp->nxt=first;
first->prev=temp;
first=temp;
temp=NULL;
}
else if(p==(cnt+1))
{
temp=new double_ll;
cout<<"\n"<<"Enter the data for the node:";
cin>>temp->entry;
last->nxt=temp;
temp->prev=last;
temp->nxt=NULL;
last=temp;
temp=NULL;
}
cnt++;
if(flag==0)
cout<<"\n"<<"Node added at position "<<z;
}
return cnt;
}
break;
case 2: {
if(cnt<0)
cnt=0;
int flag=0;
if(cnt==0)
{
cout<<"\n"<<"List is empty!";
flag=1;
}
else
{
if(cnt>0)
cout<<"\n"<<"The list has "<<cnt<<" node(s)";
cout<<"\n"<<"Which node to delete(Enter position.,eg.1 or 2...):";
cin>>p;
z=p;
//Logic for deletion
if(p<=cnt)
{
if(p>1 && p<(cnt+1))
{
temp=first;
while(p--!=1)
{
r=temp;
temp=temp->nxt;
m=temp->nxt;
}
r->nxt=m;
m->prev=r;
temp=NULL;
}
else if(p==1)
{
temp=first;
first=first->nxt;
first->prev=NULL;
temp=NULL;
}
if(flag==0)
{
cout<<"\n"<<"Deleted node is node"<<z;
return --cnt;
}
}
else
{
cout<<"\n"<<"Wrong entry!!";
return cnt;
}
}
}
break;
case 3: {
outy(cnt);
return cnt;
}
break;
default:cout<<"\n"<<"Wrong Choice!";
}
}
void outy(int c)
{
if(c==0)
cout<<"\n"<<"List is empty!";
else
{ cout<<"\n";
temp=first;
do
{
cout<<temp->entry<<"-->>";
temp=temp->nxt;
}
while(temp!=NULL);
}
}
using namespace std;
#include<conio.h>
#include<stdlib.h>
int display(void);
int single(int d,int count);
int doubly(int d,int cnt);
void out(int c);
void outy(int cn);
struct single_ll
{
int data;
struct single_ll *e;
}*f,*l,*t,*q,*n;
struct double_ll
{
int entry;
struct double_ll *nxt;
struct double_ll *prev;
}*first,*last,*temp,*m,*r;
int ch,p=0,z;
int main()
{ f=NULL;
first=NULL;
int dec,count=0,cnt=0;
char c='Y';
while(c=='Y' || c=='y')
{ system("cls");
cout<<"\n"<<"Choose:";
cout<<"\n"<<"1==Single Linked List";
cout<<"\n"<<"2==Doubly Linked List";
cout<<"\n"<<"Your choice (1 or 2) : ";
cin>>ch;
if(ch==1)
{
dec=display();
count=single(dec,count);
}
else if(ch==2)
{
dec=display();
cnt=doubly(dec,cnt);
}
else
cout<<"\n"<<"You entered a wrong choice!!";
cout<<"\n"<<"Want to continue by re-entering your choice?(Y/N): ";
cin>>c;
}
cout<<"\n"<<"ThankYou!! Have A Nice Day :) "<<"\n"<<"\n";
getch();
}
int display(void)
{ int d;
cout<<"\n"<<"Choose:";
cout<<"\n"<<"1>Adding node"<<"\n"<<"2>Deleting node"<<"\n"<<"3>Displaying the contents"<<"\n";
cout<<"\n"<<"Your choice : ";
cin>>d;
return d;
}
int single(int d,int count)
{
switch(d)
{
case 1:{
int flag=0;
if(count<0)
count=0;
if(count==0)
{
cout<<"\n"<<"Link List is empty"<<"\n";
cout<<"You can only add first node"<<endl;
f=new single_ll;
cout<<"\n"<<"Enter the data in the node:";
cin>>f->data;
f->e=NULL;
l=f;
cout<<"\n"<<"Node added at position 1";
count++;
}
else
{
cout<<"\n"<<"The list has "<<count<<" nodes";
cout<<"\n"<<"Where you want to add the node?(Enter position.,eg.1 or 2...)";
cin>>p;
z=p;
if(p<0 || p>(count+1))
{
cout<<"\n"<<"Wrong Entry!!";
flag=1;
}
else if(p>1 && p<(count+1))
{
t=new single_ll;
n=f;
//Logic for inserting in b//
while((p--)!=1)
{
q=n;
n=n->e;
}
q->e=t;
t->e=n;
q=NULL;
n=NULL;
cout<<"\n"<<"Enter the data for the node:";
cin>>t->data;
}
else if(p==1)
{
t=new single_ll;
cout<<"\n"<<"Enter the data for the node:";
cin>>t->data;
t->e=f;
f=t;
t=NULL;
}
else if(p==(count+1))
{
t=new single_ll;
cout<<"\n"<<"Enter the data for the node:";
cin>>t->data;
l->e=t;
t->e=NULL;
l=t;
t=NULL;
}
count++;
if(flag==0)
cout<<"\n"<<"Node added at position "<<z;
}
return count;
}
break;
case 2: {
if(count<0)
count=0;
int flag=0;
if(count==0)
{
cout<<"\n"<<"List is empty!";
flag=1;
}
else
{
if(count>0)
cout<<"\n"<<"The list has "<<count<<" node(s)";
cout<<"\n"<<"Which node to delete(Enter position.,eg.1 or 2...):";
cin>>p;
z=p;
//Logic for deletion
if(p<=count)
{
if(p>1 && p<(count+1))
{
t=f;
while(p--!=1)
{
q=t;
t=t->e;
}
q->e=t->e;
t=NULL;
}
else if(p==1)
{
t=f;
f=f->e;
t=NULL;
}
if(flag==0)
{
cout<<"\n"<<"Deleted node is node"<<z;
return --count;
}
}
else
{
cout<<"\n"<<"Wrong entry!!";
return count;
}
}
}
break;
case 3: {
out(count);
return count;
}
break;
default:cout<<"\n"<<"Wrong Choice!";
}
}
void out(int c)
{
if(c==0)
cout<<"\n"<<"List is empty!";
else
{ cout<<"\n";
t=f;
do
{
cout<<t->data<<"-->>";
t=t->e;
}
while(t!=NULL);
}
}
//***********************************************//
int doubly(int d,int cnt)
{
switch(d)
{
case 1:{
int flag=0;
if(cnt<0)
cnt=0;
if(cnt==0)
{
cout<<"\n"<<"Link List is empty"<<"\n";
cout<<"You can only add first node"<<endl;
first=new double_ll;
cout<<"\n"<<"Enter the data in the node:";
cin>>first->entry;
first->nxt=NULL;
first->prev=NULL;
last=first;
cout<<"\n"<<"Node added at position 1";
cnt++;
}
else
{
cout<<"\n"<<"The list has "<<cnt<<" nodes";
cout<<"\n"<<"Where you want to add the node?(Enter position.,eg.1 or 2...)";
cin>>p;
z=p;
if(p<0 || p>(cnt+1))
{
cout<<"\n"<<"Wrong Entry!!";
flag=1;
return cnt;
}
else if(p>1 && p<(cnt+1))
{
temp=new double_ll;
m=first;
//Logic for inserting in b//
while((p--)!=1)
{
r=m;
m=m->nxt;
}
r->nxt=temp;
temp->nxt=m;
m->prev=temp;
temp->prev=r;
r=NULL;
m=NULL;
cout<<"\n"<<"Enter the data for the node:";
cin>>temp->entry;
}
else if(p==1)
{
temp=new double_ll;
cout<<"\n"<<"Enter the data for the node:";
cin>>temp->entry;
temp->nxt=first;
first->prev=temp;
first=temp;
temp=NULL;
}
else if(p==(cnt+1))
{
temp=new double_ll;
cout<<"\n"<<"Enter the data for the node:";
cin>>temp->entry;
last->nxt=temp;
temp->prev=last;
temp->nxt=NULL;
last=temp;
temp=NULL;
}
cnt++;
if(flag==0)
cout<<"\n"<<"Node added at position "<<z;
}
return cnt;
}
break;
case 2: {
if(cnt<0)
cnt=0;
int flag=0;
if(cnt==0)
{
cout<<"\n"<<"List is empty!";
flag=1;
}
else
{
if(cnt>0)
cout<<"\n"<<"The list has "<<cnt<<" node(s)";
cout<<"\n"<<"Which node to delete(Enter position.,eg.1 or 2...):";
cin>>p;
z=p;
//Logic for deletion
if(p<=cnt)
{
if(p>1 && p<(cnt+1))
{
temp=first;
while(p--!=1)
{
r=temp;
temp=temp->nxt;
m=temp->nxt;
}
r->nxt=m;
m->prev=r;
temp=NULL;
}
else if(p==1)
{
temp=first;
first=first->nxt;
first->prev=NULL;
temp=NULL;
}
if(flag==0)
{
cout<<"\n"<<"Deleted node is node"<<z;
return --cnt;
}
}
else
{
cout<<"\n"<<"Wrong entry!!";
return cnt;
}
}
}
break;
case 3: {
outy(cnt);
return cnt;
}
break;
default:cout<<"\n"<<"Wrong Choice!";
}
}
void outy(int c)
{
if(c==0)
cout<<"\n"<<"List is empty!";
else
{ cout<<"\n";
temp=first;
do
{
cout<<temp->entry<<"-->>";
temp=temp->nxt;
}
while(temp!=NULL);
}
}
No comments:
Post a Comment