3)Write C++ programs to implement the stack following using a singly linked list.
#include<iostream.h>
#include<conio.h>
template <class T>
class stack
{
struct node
{
T data;
node *link;
}* top;
public:
stack();
void push(T);
T pop();
void display();
~stack();
};
template <class T>
stack<T>::stack()
{
top=0;
}
template <class T>
void stack<T>:: push(T x)
{
node *p=new node;
p->data=x;
p->link=top;
top=p;
}
template <class T>
T stack<T>::pop()
{
node *temp=top;
top=top->link;
T x=temp->data;
delete temp;
return x;
}
template <class T>
void stack<T>::display()
{
if(top==0)
cout<<"Empty stack"<<endl;
else
{
node* temp=top;
while(temp!=0)
{
cout<<temp->data<<" ";
temp=temp->link;
}
cout<<endl;
}
}
template <class T>
stack<T>::~stack()
{
node *temp;
while(top!=0)
{
temp=top;
top=top->link;
delete temp;
}
}
void menu1()
{
cout<<"1.Interger stack"<<endl;
cout<<"2.Float stack"<<endl;
cout<<"3.Character stack"<<endl;
}
void menu2()
{
cout<<"1.Insert"<<endl;
cout<<"2.Delete"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Exit"<<endl;
}
template <class T>
void stackop(stack<T> st)
{
int ch;
T x,r;
menu2();
cin>>ch;
while(ch<4)
{
switch(ch)
{
case 1:
cout<<"Enter element"<<endl;
cin>>x;
st.push(x);
break;
case 2:
r=st.pop();
if(r==-1)
cout<<"Element cannot be deleted as stack is empty"<<endl;
else
cout<<"Deleted element is "<<r<<endl;
break;
case 3:
st.display();
break;
}
menu2();
cin>>ch;
}
}
void main()
{
clrscr();
int ch;
menu1();
cin>>ch;
if(ch<4)
{
switch(ch)
{
case 1:
{
stack<int> s;
stackop(s);
break;
}
case 2:
{
stack<float> s;
stackop(s);
break;
}
case 3:
{
stack<char> s;
stackop(s);
break;
}
default :
break;
}
}
getch();
}
4)Write C++ programs to implement the queue following using a singly linked list.
#include <iostream.h>
#include <conio.h>
template <class T>
struct node
{
T data;
node *link;
};
template <class T>
class queue
{
node<T> *f,*r;
public:
queue();
void insert(T);
T del();
void display();
T first();
T last();
};
template <class T>
queue<T>::queue()
{
f=0;
r=0;
}
template <class T>
void queue<T> :: insert(T x)
{
node<T> *p=new node<T>;
p->data=x;
p->link=0;
if(f==0)
{
f=p;
r=p;
}
else
{
r->link=p;
r=p;
}
}
template <class T>
T queue<T>::del()
{
if(f==0)
return -1;
else
{
node<T> *temp=f;
T x=temp->data;
f=f->link;
return x;
}
}
template <class T>
void queue<T> ::display()
{
if(f==0)
cout<<"Empty queue"<<endl;
else
{
node<T> *temp=f;
while(temp!=0)
{
cout<<temp->data<<" ";
temp=temp->link;
}
cout<<endl;
}
}
template <class T>
T queue<T>::first()
{
if(f==0)
return -1;
else
return f->data;
}
template <class T>
T queue<T>::last()
{
if(f==0)
return -1;
else
return r->data;
}
void menu1()
{
cout<<"1.Interger queue"<<endl;
cout<<"2.Float queue"<<endl;
cout<<"3.Character queue"<<endl;
}
void menu2()
{
cout<<"1.Insert"<<endl;
cout<<"2.Delete"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.display first elemant"<<endl;
cout<<"5.display last element"<<endl;
cout<<"6.Exit"<<endl;
}
template <class T>
void queueop(queue<T> q)
{
int ch;
T x,r;
menu2();
cin>>ch;
while(ch<6)
{
switch(ch)
{
case 1:
cout<<"Enter element"<<endl;
cin>>x;
q.insert(x);
break;
case 2:
r=q.del();
if(r==-1)
cout<<"Element cannot be deleted as stack is empty"<<endl;
else
cout<<"Deleted element is "<<r<<endl;
break;
case 3:
q.display();
break;
case 4:
r=q.first();
if(r==-1)
cout<<"Empty queue"<<endl;
else
cout<<"first element is"<<r<<endl;
break;
case 5:
r=q.last();
if(r==-1)
cout<<"Empty queue"<<endl;
else
cout<<"last element is"<<r<<endl;
break;
}
menu2();
cin>>ch;
}
}
void main()
{
clrscr();
int ch;
menu1();
cin>>ch;
if(ch<4)
{
switch(ch)
{
case 1:
{
queue<int> q;
queueop(q);
break;
}
case 2:
{
queue<float> q;
queueop(q);
break;
}
case 3:
{
queue<char> q;
queueop(q);
break;
}
default :
break;
}
}
getch();
}
No comments:
Post a Comment