Question

In: Computer Science

Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called...

Please complete absolutely follow the requirements. Thanks!

Implement a stack ADT by writing a class called Stack. Use a static array to hold stack elements. Instantiate the Stack class in the main function and provide a user loop and a menu so that all the Stack class member-functions, push, pop, etc., are available so that the user can thoroughly exercise the member-functions of the Stack class. Also, implement a ReversePrint() for the stack. My StackProject, whose exposition I have given you, already has implemented a Print(), as well as Push() and Pop(). Also, implement a Roll() operator, which, when executed, interchanges (i.e., swaps) the top most two elements in an instance of the stack class; that is, a stack object. Thus, roll interchanges the topmost element with the second element from the top of the stack. Ask yourself the question and answer it: what is the stack interface?

Solutions

Expert Solution

Greetings!!!!!!!

Please find the Program and few screen shots of sample run.



#include <iostream>


using namespace std;

#define MAXSize 10

class Stack 
{ 
    int top; 
public: 
     // Maximum size of the Stack 
    
    int stk[MAXSize];
    Stack() { top = -1; } 
    /* bool push(int element); 
    int pop(); 
    bool Roll();
    int peek(); 
    bool ReversePrint();
    bool isEmpty(); */
  
bool push(int element) 
{ 
    if (top >= (MAXSize - 1)) { 
        cout << "Stack is full (Overflow)"; 
        return false; 
    } 
    else { 
        top++;
        stk[top] = element; 
        return true; 
    } 
} 
  
int pop() 
{ 
    if (isEmpty()) 
    { 
        cout << "Stack is empty (Underflow)"; 
        return 0; 
    } 
    else 
    { 
        int element = stk[top]; 
        top--;
        return element; 
    } 
} 


  
bool isEmpty() 
{ 
    if (top<0)
    return true;
    else
    return false;
} 

  
bool ReversePrint() 
{ 
    if (isEmpty()) 
    { 
        cout << "Stack is empty (Underflow)"; 
        return false; 
    } 
    else
    {
        cout<<"\nElement of stack in reverse order are:";
        for(int loopcounter=top; loopcounter>=0; loopcounter--)
            cout<<" "<<stk[loopcounter];
        
        return true;
    }
    
} 

 bool Roll()
 {
     if(top<=0)
     { 
        
        return false; 
    } 
    else
    {
       int temp;
       temp=stk[top-1];
       stk[top-1]=stk[top];
       stk[top]=temp;
       cout <<"\nTop two elements " << stk[top] <<" and " << stk[top-1]<< " of the stack are rolled \n"; 
       return true;
    }
 }

}; 

int main()
{
   int choice, itemVal;
   Stack obj;
  
   do 
   {
   cout<<"\n\n---------------- Stack Interface-----------\n";
   cout<<"************** 1) Push in stack  *************\n";
   cout<<"************** 2) Pop from stack **************\n";
   cout<<"************** 3) Print in reverse order **************\n";
   cout<<"************** 4) Roll Opeation *****************\n";
   cout<<"************** 5) Exit ****************\n";
   
   cout<<"\nEnter choice: ";
   cin>>choice;
   
   switch(choice) 
   {
    case 1:
    {
            cout<<"\nEnter value to be pushed: ";
            cin>>itemVal;
            if(obj.push(itemVal))
            cout <<"\nElement " << itemVal << " is pushed into stack\n";
            break;
    }
    case 2:
    {
            int popElment;
            popElment=obj.pop();
             
            if(popElment!=0)
                cout <<"\nElement " << popElment << " is  popped from the stack\n";
            break;
    }
    case 3:
    {
            obj.ReversePrint();
            break;
    }
    case 4:
    {
        int indexRoll=obj.Roll();
           if(!indexRoll)
                cout << "\nStack contains less than two elements, So Roll operation is not possible"; 
                break;
    }
    case 5:
    {
          cout<<"Exit\n";
          exit(1);
          //break;
    }
    default:
    {
        cout<<"Invalid Choice"<<endl;
    }
  }
 }while(choice!=5);
  return 0;
}

Updated code as per comment (Contains main function at top) is as below.



#include <iostream>


using namespace std;

#define MAXSize 10

class Stack 
{ 
    int top; 
public: 
     // Maximum size of the Stack 
    
    int stk[MAXSize];
    Stack() { top = -1; } 
    bool push(int element); 
    int pop(); 
    bool Roll();
    bool ReversePrint();
    bool isEmpty(); 
}; 

int main()
{
   int choice, itemVal;
   Stack obj;
  
   do 
   {
   cout<<"\n\n---------------- Stack Interface-----------\n";
   cout<<"************** 1) Push in stack  *************\n";
   cout<<"************** 2) Pop from stack **************\n";
   cout<<"************** 3) Print in reverse order **************\n";
   cout<<"************** 4) Roll Opeation *****************\n";
   cout<<"************** 5) Exit ****************\n";
   
   cout<<"\nEnter choice: ";
   cin>>choice;
   
   switch(choice) 
   {
    case 1:
    {
            cout<<"\nEnter value to be pushed: ";
            cin>>itemVal;
            if(obj.push(itemVal))
            cout <<"\nElement " << itemVal << " is pushed into stack\n";
            break;
    }
    case 2:
    {
            int popElment;
            popElment=obj.pop();
             
            if(popElment!=0)
                cout <<"\nElement " << popElment << " is  popped from the stack\n";
            break;
    }
    case 3:
    {
            obj.ReversePrint();
            break;
    }
    case 4:
    {
        int indexRoll=obj.Roll();
           if(!indexRoll)
                cout << "\nStack contains less than two elements, So Roll operation is not possible"; 
                break;
    }
    case 5:
    {
          cout<<"Exit\n";
          exit(1);
          //break;
    }
    default:
    {
        cout<<"Invalid Choice"<<endl;
    }
  }
 }while(choice!=5);
  return 0;
}


  
bool Stack::push(int element) 
{ 
    if (top >= (MAXSize - 1)) { 
        cout << "Stack is full (Overflow)"; 
        return false; 
    } 
    else { 
        top++;
        stk[top] = element; 
        return true; 
    } 
} 
  
int Stack::pop() 
{ 
    if (isEmpty()) 
    { 
        cout << "Stack is empty (Underflow)"; 
        return 0; 
    } 
    else 
    { 
        int element = stk[top]; 
        top--;
        return element; 
    } 
} 


  
bool Stack::isEmpty() 
{ 
    if (top<0)
    return true;
    else
    return false;
} 

  
bool Stack::ReversePrint() 
{ 
    if (isEmpty()) 
    { 
        cout << "Stack is empty (Underflow)"; 
        return false; 
    } 
    else
    {
        cout<<"\nElement of stack in reverse order are:";
        for(int loopcounter=top; loopcounter>=0; loopcounter--)
            cout<<" "<<stk[loopcounter];
        
        return true;
    }
    
} 

 bool Stack::Roll()
 {
     if(top<=0)
     { 
        
        return false; 
    } 
    else
    {
       int temp;
       temp=stk[top-1];
       stk[top-1]=stk[top];
       stk[top]=temp;
       cout <<"\nTop two elements " << stk[top] <<" and " << stk[top-1]<< " of the stack are rolled \n"; 
       return true;
    }
 }







Related Solutions

1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
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...
Implement the stack ADT in a fully generic manner (through the use of templates) by means...
Implement the stack ADT in a fully generic manner (through the use of templates) by means of a singly linked list. (Give your implementation “from scratch,” without the use of any classes from the Standard Template Library ).
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be...
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be used are the ones defined in the Queue class. class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0, item) def dequeue(self): return self.items.pop() def size(self): return len(self.items) The codes to fill: """ 1. Stack2 class Implement stack data structure using queue """ class Stack2: def __init__(self): # Write your definition for __init__ here def isEmpty(self): #...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure. Related codes: public interface Stack<E> { int size( ); boolean isEmpty( ); void push(E e); E top( ); E pop( ); } public class LinkedStack<E> implements Stack<E> { private SinglyLinkedList<E> list = new SinglyLinkedList<>( );    public LinkedStack( ) { }    public int size( ) { return...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT