In: Computer Science
I want to know 'Linked link + queue' and 'Linked link Stack' code in C++
But there is a condition. There shall be the following 'menu':
*****Menu*****
1. Insert into Stack 2. Insert into Queue
3. Delete from Stack 4. Delete from Queue
5. View Content in Stack 6. View Content in Queue
**********************************************
Let me give you a example.
>> 1 30 // Insert 30 into Stack
>>1 58// Insert 58 into Stack
>>1 26 // Insert 26 into Stack
>>3 // Delete from Stack
>>5 // View Content in Stack
Result: 58 30 ( Outputs in LIFO order)
Please help me. I am so curious.
#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 insert_stack (int value)
{
// Create new node temp and allocate memory
Node *ptr = new Node ();
ptr->data = value;
ptr->link = top;
//// Make ptr as top of Stack
top = ptr;
}
void del_stack ()
{
//check stack is empty or not
if (isempty ())
{
cout << "Stack is empty";
}
else
{
Node *ptr = top;
// Destroy connection between
// first and second
top = top->link;
delete (ptr);
}
}
void viewContent_stack ()
{
struct Node *temp;
// Check for stack underflow
if (isempty ())
{
cout << "\nStack Underflow";
exit (1);
}
else
{
temp = top;
cout << "Result:";
while (temp != NULL)
{
// Print node data
cout << temp->data << " ";
// Assign temp link to temp
temp = temp->link;
}
}
}
struct Qnode
{
int qdata;
Qnode *qlink;
};
Qnode *front = NULL;
Qnode *rear = NULL;
bool isqempty ()
{
if (front == NULL && rear == NULL)
{
return true;
}
else
return false;
}
void insert_queue (int value)
{
//new node and ptr store its address
Qnode *ptr = new Qnode ();
//put the value in data part
ptr->qdata = value;
//make link is NULL
ptr->qlink = NULL;
//adjust front pointer
if (front == NULL)
{
//it points to first node
front = ptr;
rear = ptr;
}
else
{
rear->qlink = ptr;
rear = ptr;
}
}
void del_queue ()
{
if (isqempty ())
cout << "Queue empty";
else
{
if (front == rear)
{
//delete
free (front);
front = rear = NULL;
}
else
{
Qnode *ptr = front;
front = front->qlink;
free (ptr);
}
}
}
void viewContent_queue ()
{
if (front == NULL)
{
cout << "queue Underflow." << endl;
return;
}
Qnode *temp = front;
cout << "Result:";
//will check until NULL is not found
while (temp)
{
cout << temp->qdata << " ";
temp = temp->qlink;
}
cout << endl;
}
int main ()
{
int c, num, flag = 1;
cout<<"\n";
cout << "*****Menu*****" << "\n";
cout << "1. Insert into Stack 2. Insert into Queue\n";
cout << "3. Delete from Stack 4. Delete from Queue \n";
cout << "5. View Content in Stack 6. View Content in Queue \n";
cout << "**********************************************\n";
//read choice from the menu
while (flag == 1)
{
cin >> c;
if (c == 1)
{
cin >> num;
insert_stack (num);
}
else if (c == 2)
{
cin >> num;
insert_queue (num);
}
else if (c == 3)
{
del_stack ();
}
else if (c == 4)
del_queue ();
else if (c == 5)
{
viewContent_stack ();
cout<<"\n";
cout << "*****Menu*****" << "\n";
cout << "1. Insert into Stack 2. Insert into Queue\n";
cout << "3. Delete from Stack 4. Delete from Queue \n";
cout << "5. View Content in Stack 6. View Content in Queue \n";
cout << "**********************************************\n";
}
else if(c==6)
{
viewContent_queue ();
cout<<"\n";
cout << "*****Menu*****" << "\n";
cout << "1. Insert into Stack 2. Insert into Queue\n";
cout << "3. Delete from Stack 4. Delete from Queue \n";
cout << "5. View Content in Stack 6. View Content in Queue \n";
cout << "**********************************************\n";
}
else{
flag=0;
}
}
return 0;
}
//screenshots for code:
Explanation: