Question

C++ Data Structures Write the C++ (not just pseudocode) for a client program that creates a...

C++ Data Structures

Write the C++ (not just pseudocode) for a client program that creates a stack of the strings "Jamie", "Jane" and "Jill" in that order with "Jamie" at the top of the stack, "Jane" next and "Jill" at the bottom. Peek at each item and then pop it off the stack to show Jamie is at the top, Jane next and Jill at the bottom.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

using namespace std;

class stack {
   private:
       string *arr;
       int top;
       int size;
   public:
       stack(){
           arr = new string[10];
           top = -1;
           size = 10;
       }

       stack(int n) {
           arr = new string[n];
           top = -1;
           size = n;
       }

       void push (string s) {
           if (top == size-1)
               return;
           arr[++top] = s;
       }

       string pop () {
           if (top >= 0)
               return arr[top--];
           return NULL;
       }

       string peek () {
           if (top >= 0)
               return arr[top];
           return NULL;
       }
};

int main() {
   stack mystack;
   mystack.push("Jill");
   mystack.push("Jane");
   mystack.push("Jamie");

   cout << "top: " << mystack.peek() << endl;
   cout << "Pop from stack.." << endl;
   mystack.pop();
   cout << "top: " << mystack.peek() << endl;
   cout << "Pop from stack.." << endl;
   mystack.pop();
   cout << "top: " << mystack.peek() << endl;
}

Here you go champ.. Personalized array based stack implementation for you... Hope you like it. Let me know if you need any changes.

Add a comment
Know the answer?
Add Answer to:
C++ Data Structures Write the C++ (not just pseudocode) for a client program that creates a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ========================Data Structure C++============================ 1. Write a CLIENT program that creates a stacks fo type int (so...

    ========================Data Structure C++============================ 1. Write a CLIENT program that creates a stacks fo type int (so one stack instance):  aStack. Ask the user for 5 ints and push them onto aStack. Find a way to print the values in aStack in from the bottom to the top (so display the top LAST) - so if aStack was 1,2,3,4,5 with 5 at the top and 1 at the bottom you want it to display 1,2,3,4,5 (you don't want it to display 5,4,3,2,1...

  • 1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push(...

    1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push( "Jane" ); push( "Jess" ); push( "Jill" ); push( pop() ); push( "Jim" ); String name = pop(); push( peek() ); Write separate programs for each of the data structures Stack, Queue, PriorityQueue, and List. Use the appropriate push(), pop(), peek() for each of the respective ADT's. Use the Java class library of the ADT's as opposed to the author's implementation. What is in...

  • C++ Using the Stack operations, write a pseudocode routine, dupA, that takes aStack for string, checks...

    C++ Using the Stack operations, write a pseudocode routine, dupA, that takes aStack for string, checks to see if the top starts with ‘A’ or ‘a’. If so, duplicate the top of the stack (i.e. push a copy of that value onto the stack) else if length > 10, pop it off the stack

  • C++ visual studio program...Write your own version of a class template that will create a dynamic...

    C++ visual studio program...Write your own version of a class template that will create a dynamic stack of any data type. The pop function must return a bool; it should return a false if it was not able to pop an item off the stack. Otherwise it returns true. The parameter to the pop function is passed by reference and should be the item on the list if it was able to pop something. Create a driver program (main) that...

  • Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack...

    Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack and use its classes and methods. Note that java.uitl.stack uses peek instead of top (as we did in class). Look up the documentation for java.uitl.stack to see what methods it contains and how to call them. Create a Stack of cards that will hold strings. Push new items to the stack (Use "Qclubs" for Queen of clubs and so on) After your 'deck' has...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...

    Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that passes in a stack and returns (using a return statement) the number of items in the stack (the stack size). a. assume the this function is to be toolkit function in the implementation of the ADT stack. b. assume that this function is not a toolkit function. 2. Given the declaration: s = stack i = item struct STACK { INFO_RC i; int top; }...

  • Need help. write a C program stack-ptr.c that implements a stack using a link list. Below...

    Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly. examplpe: push(5, &top); push(10, &top); push(15, &top); int value = pop(&top); value = pop(&top); value = pop(&top); this program currently has a race condition. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values. -use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT