Question

In: Computer Science

Implement a stack in C++ using an array, not an array list. Make your stack size...

Implement a stack in C++ using an array, not an array list.

Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable.

DO NOT use Stack class defined in C++

Implement the following methods in your stack class.

  • stack() creates an empty stacks, stack s is new and empty.
  • push(item) adds a new item to the stack s, stacks is modified.
  • pop() removes and returns an item, stack s is modified.
  • isEmpty() returns a boolean and tests for an empty stacks, stack s is not modified.
  • isFull() returns a boolean and tests for an full stacks, stack s is not modified.
  • size() returns the int size of the stacks, stack s is not modified
  • print() prints the stacks from front to rear, stack s is not modified.
  • top() prints the front element, stack s is not modified.

Write your own Driver file to show that you have implemented all the methods of the stack. A psuedocode example is listed below to guide you. Use print statements as well to show the popped elements, to show the stack after some pushes, to print the size of the stack.

  • push(redShirt)
  • push(greenShirt)
  • push(yellowPants)
  • push(purpleSock)
  • push(pinkSocks)
  • printStack()
  • size()
  • push(blueShirt)
  • size()
  • pop()
  • pop()
  • size()
  • pop()
  • printStack()
  • top()
  • pop()
  • pop()
  • printStack()
  • size()
  • isEmpty()
  • printStack()

Solutions

Expert Solution


Given below is the code and output. Please do rate the answer if it helped. Thank you.

stack.h
--------
#ifndef STACK_H
#define STACK_H
#define STACK_SIZE 5
#include <iostream>
using namespace std;

template <typename T>
class Stack
{
private:
T data[STACK_SIZE];
int count;
public:
Stack(){
count = 0;
}

void push(T item){
if(!isFull())
data[count++] = item;
else
cout << "Stack is full. Can not push " << item << endl;
}

T pop(){
T dummy;
if(isEmpty()){
cout << "Stack empty. Can not pop!" << endl;
return dummy;
}
else{
return data[--count];
}
}

bool isEmpty(){
return count == 0;
}

bool isFull(){
return count == STACK_SIZE;
}

int size(){
return count;
}

void top(){
if(isEmpty()){
cout << "Stack empty" << endl;
}
else{
cout << "top = " << data[count-1] << endl;
}
}

void printStack(){
if(isEmpty()){
cout << "Stack empty" << endl;
}
else{
cout << endl << "Stack contents" << endl;
cout << data[count-1] << "<-top" << endl;
for(int i = count-2; i >= 0; i--)
cout << data[i] << endl;
}
cout << endl;
}
};


#endif

----
stacktest.cpp
------
#include <iostream>
#include "stack.h"
int main(){
Stack<string> shirts;
shirts.push("redShirt");
shirts.push("greenShirt");
shirts.push("yellowPants");
shirts.push("purpleSock");
shirts.push("pinkSocks");
shirts.printStack();
cout << "stack size = " << shirts.size() << endl;
shirts.push("blueShirt");
cout << "stack size = " << shirts.size() << endl;
cout << "popping " << shirts.pop() << endl;
cout << "popping " << shirts.pop() << endl;
cout << "stack size = " << shirts.size() << endl;
cout << "popping " << shirts.pop() << endl;
shirts.printStack();
shirts.top();
cout << "popping " << shirts.pop() << endl;
cout << "popping " << shirts.pop() << endl;
shirts.printStack();
cout << "stack size = " << shirts.size() << endl;
cout << "stack empty? " << shirts.isEmpty() << endl;
shirts.printStack();

return 0;
}


Related Solutions

in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
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...
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements....
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill in...
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...
In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
You are provided with a StackInterface to implement your stack, make sure to implement the data...
You are provided with a StackInterface to implement your stack, make sure to implement the data structure using java.util.ArrayList and naming your implementation ArrayListStack (make sure to use generics like you have been for the data structures so far). The provided StackTester assumes you name your implementation that way. Note that you have also been provided a PalindromeTester with the specific isPalindrome() method you are required to implement, following the approach above. The method should take whitespace, case-sensitivity, digits, and...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked List as a backing data structure Requirements Write the following structs struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; }; Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT