Create a stack and queue in main function and demonstrate working of both above created functions. You can populate stack and queue with some random values.
CODE
#include<iostream>
using namespace std;
int main()
{
class Stack
{
private:
int *stackArray;
int stackSize;
int top;
public:
Stack(int size)
{
stackArray = new int[size];
stackSize = size;
top = -1;
}
bool isFull(void)
{
bool status;
if (top == stackSize - 1)
status = true;
else
status = false;
return status;
}
bool isEmpty(void)
{
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
void Push(int num)
{
if (isFull())
{
cout << "The stack is full.\n";
}
else
{
top++;
stackArray[top] = num;
}
}
int Pop(void)
{
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else
{
top--;
}
}
int Top()
{
return stackArray[top];
}
void Display(void)
{
if (isEmpty()) {
cout << "The stack is empty.\n";
}
else {
int t = top;
cout << "\tAll Value Show Store In Stack : " << endl;
while (t >= 0)
{
cout << "\t\tArray [" << t << "] = "
<< stackArray[t] << endl;
t--;
}
}
}
};
// Start of Queue
class QueueArr
{
private:
int *queueArray;
int queueSize;
int rear, front;
public:
~QueueArr()
{
delete [] queueArray;
}
// INQuee
QueueArr(int size)
{
queueArray = new int[size];
queueSize = size;
rear = -1;
front = -1;
}
Enqueue(int num)
{
if (isFull())
{
cout << "The queue is full.\n";
}
else
{
rear++;
queueArray[rear] = num;
}
}
// DeQueee
int Dequeue(void)
{
int data;
if (isEmpty())
{
cout << "The queue is empty.\n";
}
else
{
front++;
data = queueArray[front];
}
return data;
}
// IsFull
bool isFull(void)
{
bool status;
if (rear == queueSize - 1)
status = true;
else
status = false;
return status;
}
// IsEmpty
bool isEmpty(void)
{
bool status;
if (front == rear)
status = true;
else
status = false;
return status;
}
// Display Function defination
void Display(void)
{
if (isEmpty())
{
cout << "The Queue is empty.\n";
}
else
{
for (int i = front + 1; i <= rear; i++)
{
cout << "queueArray[" << i << "] = "
<< queueArray[i] << endl;
}
}
}
};
// end of Queue
// object creation
int Stacksize , QueueSize;
char choice;
cout << "Enter Size of Stack :- ";
cin >> Stacksize;
cout << "Enter Size of Queue :- ";
cin >> QueueSize;
Stack st(Stacksize);
QueueArr obj(QueueSize);
// calling QUeue And Stack
do
{
system("cls");
cout << "\t\tPress 1 : For Push value " << endl;
cout << "\t\tPress 2 : For Pop value "<<endl;
cout << "\t\tPress 3 : For Display all Stack information " << endl;
cout << "\t\tPress 4 : For seeing top value "<<endl;
cout << "\t\tPress 5 : For EnQueue " << endl;
cout << "\t\tPress 6 : For DeQueue "<<endl;
cout << "\t\tPress 7 : For Display all Queue information " << endl;
cout << "\t\tPress Any Button For Exit " << endl;
cout << endl;
cout << "\t\tEnter Your Choice :- " ;
cin >> choice;
switch (choice)
{
case '1':
int val;
cout << "\n\tInsert Value : ";
cin >> val;
st.Push(val);
break;
case '2':
st.Pop() ;
cout << "\tValue Removed " ;
cout<<endl;
break;
case '3':
cout << endl;
st.Display();
break;
case '4':
cout << "\n\t\tTop value of Stack :: " << st.Top() <<endl;
break;
case '5':
{
int val;
cout << "\n\tInsert Value : ";
cin >> val;
obj.Enqueue(val);
}
break;
case '6':
obj.Dequeue();
cout << "\tValue Removed " ;
cout<<endl;
break;
case '7':
cout << endl;
cout << "\tAll Value Show Store In Queue : " << endl;
obj.Display();
break;
default:
exit(0);
}
cout << endl;
cout << "\tDo You Want to Run Again (Y/y) : ";
cin >> choice;
} while (choice == 'Y' || choice == 'y'); // end of Stack
// End of Calling
cout << endl;
cout << "End of program!!! bye bye ... ";
system("pause");
return 0;
}