Question: So using Microsoft visual studio I'm having trouble with this C++ problem. Please help me with my code. Here is the question: Here is my

So using Microsoft visual studio I'm having trouble with this C++ problem. Please help me with my code.

Here is the question:

So using Microsoft visual studio I'm having trouble with this C++ problem.Please help me with my code. Here is the question: Here ismy code: #include using namespace std; class Stack { int arr[27][2]; bool

Here is my code:

#include using namespace std;

class Stack {

int arr[27][2]; bool empty = true; int topIndex = -1; int count = 0;

public : //delete top element void pop() {

//return if empty if(empty) return;

count--;

//if only one element , then remove it and set isEmpty true if(arr[topIndex][1] == -1) { empty = true; topIndex = 0; arr[topIndex][0]=0; arr[topIndex][1]=0; return; }

int nextTop = arr[topIndex][1]; arr[topIndex][0] = 0; arr[topIndex][1] = 0; topIndex = nextTop; }

//return top element int top() { return arr[topIndex][0]; }

//push element at top void push(int value) {

//return if stack full if(count==26) return;

empty = false;

//increase count count++;

int randomIndex;

for(int i=1; i

arr[randomIndex][0] = value; arr[randomIndex][1] = topIndex; topIndex = randomIndex; }

//return isEmpty bool isEmpty() { return empty; }

//display all elements from top to bottom void display() { if(empty) return;

int index = topIndex; while(index!=-1) { cout

//check if index is valid to add number or not bool isValid(int index) { if(arr[index][1]==0) return true;

return false; } }; //main program int main() {

Stack stack; stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.push(5); stack.push(6); stack.push(7); stack.push(8); stack.push(9); stack.push(10); stack.display();

stack.push(20); stack.display();

stack.push(30); stack.display();

stack.push(40); stack.display();

cout

cout

cout

return 0; }

Eiecutive Summarv: Nodes in a linked list contain two parts: a data part that stores an element of the list; a next part that points to a node containing the successor of this list element or that is null if this is the last element in the list. This suggests that each node can be represented as a struct and the linked list can be represented as an array of structs You are asked to use 2D array to simulate the linked list and use it to construct the STACK with six basic functions: Construction, Empty, Push, Top, pop, and Display 1. Write the construction and five basic functions for the STACK listed below by using 2D array to simulate Linked List approach. Details for each function: Construction: construct an empty 2D array with capacity of 26 to simulate the OS 1. Empty: test if the STACK is empty 2. Push(element): Add a value at the top of the stack in a valid random space of the 2D array 3. Top():Read the value at the top of the stack 4. Pop): Remove the value at the top of the stack 5. Display Displays all the elements in the stack using from Top to Bottom ordering. (Show array index, data value, and next array index) After you finished the six functions, create an empty stack (with capacity-26) and then start push 10 values (1,2,3,4,5,6,7,8,9,10) by Push consecutively. After that, you will have 10 elements in your STACK. Run Displayto print out the STACK. Then perform 3 times of Push of value (20,30,40) on yalid positions, run Display() to print out the list every time you insert a new element. Then perform 3 times of Top0 and Pop(), again, run Display) to print our the list every time you delete an element To visualize the requirement, the code should looks like Push(1) Push(2) Push(3) Push(4) Push(5) Push(6) Push(7) Push(8) Push(9) Push(10) Display Push(20)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!