Can anyone convert this c++ code to python?   //THIS PROGRAM IS CREATED TO DEMONSTRATE THE OPERATIONS PERFORMED ON STACK & ITS IMPLEMENTATION USING ARRAYS #include #include #include #include #include //Defining the maximum size of the stack #define MAXSIZE 7

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Can anyone convert this c++ code to python?

 

//THIS PROGRAM IS CREATED TO DEMONSTRATE THE OPERATIONS PERFORMED ON STACK & ITS IMPLEMENTATION USING ARRAYS

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <process.h>

//Defining the maximum size of the stack
#define MAXSIZE 7

using namespace std;

//A class initialized with public and private variables and functions
class STACK_ARRAY
    {
    int stack[MAXSIZE];
    int Top;
    public:
        //constructor is called and Top pointer is initialized to -1 when an object is created for the class
 STACK_ARRAY()
    {
        Top = -1;
    }
    void push();
    void pop();
    void traverse();
    };
    
//This function will add/insert an element to Top of the stack
void STACK_ARRAY::push()
{
 int item;
 //if the top pointer already reached the maximum allowed size then we can say that the stack is full or overflow
 if (Top == MAXSIZE-1)
    {
    cout << "\nThe Stack Is Full";
    }
 //Otherwise an element can be added or inserted by incrementing the stack 
pointer Top as follows
 else
    {
        cout << "\nEnter The Element (number) To Be Inserted: "; cin >> item;
        stack[++Top] = item;
    }
}

//This function will delete an element from the Top of the stack
void STACK_ARRAY::pop()
    {
        int item;
 //If the Top pointer points to NULL, then the stack is empty. That is NO element is there to delete or pop
    if (Top == -1)
        cout << "\nThe Stack Is Empty";
 //Otherwise the top most element in the stack is popped or deleted by decrementing the Top pointer
 else
    {
        item = stack[Top--];
        cout << "\nThe Deleted Element (number) Is: " << item;
    }
}

//This function to print all the existing elements in the stack
void STACK_ARRAY::traverse(){
 int i;
 //If the Top pointer points to NULL, then the stack is empty. That is NO element is there to delete or pop
 if (Top == -1)
    cout << "\nThe Stack is Empty";
 //Otherwise all the elements in the stack is printed
 else
    {
        cout << "\nThe Element(s) in the Stack(s) is/are: ";
        for(i = Top; i >= 0; i--)
            cout << stack[i] << " ";
        cout << "\n";
    }
}

int main()
{
 int choice;
 char ch;
 //Declaring an object to the class
 STACK_ARRAY ps;
 do
{
    //A menu for the stack operations
    cout << "MAIN MENU\n\n";
    cout << "STACK using Array \n\n";
    cout << "[1] PUSH (Add number)\n";
    cout << "[2] POP (Delete number)\n";
    cout << "[3] TRAVERSE (Display number)\n";
    cout << "[4] QUIT\n\n";
    cout << "Enter Your Choice: "; cin >> choice;
    switch(choice)
    {
        case 1://Calling push() function by class object
            ps.push();
            break;
        case 2://calling pop() function
            ps.pop();
            break;
        case 3://calling traverse() function
            ps.traverse();
            break;
        case 4:
            exit(0);
        default:
            cout << "\nYou Entered wrong Choice";
    }
    cout << "\n\nPress (Y/y) To Continue... ";
    cin >> ch;
    cout << "\n";
 } while(ch == 'Y' || ch == 'y');
}

Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education