In: Computer Science
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.
####################################### 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.