Question

In: Computer Science

Write an implementation of the ADT stack that uses a resizeable array to represent the stack...

Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array.

Use c++ to write this code.

Solutions

Expert Solution

#include <iostream>

using namespace std;

/*
* "Stack" class is the implementation of the abstract data type - Stack
* It uses a resizable array to represent the stack elements, anytime
the stack becomes full, the size of the array representing the stack has to be doubled.
* This particular implementation assumes that only positive int are
   inserted into the stack.
*/

/* API interface for the class
1. "Stack" class represents the ADT
2. Attributes:
(a) size_of_stack (stores the current length of the stack[] array.)
(b) num_of_elems_in_stack (stores number of elements in the stack)
3. Methods:
(a) push(elem)
(b) pop()
(c) return_top()
(d) return_status_of_stack()
*/

class Stack {
   public:
       // class atributes
       int size_of_stack;
       int num_of_elems_in_stack;
       int* stack_arr = NULL;

       Stack(int top_elem){ // this is the constructor that initializes the stack
           stack_arr = new int[1]; // Allocate 1 int and save ptr in stack_arr.
           stack_arr[0] = top_elem;
           size_of_stack = 1;
           num_of_elems_in_stack = 1;
       }

       // this method is used as a helper function to move contents within the stack array
       void shift(int left_or_right){
           // left_or_right = 0, i.e. shift left
           if (left_or_right==0)
           {
               for (int i = num_of_elems_in_stack-1; i >0 ; --i)
               {
                   stack_arr[i-1] = stack_arr[i];
               }
               num_of_elems_in_stack-=1;
           }
           // left_or_right = 1, i.e. shift right
           else
           {
               for (int i = 0; i < num_of_elems_in_stack; ++i)
               {
                   stack_arr[i+1] = stack_arr[i];
               }
               num_of_elems_in_stack+=1;
           }
       }

       // This method pushes elements on to the stack by appropriately checking the
       // end conditions
       void push(int elem){
           // Case 1: size of stack = num of elems in stack
           // => insertion will cause overflow. So, need to resize array
           if (size_of_stack == num_of_elems_in_stack)
           {
               int* temp = stack_arr;
               stack_arr = new int[2*num_of_elems_in_stack];

               stack_arr[0] = elem;
               for (int i = 1; i < num_of_elems_in_stack+1; ++i)
               {
                   stack_arr[i] = temp[i];
               }

               delete [] temp; // free memory pointed to by temp;
               num_of_elems_in_stack+=1;
               size_of_stack = 2*num_of_elems_in_stack;
           }  

           // Case 2: size of stack > num of elems in stack
           // => insertion will not cause overflow.
           else
           {
               shift(1); // shift right
               stack_arr[0] = elem;
           }

       }

       // This method pops elements from the stack by appropriately checking the
       // end conditions
       int pop(){
           if (num_of_elems_in_stack==0)
           {
               cout<<"Underflow Error"<<endl;
               return -1;
           }
           else{
               int top = stack_arr[0];
               shift(0);// pop the element of the stack
               return top;
           }
       }

       // this method returns top of the stack(if stack not empty)
       int return_top(){
           if (num_of_elems_in_stack==0)
           {
               cout<<"No elements in the stack"<<endl;
               return -1;
           }
           else
               return stack_arr[0];
       }

       int return_status_of_stack(){
           if (num_of_elems_in_stack!=0)
               cout<<"Top of stack: "<<stack_arr[0]<<endl;

           cout<<"Size of the stack: "<<size_of_stack<<endl;
           cout<<"Number of elements in stack: "<<num_of_elems_in_stack<<endl;
       }
};

int main(int argc, char const *argv[])
{
   cout<<endl;
   cout<<"Checking stack init"<<endl;
   Stack s(100);
   s.return_status_of_stack();

   // testing push method
   cout<<endl;
   cout<<"Check push method: "<<"elems inserted 230,50,70"<<endl;
   s.push(230);
   s.push(50);
   s.push(70);
   s.return_status_of_stack();

   // testing pop method
   cout<<endl;
   cout<<"Checking popped element"<<endl;
   cout<<"Popped element: "<<s.pop()<<endl;
   s.return_status_of_stack();

   cout<<endl;
   cout<<"End of testing"<<endl;
   return 0;
}


Related Solutions

write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items.
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array.
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. In Java please.
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a...
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a linked list-based queue ADT that implements the provided QueueInterface.java You may not make any changes to StackInterface.java or QueueInterface.java The classes must be generic The attached generic singly linked list node class, LLNode.java, must be used for the queue implementation; you may not make any modifications to this class Your implementations cannot throw any exceptions Each ADT must include a toString( ) method: The...
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What...
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What is the running time for each operation?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT