Instructions: Kindly check the code below and (correct if there are mistakes in the program), and must require to put SORTING and SEARCHED. (20 max size value; set it vertical for stacks and horizontal for queue on the display from SORTING.) #include using namespace std; struct Node { int data; Node *link; }; Node *top = NULL; bool isempty() { if(top == NULL) return true; else return false; } void push (int value) { Node *ptr = new Node(); ptr->data = value; ptr->link = top; top = ptr; } void pop() { if (isempty()) cout<<"## Stack is Empty"< link; delete(ptr); } } void showTop() { if (isempty()) cout<<"## Stack is Empty"<data <data; temp=temp->link; counter++; } cout<<"## Stack Size : "<dataQ= valueQ; ptr->linkQ = NULL; if( front == NULL ) { front = ptr; rear = ptr; } else { rear ->linkQ = ptr; rear = ptr; } } void dequeue ( ) { if( isemptyQ() ) cout<<"Queue is empty\n"; else if( front == rear) { free(front); front = rear = NULL; } else { NodeQ *ptr = front; front = front->linkQ; free(ptr); } } void showfront( ) { if( isemptyQ()) cout<<"Queue is empty\n"; else cout<<"Element at front is: "<dataQ; } void sizeQueue() { if (isemptyQ()) cout<<"Queue is empty\n"; else { NodeQ *ptr = front; int counterQ = 0; while( ptr !=NULL) { ptr->dataQ; ptr= ptr->linkQ; counterQ++; } cout << "Size is: " << counterQ <> mm; if (mm == 1) { int choice, flag=1, value, count=0; const int MAX_SIZE=5; while( flag == 1) { cout<<"\nMENU FOR STACKS: " <>choice; switch (choice) { case 1: if(count==MAX_SIZE){ cout<<"## Stack is Full!"<>value; push(value); count++; } break; case 2: pop(); count--; break; case 3: showTop(); break; case 4: size(); break; case 5: flag = 0; break; default: cout << "\nWrong Choice!!\n"; } } } else if (mm == 2) { int ch, flag2=1, value; while( flag2 == 1) { cout << "\nMENU FOR QUEUES: "<>ch; switch (ch) { case 1: cout<<"Enter Value: "; cin>>value; enqueue(value); break; case 2: dequeue(); break; case 3: showfront(); break; case 4: sizeQueue(); break; case 5: flag2 = 0; break; } } } else { flow=0; } } exit(0); }
Instructions: Kindly check the code below and (correct if there are mistakes in the program), and must require to put SORTING and SEARCHED. (20 max size value; set it vertical for stacks and horizontal for queue on the display from SORTING.)
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *link;
};
Node *top = NULL;
bool isempty()
{
if(top == NULL)
return true;
else
return false;
}
void push (int value)
{
Node *ptr = new Node();
ptr->data = value;
ptr->link = top;
top = ptr;
}
void pop()
{
if (isempty())
cout<<"## Stack is Empty"<<endl;
else
{
Node *ptr = top;
top = top -> link;
delete(ptr);
}
}
void showTop()
{
if (isempty())
cout<<"## Stack is Empty"<<endl;
else
cout<<"The element at top is : "<< top->data <<endl;
}
void size()
{
if (isempty())
cout<<"## Stack is Empty"<<endl;
else
{
Node *temp=top;
int counter = 0;
while(temp!=NULL)
{
temp->data;
temp=temp->link;
counter++;
}
cout<<"## Stack Size : "<<counter<<endl;
}
}
struct NodeQ
{
int dataQ;
NodeQ *linkQ;
};
NodeQ *front = NULL;
NodeQ *rear = NULL;
bool isemptyQ()
{
if(front == NULL && rear == NULL)
return true;
else
return false;
}
void enqueue ( int valueQ )
{
NodeQ *ptr = new NodeQ();
ptr->dataQ= valueQ;
ptr->linkQ = NULL;
if( front == NULL )
{
front = ptr;
rear = ptr;
}
else
{
rear ->linkQ = ptr;
rear = ptr;
}
}
void dequeue ( )
{
if( isemptyQ() )
cout<<"Queue is empty\n";
else
if( front == rear)
{
free(front);
front = rear = NULL;
}
else
{
NodeQ *ptr = front;
front = front->linkQ;
free(ptr);
}
}
void showfront( )
{
if( isemptyQ())
cout<<"Queue is empty\n";
else
cout<<"Element at front is: "<<front->dataQ;
}
void sizeQueue()
{
if (isemptyQ())
cout<<"Queue is empty\n";
else
{
NodeQ *ptr = front;
int counterQ = 0;
while( ptr !=NULL)
{
ptr->dataQ;
ptr= ptr->linkQ;
counterQ++;
}
cout << "Size is: " << counterQ <<endl;
}
}
int main()
{
int flow=1;
while (flow == 1)
{
int mm;
cout << "\nMain Menu: " <<endl;
cout << "\n[1] Stacks";
cout << "\n[2] Queues";
cout << "\n[3] Quit\n";
cout << "\nEnter Choice: ";
cin >> mm;
if (mm == 1)
{
int choice, flag=1, value, count=0;
const int MAX_SIZE=5;
while( flag == 1)
{
cout<<"\nMENU FOR STACKS: " <<endl;
cout << "\n[1] Push";
cout << "\n[2] Pop";
cout << "\n[3] Top";
cout << "\n[4] Size";
cout << "\n[5] Exit\n";
cout<<"\nEnter your choice : ";
cin>>choice;
switch (choice)
{
case 1: if(count==MAX_SIZE){
cout<<"## Stack is Full!"<<endl;}
else{
cout<<"Enter the Value to be pushed : ";
cin>>value;
push(value);
count++;
}
break;
case 2: pop();
count--;
break;
case 3: showTop();
break;
case 4: size();
break;
case 5: flag = 0;
break;
default: cout << "\nWrong Choice!!\n";
}
}
}
else if (mm == 2)
{
int ch, flag2=1, value;
while( flag2 == 1)
{
cout << "\nMENU FOR QUEUES: "<<endl;
cout << "\n[1] Enqueue";
cout << "\n[2] Dequeue";
cout << "\n[3] Front";
cout << "\n[4] Size";
cout << "\n[5] Exit\n";
cout << "\nEnter your choice: ";
cin>>ch;
switch (ch)
{
case 1: cout<<"Enter Value: ";
cin>>value;
enqueue(value);
break;
case 2: dequeue();
break;
case 3: showfront();
break;
case 4: sizeQueue();
break;
case 5: flag2 = 0;
break;
}
}
}
else
{
flow=0;
}
}
exit(0);
}
Step by step
Solved in 10 steps with 7 images