Question

In: Computer Science

I want to know 'Linked link + queue' and 'Linked link Stack' code in C++ But...

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.

Solutions

Expert Solution

#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:

  1. In main, we are initializing flag is 1 when ever the flag is 1 the while condition is true
  2. Executes menu option
  3. when 1 clicked element is inserted into stack
  4. when 2 clicked element is inserted into queue;
  5. when option 3 is selected top of the element from stack is removed;
  6. when option 4 is selected element will be removed from queue;
  7. for option 5 stack elements will be deleted and for 6 queue elements will be displayed

Related Solutions

Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack...
Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal =...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal = top; top...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
Hi I would like to see an example in c++ of Stack As Linked List I...
Hi I would like to see an example in c++ of Stack As Linked List I need a seek() function which receives a double number X and which returns in which position in the stack is X. If the value X is not in the stack, the function should return -1
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
How to write code for stack with singly linked list using C? Please show examples for...
How to write code for stack with singly linked list using C? Please show examples for create, free, isempty, push, top, pop functions.
Q1 A- What are stack and queue? What are the differences between stack and queue? B-...
Q1 A- What are stack and queue? What are the differences between stack and queue? B- What is the priority queue? What are the differences between queue and priority queue
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT