Question

In: Computer Science

C++, ArrayStack, data structure. Make a main using the following arraystack structure the main most do...

C++, ArrayStack, data structure.

Make a main using the following arraystack structure the main most do the following Stack: Stack 1 and Stack 2 most have their own result

I need to see the state of the stack1 and stack2 after doing the following operation. (stack1 and stack2 need to have individually there own state result)

stack1.push(1)
stack1.push(2)
stack2.push(3)
stack2.push(4)
stack1.pop()
stackTop = stack2.peek()
stack1.push(stackTop)
stack1.push(5)
stack2.pop()
stack2.push(6)

ArrayStackInterface.h

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file StackInterface.h */

#ifndef _STACK_INTERFACE
#define _STACK_INTERFACE

template<class ItemType>
class StackInterface
{
public:
/** Sees whether this stack is empty.
@return True if the stack is empty, or false if not. */
virtual bool isEmpty() const = 0;

/** Adds a new entry to the top of this stack.
@post If the operation was successful, newEntry is at the top of the stack.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful or false if not. */
virtual bool push(const ItemType& newEntry) = 0;

/** Removes the top of this stack.
@post If the operation was successful, the top of the stack
has been removed.
@return True if the removal is successful or false if not. */
virtual bool pop() = 0;

/** Returns the top of this stack.
@pre The stack is not empty.
@post The top of the stack has been returned, and
the stack is unchanged.
@return The top of the stack. */
virtual ItemType peek() const = 0;
}; // end StackInterface
#endif

ArrayStack.h

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** ADT stack: Array-based implementation.
Listing 7-1
@file ArrayStack.h */

#ifndef _ARRAY_STACK
#define _ARRAY_STACK

#include "StackInterface.h"

const int MAX_STACK = 5;

template<class ItemType>
class ArrayStack : public StackInterface<ItemType>
{
private:  
   ItemType items[MAX_STACK]; // Array of stack items
   int top; // Index to top of stack
  
public:
   ArrayStack(); // Default constructor
   bool isEmpty() const;
   bool push(const ItemType& newEntry);
   bool pop();
   ItemType peek() const;  
}; // end ArrayStack

#include "ArrayStack.cpp"
#endif

ArrayStack.cpp

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 7-1
@file ArrayStack.cpp */
#include <cassert> // For assert
#include "ArrayStack.h" // Header file

template<class ItemType>
ArrayStack<ItemType>::ArrayStack() : top(-1)
{
} // end default constructor

// Copy constructor and destructor are supplied by the compiler

template<class ItemType>
bool ArrayStack<ItemType>::isEmpty() const
{
   return top < 0;  
} // end isEmpty

template<class ItemType>
bool ArrayStack<ItemType>::push(const ItemType& newEntry)
{
   bool result = false;  
   if (top < MAX_STACK - 1) // Does stack have room for newEntry?
   {
       top++;
       items[top] = newEntry;
       result = true;
   } // end if

   return result;
} // end push


template<class ItemType>
bool ArrayStack<ItemType>::pop()
{
   bool result = false;
   if (!isEmpty())
   {
       top--;
       result = true;
   } // end if

   return result;
} // end pop


template<class ItemType>
ItemType ArrayStack<ItemType>::peek() const
{
   assert(!isEmpty()); // Enforce precondition

   // Stack is not empty; return top
   return items[top];
} // end peek
// End of implementation file.

Solutions

Expert Solution

#######################################
      ArrayStack.cpp
#######################################
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 7-1
@file ArrayStack.cpp */
#include <cassert> // For assert
#include "ArrayStack.h" // Header file

template<class ItemType>
ArrayStack<ItemType>::ArrayStack() : top(-1)
{
} // end default constructor

// Copy constructor and destructor are supplied by the compiler

template<class ItemType>
bool ArrayStack<ItemType>::isEmpty() const
{
   return top < 0;  
} // end isEmpty

template<class ItemType>
bool ArrayStack<ItemType>::push(const ItemType& newEntry)
{
   bool result = false;  
   if (top < MAX_STACK - 1) // Does stack have room for newEntry?
   {
       top++;
       items[top] = newEntry;
       result = true;
   } // end if

   return result;
} // end push


template<class ItemType>
bool ArrayStack<ItemType>::pop()
{
   bool result = false;
   if (!isEmpty())
   {
       top--;
       result = true;
   } // end if

   return result;
} // end pop


template<class ItemType>
ItemType ArrayStack<ItemType>::peek() const
{
   assert(!isEmpty()); // Enforce precondition

   // Stack is not empty; return top
   return items[top];
} // end peek


template<class ItemType>
void ArrayStack<ItemType>::show() const
{
        for(int i=0; i<=top; i++) {
                std::cout << items[i] << " ";
        }
        std::cout << ":top " << std::endl;
} // end peek

// End of implementation file.



#######################################
        ArrayStack.h
#######################################
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** ADT stack: Array-based implementation.
Listing 7-1
@file ArrayStack.h */
#ifndef _ARRAY_STACK
#define _ARRAY_STACK

#include "StackInterface.h"

const int MAX_STACK = 5;

template<class ItemType>
class ArrayStack : public StackInterface<ItemType>
{
private:  
   ItemType items[MAX_STACK]; // Array of stack items
   int top; // Index to top of stack
  
public:
   ArrayStack(); // Default constructor
   bool isEmpty() const;
   bool push(const ItemType& newEntry);
   bool pop();
   ItemType peek() const;  
   void show() const;
}; // end ArrayStack

#endif



#######################################
    StackInterface.h
#######################################
#ifndef _STACK_INTERFACE
#define _STACK_INTERFACE

#include<iostream>

template<class ItemType>
class StackInterface
{
        public:
        /** Sees whether this stack is empty.
        @return True if the stack is empty, or false if not. */
        virtual bool isEmpty() const = 0;

        /** Adds a new entry to the top of this stack.
        @post If the operation was successful, newEntry is at the top of the stack.
        @param newEntry The object to be added as a new entry.
        @return True if the addition is successful or false if not. */
        virtual bool push(const ItemType& newEntry) = 0;

        /** Removes the top of this stack.
        @post If the operation was successful, the top of the stack
        has been removed.
        @return True if the removal is successful or false if not. */
        virtual bool pop() = 0;

        /** Returns the top of this stack.
        @pre The stack is not empty.
        @post The top of the stack has been returned, and
        the stack is unchanged.
        @return The top of the stack. */
        virtual ItemType peek() const = 0;
}; // end StackInterface
#endif



#######################################
            main.cpp
#######################################
#include <iostream>
#include "ArrayStack.h"
#include "ArrayStack.cpp"

using namespace std;

int main() {
        ArrayStack<int> stack1, stack2;
        stack1.push(1);
        stack1.push(2);
        stack2.push(3);
        stack2.push(4);
        stack1.pop();

        stack1.show();
        stack2.show();

        int stackTop = stack2.peek();
        stack1.push(stackTop);
        stack1.push(5);
        stack2.pop();
        stack2.push(6);

        cout << endl;
        stack1.show();
        stack2.show();
}



**************************************************
Please write the way i have given code.. Please put the code of respective files in their own file as shown, else compilation errors may come.

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

C++, ArrayStack. Make a main using the following arraystack structure the main most do the following...
C++, ArrayStack. Make a main using the following arraystack structure the main most do the following Stack: Stack 1 and Stack 2 most have their own result stack1.push(1) stack1.push(2) stack2.push(3) stack2.push(4) stack1.pop() stackTop = stack2.peek() stack1.push(stackTop) stack1.push(5) stack2.pop() stack2.push(6) ArrayStackInterface.h // Created by Frank M. Carrano and Tim Henry. // Copyright (c) 2013 __Pearson Education__. All rights reserved. /** @file StackInterface.h */ #ifndef _STACK_INTERFACE #define _STACK_INTERFACE template<class ItemType> class StackInterface { public: /** Sees whether this stack is empty. @return...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
C++ (data structure using c++). please send me copyable file. Write a program and test a...
C++ (data structure using c++). please send me copyable file. Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble...
so the assigment is is a data strucutre using c++ to make a doubly linked list...
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code...
Create your own data type using a C++ structure. Assign values to a variable of that...
Create your own data type using a C++ structure. Assign values to a variable of that data type. Retrieve values from the variable of that data type. Description: In this lab you will choose the statements to create a program that: Creates a brand new data type called "Monster". Once created, creates a variable of type Monster and assign values to the variable and then display the contents. This lab will again take the form of a guided multiple-choice quiz...
C++ - Checks the relevance of a data structure in terms of following the interface specification...
C++ - Checks the relevance of a data structure in terms of following the interface specification for an ADT that represents a linear data structure: Depending on the ADT of linear data structure, you must create the operations CRUD (Create, Read (search), Update, Delete) elements in the data structure. Some operations do not apply for certain data structures Create: Description: Insert an element in the data structure (create) according to the access policy of the structure Input:Data structure and element...
****C++, put both of them in to one main method, make sure to start with main...
****C++, put both of them in to one main method, make sure to start with main class, if I get a screenshot of the output as well. thank you (1) A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the...
****C++, put both of them in to one main method, make sure to start with main...
****C++, put both of them in to one main method, make sure to start with main class, if I get a screenshot of the output as well. thank you (3) Use singly linked lists to implement integers of unlimited size. Each node of the list should store one digit of the integer. You should implement addition, subtraction, multiplication, and exponentiation operations. Limit exponents to be positive integers. What is the asymptotic running time for each of your operations, expressed in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT