#include<iostream>
#include<conio.h>
using namespace std;
struct stack
{
int data;
struct stack *next;
}*start=NULL;
typedef struct stack node;
void create();
void insert_atbeg();
void insert_after();
void deletenode();
void display();
void reverse(node *);
main()
{
int choice,ch;
do
{
cout<<"\n 1. create";
cout<<"\n 2. insert ";
cout<<"\n 3.delete";
cout<<"\n 4. display";
cout<<"\n 5.reverse";
cout<<"\n Enter the choice :";
cin>>choice;
switch(choice)
{
case 1 : create();
break;
case 2 :
cout<<"\n 1.Insert at beginning";
cout<<"\n 2. Insert after a node";
cout<<"\n Enter choice ";
cin>>ch;
switch(ch)
{ case 1 : insert_atbeg();
break;
case 2 : insert_after();
break;
default : break;
}
break;
case 3 : deletenode();
break;
case 4 : display();
break;
case 5 : reverse(start);
break;
default : cout<<"\n Wrong choice! ";
break;
}
}while(choice>0&&choice<5);
getch();
}
void create()
{
char choice;
do
{
node *temp,*first;
temp=new node;
cout<<"\n Enter the item to insert ";
cin>>temp->data;
temp->next=NULL;
if(start==NULL)
{ start=temp;
first=start;
}
else
{
first->next=temp;
first=temp;
}
cout<<"\n Do you want to enter more node ?(y/n)";
cin>>choice;
}while(toupper(choice)=='Y');
}
void insert_atbeg()
{ node *temp;
temp=new node;
cout<<"\n Enter the data :";
cin>>temp->data;
temp->next=start;
start=temp;
}
void insert_after()
{
int location;
display();
cout<<"\n Enter the location to insert :";
cin>>location;
node *temp,*first;;
first=start;
temp = new node;
cout<<"\n Enter the data item :";
cin>>temp->data;
while(first!=NULL)
{
if(first->data==location)
{
if(first->next==NULL)
{ first->next=temp;
temp->next=NULL;
}
else
{ temp->next=first->next;
first->next=temp;
}
}
first=first->next;
}
}
void deletenode()
{
node *temp,*c;
int item;
if(start==NULL)
cout<<"\n No item to delete ";
else
{
temp=start;
cout<<"\n Enter the node to delete ";
cin>>item;
if(temp->data==item)
{
if(temp->next==NULL)
{
delete temp;
start=NULL;
}
else
{
start=start->next;
delete temp;
}
}
else
{
while(temp->next!=NULL)
{
if(temp->next->data==item)
{
c=temp->next;
temp->next=temp->next->next;
delete c;
break;
}
temp=temp->next;
}
}
}
}
void display()
{
node *temp;
if(start==NULL)
cout<<"\n No item to display ";
else
{ cout<<"\n";
temp=start;
while(temp!=NULL)
{
cout<<" "<<temp->data;
temp=temp->next;
}
}
}
void reverse(node *t)
{
if(t==NULL)
return;
reverse(t->next);
printf("%d",t->data);
}
#include<conio.h>
using namespace std;
struct stack
{
int data;
struct stack *next;
}*start=NULL;
typedef struct stack node;
void create();
void insert_atbeg();
void insert_after();
void deletenode();
void display();
void reverse(node *);
main()
{
int choice,ch;
do
{
cout<<"\n 1. create";
cout<<"\n 2. insert ";
cout<<"\n 3.delete";
cout<<"\n 4. display";
cout<<"\n 5.reverse";
cout<<"\n Enter the choice :";
cin>>choice;
switch(choice)
{
case 1 : create();
break;
case 2 :
cout<<"\n 1.Insert at beginning";
cout<<"\n 2. Insert after a node";
cout<<"\n Enter choice ";
cin>>ch;
switch(ch)
{ case 1 : insert_atbeg();
break;
case 2 : insert_after();
break;
default : break;
}
break;
case 3 : deletenode();
break;
case 4 : display();
break;
case 5 : reverse(start);
break;
default : cout<<"\n Wrong choice! ";
break;
}
}while(choice>0&&choice<5);
getch();
}
void create()
{
char choice;
do
{
node *temp,*first;
temp=new node;
cout<<"\n Enter the item to insert ";
cin>>temp->data;
temp->next=NULL;
if(start==NULL)
{ start=temp;
first=start;
}
else
{
first->next=temp;
first=temp;
}
cout<<"\n Do you want to enter more node ?(y/n)";
cin>>choice;
}while(toupper(choice)=='Y');
}
void insert_atbeg()
{ node *temp;
temp=new node;
cout<<"\n Enter the data :";
cin>>temp->data;
temp->next=start;
start=temp;
}
void insert_after()
{
int location;
display();
cout<<"\n Enter the location to insert :";
cin>>location;
node *temp,*first;;
first=start;
temp = new node;
cout<<"\n Enter the data item :";
cin>>temp->data;
while(first!=NULL)
{
if(first->data==location)
{
if(first->next==NULL)
{ first->next=temp;
temp->next=NULL;
}
else
{ temp->next=first->next;
first->next=temp;
}
}
first=first->next;
}
}
void deletenode()
{
node *temp,*c;
int item;
if(start==NULL)
cout<<"\n No item to delete ";
else
{
temp=start;
cout<<"\n Enter the node to delete ";
cin>>item;
if(temp->data==item)
{
if(temp->next==NULL)
{
delete temp;
start=NULL;
}
else
{
start=start->next;
delete temp;
}
}
else
{
while(temp->next!=NULL)
{
if(temp->next->data==item)
{
c=temp->next;
temp->next=temp->next->next;
delete c;
break;
}
temp=temp->next;
}
}
}
}
void display()
{
node *temp;
if(start==NULL)
cout<<"\n No item to display ";
else
{ cout<<"\n";
temp=start;
while(temp!=NULL)
{
cout<<" "<<temp->data;
temp=temp->next;
}
}
}
void reverse(node *t)
{
if(t==NULL)
return;
reverse(t->next);
printf("%d",t->data);
}
No comments:
Post a Comment