Sie sind auf Seite 1von 16

/* Stack ADT */

template <class T>


class stack
{
private:
int top;
int MaxTop;
T *a;
public:
stack();
int IsEmpty();
int IsFull();
T Top();
stack<T> & Add(const T& x);
stack<T> & Delete(T &x);
void display(void);
};
template<class T>
stack<T> :: stack()
{
int size;
cout<<"Enter Max Size of Stack:\n";
cin>>size;
MaxTop=size-1;
a=new T[size];
top=-1;
}
template<class T>
int stack<T> :: IsEmpty()
{
if(top==-1) return 1;
else return 0;
}
template<class T>
int stack<T> :: IsFull()
{
if(top==MaxTop) return 1;
else return 0;
}
template<class T>
T stack<T>::Top()
{
if (IsEmpty()) cout<< "\n Stack Is Empty\n";
else return a[top];
}

1
template<class T>
stack<T> & stack<T> :: Add(const T &x)
{
if (IsFull()) cout<<"\n Stack Full\n";
else
{
a[++top]=x;
return *this;
}
}
template<class T>
stack<T> & stack<T> :: Delete(T &x)
{
if (IsEmpty()) cout<< "\n Stack Is Empty\n";
else
{
x=a[top--];
cout<<"\n Deleted Element: "<<x;
return *this;
}
}
template<class T>
void stack<T>::display(void)
{
for(int i=0;i<=top;i++) cout<<"\t"<<a[i];
}
int main()
{
cout<<"\n Stack For Integers:\n";
stack<int>s1;
int choice,x;
cout<<"\n Stack For Float:\n";
stack<float>s2;
float f;
char ch='y';
while(ch=='y')
{
cout<<"\n *******Stack Menu******* \n";
cout<<"\n 1.Push \n 2.Pop \n 3. Top Element \n 4. Is Empty \n 5. Is Full \n ";
cout<<"6. Display \n 7.Exit \n";
cout<<"\n Enter Choice:\t";
cin>>choice;
switch(choice)
{ case 1: cout<<"\n Enter integer element to be push :\n";
cin>>x; s1.Add(x);
cout<<"\n Enter float element to be push :\n";

2
cin>>f;
s2.Add(f);
break;

case 2: s1.Delete(x);
cout<<"\n Integer Element deleted from stack. Now Stack is :\n";
s2.Delete(f);
cout<<"\n Float Element deleted from stack. Now Stack is:\n ";
break;
case 3: cout<<"\n Top Most element of integer stack is :\t"<<s1.Top();
cout<<"\n Top Most element of float stack is :\t"<<s2.Top();
break;
case 4: if(s1.IsEmpty()) cout<<"\n Integer Stack is Empty\n";
else cout<<"\n Integer Stack is not empty";
if(s2.IsEmpty()) cout<<"\n Float Stack is Empty\n";
else cout<<"\n Float Stack is not empty";
break;
case 5: if(s1.IsFull()) cout<<"\n Integer Stack Is Full\n";
else cout<<"\n Integer Stack is Not Full\n";
if(s2.IsFull()) cout<<"\n Float Stack Is Full\n";
else cout<<"\n Flaot Stack is Not Full\n";
break;
case 6: cout<<"\n Integer Stack Elements are:\n "; s1.display();
cout<<"\n Float Stack Elements are:\n "; s2.display();
break;
default: cout<<"\n U Entered wrong choice try for again:\n";
exit(1);
break;
}
cout<<"\n If u want to continue press 'y' else 'n':\n ";
fflush(stdin);
cin>>ch;
}
return 0;
}

/* Queue ADT*/
template <class T>
class queue
{
private:
int front,rear;
int MaxSize;
T *q;
public:
queue();

3
int IsEmpty();
int IsFull();
T Rear();
T Front();
queue<T> & Add(const T& x);
queue<T> & Delete();
void display(void);
};
template<class T>
queue<T> :: queue()
{
int size;
cout<<"Enter Max Size of Queue:\n";
cin>>size;
MaxSize=size-1;
q=new T[size];
front=rear=-1;
}
template<class T>
int queue<T>::IsEmpty()
{
if(front==-1||rear==-1) return 1;
else return 0;
}
template<class T>
int queue<T>::IsFull()
{
if(rear==MaxSize||front==MaxSize) return 1;
else return 0;
}
template<class T>
T queue<T>::Rear()
{
if (IsFull()) cout<< "\n Queue Is Full\n";
else return q[rear];
}
template<class T>
T queue<T>::Front()
{
if (IsEmpty()) cout<< "\n Queue Is Empty\n";
else return q[front];
}
template<class T>
queue<T> & queue<T> :: Add(const T &x)
{
if (IsFull()) cout<<"\n Queue Full\n";

4
else
{
q[++rear]=x;
if(front==-1)
front++;
return *this;
}
}
template<class T>
queue<T> & queue<T> :: Delete(T &x)
{
if (IsEmpty()) cout<< "\n Queue Is Empty\n";
else x=q[front++];
return *this;
}
template<class T>
void queue<T>::display(void) { for(int i=front;i<=rear;i++) cout<<"\t"<<q[i]; }
int main()
{
cout<<"\n Queue For Integers:\n";
queue<int>q1;
int choice,x;
cout<<"\n Queue For Float:\n";
queue<float>q2;
float f;
char ch='y';
while(ch=='y')
{
cout<<"\n *******Queue Menu******* \n";
cout<<"\n 1.Insert \t 2.Delete \t 3. Is Empty \t 4. Is Full \n ";
cout<<"5. Rear \t 6.Front \t 7.Display \t 8.Exit \n";
cout<<"\n Enter Choice:\t";
cin>>choice;
switch(choice)
{ case 1: cout<<"\n Enter integer element to be Insert :\n";
cin>>x; q1.Add(x);
cout<<"\n Enter float element to be Insert :\n";
cin>>f;
q2.Add(f);
break;
case 2: cout<<"\n Integer Element deleted from queue \t "; q1.Delete();
cout<<"\n Float Element deleted from queue \t "; q2.Delete();
q2.display();
break;
case 3: if(q1.IsEmpty()) cout<<"\n Integer Queue is Empty\n";
else cout<<"\n Integer Queue is not empty";

5
if(q2.IsEmpty()) cout<<"\n Float Queue is Empty\n";
else cout<<"\n Float Queue is not empty";
break;
case 4: if(q1.IsFull()) cout<<"\n Integer Queue Is Full\n";
else cout<<"\n Integer Queue is Not Full\n";
if(q2.IsFull()) cout<<"\n Float Queue Is Full\n";
else cout<<"\n Flaot Queue is Not Full\n";
break;
case 5: cout<<"\n Rear Element of Integer queue = "<<q1.Rear();
cout<<"\n Rear Element of Float queue = "<<q2.Rear();
break;
case 6: cout<<"\n Front Elementof Integer queue = "<<q1.Front();
cout<<"\n Front Elementof Float queue = "<<q2.Front();
break;
case 7: cout<<"\n Integer Queue Elements are:\n "; q1.display();
cout<<"\n Float Queue Elements are:\n "; q2.display();
break;
default: cout<<"\n U Entered wrong choice try for again:\n";
exit(1);
break;
}
cout<<"\n If u want to continue press 'y' else 'n':\n ";
fflush(stdin);
cin>>ch;
}
return 0;
}

/* Stack Linked List */


template <class T>
class stack
{
struct node
{
T data;
node *next;
}*top,*bottom,*temp;
public:
stack();
int IsEmpty();
T Top();
stack<T> & Add(T &x);
stack<T> & Delete(void);
void display(void);
};
template<class T>

6
stack<T> :: stack()
{
top=NULL; bottom=NULL;
}
template<class T>
int stack<T> :: IsEmpty()
{
if(top==NULL||bottom==NULL) return 1;
else return 0;
}
template<class T>
T stack<T>::Top()
{
if (IsEmpty()) cout<< "\n Stack Is Empty\n";
else return top->data;
}
template<class T>
stack<T> & stack<T> :: Add(T &x)
{
temp=new node;
temp->data=x;
temp->next=NULL;
if(bottom==NULL)
bottom=top=temp;
else
{
top->next=temp;
top=temp;
}
return *this;
}
template<class T>
stack<T> & stack<T> :: Delete(void)
{
if (IsEmpty()) cout<< "\n Stack Is Empty\n";
else
{
temp=bottom;
if(temp->next==NULL)
{
cout<<"\n Deleted Element: "<<temp->data;
top=bottom=NULL;
}
else
{
while(temp->next->next!=NULL)

7
temp=temp->next;
cout<<"\n Deleted Element: "<<temp->next->data;
top=temp;
top->next=NULL;
}
}
return *this;
}
template<class T>
void stack<T>::display(void)
{
if(IsEmpty()) { cout<<"Stack Is Empty"; }
else
{
temp=bottom;
while(temp!=NULL)
{
cout<<"\t"<<temp->data;
temp=temp->next;
}
}
}
int main()
{
cout<<"\n Stack For Integers:\n";
stack<int>s1;
int choice,x;
cout<<"\n Stack For Float:\n";
stack<float>s2;
float f;
char ch='y';
while(ch=='y')
{
cout<<"\n *******Stack Menu******* \n";
cout<<"\n 1.Push \n 2.Pop \n 3. Top Element \n 4. Is Empty \n ";
cout<<"5. Display \n 6.Exit \n";
cout<<"\n Enter Choice:\t";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n Enter integer element to be push :\n";
cin>>x;
s1.Add(x);
cout<<"\n Enter float element to be push :\n";
cin>>f;
s2.Add(f);

8
break;
case 2: s1.Delete(); cout<<"\n Integer Element deleted from stack.\n";
s2.Delete(); cout<<"\n Float Element deleted from stack.\n ";
break;
case 3: cout<<"\n Top Most element of integer stack is :\t"<<s1.Top();
cout<<"\n Top Most element of float stack is :\t"<<s2.Top();
break;
case 4: if(s1.IsEmpty()) cout<<"\n Integer Stack is Empty\n";
else cout<<"\n Integer Stack is not empty";
if(s2.IsEmpty()) cout<<"\n Float Stack is Empty\n";
else cout<<"\n Float Stack is not empty";
break;
case 5: cout<<"\n Integer Stack Elements are:\n"; s1.display();
cout<<"\n Float Stack Elements are:\n"; s2.display();
break;
default: cout<<"\n U Entered wrong choice try for again:\n";
exit(1);
break;
}
cout<<"\n If u want to continue press 'y' else 'n':\n ";
fflush(stdin);
cin>>ch;
}
return 0;
}
/* Queue Linked List */
template <class T>
class queue
{
struct node
{
T data;
node *next;
}*front,*rear,*temp;
public:
queue();
int IsEmpty();
T Rear();
T Front();
queue<T> & Add(T &x);
queue<T> & Delete(void);
void display(void);
};

template<class T>
queue<T> :: queue()

9
{
rear=NULL; front=NULL;
}
template<class T>
int queue<T> :: IsEmpty()
{
if(rear==NULL||front==NULL) return 1;
else return 0;
}
template<class T>
T queue<T>::Rear()
{
if(IsEmpty()) cout<<"\n Queue Empty\n";
else return rear->data;
}
template<class T>
T queue<T>::Front()
{
if(IsEmpty()) cout<<"\n Queue Empty\n";
else return front->data;
}
template<class T>
queue<T> & queue<T> :: Add(T &x)
{
temp=new node;
temp->data=x;
temp->next=NULL;
if(front==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=temp;
}
return *this;
}
template<class T>
queue<T> & queue<T> :: Delete(void)
{
if (IsEmpty()) cout<< "\n Queue Is Empty\n";
else
{
if(front->next==NULL)
{
cout<<"\n Deleted Element: "<<front->data;
front=rear=NULL;

10
}
else
{
cout<<"\n Deleted Element: "<<front->data;
front=front->next;
}
}
return *this;
}
template<class T>
void queue<T>::display(void)
{
temp=front;
while(temp!=NULL)
{
cout<<"\t"<<temp->data;
temp=temp->next;
}
}
int main()
{
cout<<"\n Queue For Integers:\n";
queue<int>q1;
int choice,x;
cout<<"\n Queue For Float:\n";
queue<float>q2;
float f;
char ch='y';
while(ch=='y')
{
cout<<"\n *******Queue Menu******* \n";
cout<<"\n 1.Insert \n 2.Delelte \n 3. Rear Element \n 4. Front Element \n";
cout<<"5.IsEmpty \n 6. Display \n 7.Exit \n";
cout<<"\n Enter Choice:\t";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n Enter integer element to insert :\n";
cin>>x; q1.Add(x);
cout<<"\n Enter float element to insert :\n";
cin>>f; q2.Add(f);
break;
case 2: q1.Delete(); cout<<"\n Integer Element deleted from Queue.\n";
q2.Delete(); cout<<"\n Float Element deleted from Queue. :\n ";
break;
case 3: cout<<"\n Rear element of integer queue is :\t"<<q1.Rear();

11
cout<<"\n Rear element of float queue is :\t"<<q2.Rear();
break;
case 4: cout<<"\n Front element of integer queue is :\t"<<q1.Front();
cout<<"\n Front element of float queue is :\t"<<q2.Front();
break;
case 5: if(q1.IsEmpty()) cout<<"\n Integer Queue is Empty\n";
else cout<<"\n Integer Queue is not empty";
if(q2.IsEmpty()) cout<<"\n Float Queue is Empty\n";
else cout<<"\n Float Queue is not empty";
break;
case 6: cout<<"\n Integer Queue Elements are:\n "; q1.display();
cout<<"\n Float Queue Elements are:\n "; q2.display();
break;
default: cout<<"\n U Entered wrong choice try again:\n";
exit(1);
break;
}
cout<<"\n If u want to continue press 'y' else 'n':\n ";
fflush(stdin);
cin>>ch;
}
return 0;
}

/* Circular Queue ADT*/


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
template <class T>
class queue
{
private:
int front,rear;
int MaxSize;
T *q;
public:
queue();
int IsEmpty();
int IsFull();
T Rear();
T Front();
queue<T> & Add(const T& x);
queue<T> & Delete(T &x);
void display(void);
// ~queue() { delete [] q; }
};

12
template<class T>
queue<T> :: queue()
{
int size;
cout<<"Enter Max Size of Circular Queue:\n";
cin>>size;
MaxSize=size-1;
q=new T[size];
front=-1;
rear=-1;
}
template<class T>
int queue<T>::IsEmpty()
{
if(front==-1||rear==-1)
return 1;
else
return 0;
}
template<class T>
int queue<T>::IsFull()
{
if((rear==MaxSize && front==0) ||(front==rear+1) )
return 1;
else
return 0;
}
template<class T>
T queue<T>::Rear()
{
if (IsFull())
cout<< "\n Circular Queue Is Full\n";
else
return q[rear];
}
template<class T>
T queue<T>::Front()
{
if (IsEmpty())
cout<< "\n Circular Queue Is Empty\n";
else
return q[front];
}
template<class T>
queue<T> & queue<T> :: Add(const T &x)
{

13
if (IsFull())
cout<<"\n Circualr Queue Is Full\n";
else
{
rear=(rear+1)%(MaxSize+1);
q[rear]=x;
if(front==-1)
front++;
}
return *this;
}
template<class T>
queue<T> & queue<T> :: Delete(T &x)
{
if (IsEmpty())
cout<< "\n Circular Queue Is Empty\n";
else if(front==rear)
{
x=q[front];
front=-1;
rear=-1;
}
else
{
x=q[front];
front=(front+1)%(MaxSize+1);
}
return *this;
}
template<class T>
void queue<T>::display(void)
{
int i;
if(front>rear)
{
for(i=front;i<=MaxSize;i++)
cout<<"\t"<<q[i];
for(i=0;i<=rear;i++)
cout<<"\t"<<q[i];
}
else
{
for(i=front;i<=rear;i++)
cout<<"\t"<<q[i];
}
}

14
int main()
{
cout<<"\n Circular Queue For Integers:\n";
queue<int>q1;
int choice,x;
cout<<"\n Circular Queue For Float:\n";
queue<float>q2;
float f;
char ch='y';
while(ch=='y')
{
cout<<"\n *******Circular Queue Menu******* \n";
cout<<"\n 1.Insert \t 2.Delete \t 3. Is Empty \t 4. Is Full \n ";
cout<<"5. Rear \t 6.Front \t 7.Display \t 8.Exit \n";
cout<<"\n Enter Choice:\t";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n Enter integer element to be Insert :\n";
cin>>x; q1.Add(x);
cout<<"\n Enter float element to be Insert :\n";
cin>>f;
q2.Add(f);
break;
case 2:
cout<<"\n Integer Element deleted from queue \t ";
q1.Delete(x);
q1.display();
q2.Delete(f);
cout<<"\n Float Element deleted from queue \t ";
q2.display();
break;
case 3:
if(q1.IsEmpty())
cout<<"\n Integer Circular Queue is Empty\n";
else
cout<<"\n Integer Circular Queue is not empty";
if(q2.IsEmpty())
cout<<"\n Float Circular Queue is Empty\n";
else
cout<<"\n Float Circular Queue is not empty";
break;
case 4:
if(q1.IsFull())
cout<<"\n Integer Circular Queue Is Full\n";

15
else
cout<<"\n Integer Circular Queue is Not Full\n";
if(q2.IsFull())
cout<<"\n Float Circular Queue Is Full\n";
else
cout<<"\n Flaot Circular Queue is Not Full\n";
break;
case 5:
cout<<"\n Rear Element of Integer Circular queue = "<<q1.Rear();
cout<<"\n Rear Element of Float Circular queue = "<<q2.Rear();
break;
case 6:
cout<<"\n Front Elementof Integer Circular queue = "<<q1.Front();
cout<<"\n Front Elementof Float Circular queue = "<<q2.Front();
break;
case 7:
cout<<"\n Integer Circular Queue Elements are:\n ";
q1.display();
cout<<"\n Float Circular Queue Elements are:\n ";
q2.display();
break;
default:
cout<<"\n U Entered wrong choice try for again:\n";
break;
}
cout<<"\n If u want to continue press 'y' else 'n':\n ";
fflush(stdin);
cin>>ch;
}
return 0;
}

16

Das könnte Ihnen auch gefallen