Question

In: Computer Science

C++ ONLY -- PRACTICE ASSIGNMENT We are given a code (Driver.cpp) and asked to create a...

C++ ONLY -- PRACTICE ASSIGNMENT

We are given a code (Driver.cpp) and asked to create a file (StringStack.h) and turn it in.

What should the code for StringStack.h look like based on the instructions below?

  1. Download Driver.cpp
  2. Create a class named StringStack in a file named StringStack.h.
  3. Create a ListNode structure as a private member of the class. The node should be able to hold a string called value.  
  4. Create a top pointer as private attributes of the class. (this is like the head pointer for a linked list)
  5. Create the following public member functions:
    1. A constructor, which will set top to NULL.
    2. A push function, which will push a string on top of the stack.
    3. A pop function, which will remove & return the string from the top of the stack

6. Test your stack class using the Driver.cpp file provided for you.

GIVEN DRIVER.CPP FILE:

#include <iostream>

#include "StringStack.h"

using namespace std;

int main()

{

// Define a StringStack object.

StringStack stack;

string animal, lastAnimal;

//get input from user and push to the stack

cout << "\n\nWhat did the old lady eat first? ";

getline(cin, animal);

stack.push(animal);

for(int x=0; x < 6; x++)

{

cout << "\nWhat did she eat next? ";

getline(cin, animal);

stack.push(animal);

}

cout << "\nWhat did she eat last? ";

getline(cin, animal);

stack.push(animal);

//start popping from stack and print results

lastAnimal = stack.pop();

animal = stack.pop();

cout << "\n\nShe swallowed the " << animal << " to catch the ";

animal = stack.pop();

cout << animal << ",\nShe swallowed the " << animal << " to catch the ";

animal = stack.pop();

cout << animal << ",\nShe swallowed the " << animal << " to catch the ";

animal = stack.pop();

cout << animal << ",\nShe swallowed the " << animal << " to catch the ";

animal = stack.pop();

cout << animal << ",\nShe swallowed the " << animal << " to catch the ";

animal = stack.pop();

cout << animal << "\nThat wriggled and jiggled and tickled inside her!\n";

cout << "She swallowed the " << animal << " to catch the ";

animal = stack.pop();

cout << animal << ";\nI don\'t know why she swallowed a " << animal;

cout << " - Perhaps she\'ll die!\n\n";

cout << "There was an old lady who swallowed a " << lastAnimal;

cout << ";\n...She\'s dead, of course!\n\n";

return 0;

}

Solutions

Expert Solution

Code to be pasted in stringStack.h

#pragma once
#include"string"
using namespace std;
class StringStack
{
private:
        struct ListNode
        {
                string str;
                ListNode *next;
        };
        ListNode *headPtr;
public:
        StringStack()
        {
                headPtr = NULL;
        }

        void push(string inp)
        {
                ListNode *temp = new ListNode; /*create a node of type list node and initialize its str*/
                temp->str = inp;
                temp->next = headPtr;  /* insert at beginning of list*/
                headPtr = temp; 
        }

        string pop()
        {
                if (headPtr == NULL) /* if stack is empty return empty string*/
                {
                        return "";
                }
                ListNode * temp = headPtr;
                headPtr = headPtr->next;   /* advance headptr to next node to delete the first node*/
                string tempStr = temp->str;  /* store string in node to tempStr. It is done to return it*/
                delete temp;  /* release memory using delete*/
                return tempStr;
        }
};


Related Solutions

In C++ with comments in code with screenshot In this assignment, you are asked to create...
In C++ with comments in code with screenshot In this assignment, you are asked to create a class called Account, which models a bank account. The requirement of the account class is as follows, (1) It contains two data members: accountNumber and balance, which maintains the current account name and balance, respectively. (1) It contains three functions: functions credit() and debit(), which adds or subtracts the given amount from the balance, respectively. The debit() function shall print ”amount withdrawn exceeds...
C programming assignment. instructions are given below and please edit this code only. also include screenshot...
C programming assignment. instructions are given below and please edit this code only. also include screenshot of the output //In this assignment, we write code to convert decimal integers into hexadecimal numbers //We pratice using arrays in this assignment #include <stdio.h> #include <stdlib.h> #include <assert.h> //convert the decimal integer d to hexadecimal, the result is stored in hex[] void dec_hex(int d, char hex[]) {    char digits[] ={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',   ...
In this assignment, you are given partial code and asked to finish the rest according to...
In this assignment, you are given partial code and asked to finish the rest according to the specified requirement. Problem statement: one benefit of inheritance is the code reuse. Instead of writing everything from scratch, you can write a child class that reuse all the features already existed in its parent, grandparent, great grandparent, etc. In the child class, you only need to supply small amount of code that makes it unique. In the following, you are given code for...
Needed in C++ In this assignment, you are asked to create a class called Account, which...
Needed in C++ In this assignment, you are asked to create a class called Account, which models a bank account. The requirement of the account class is as follows (1) It contains two data members: accountNumber and balance, which maintains the current account name and balance, respectively. (1) It contains three functions: functions credit() and debit(), which adds or subtracts the given amount from the balance, respectively. The debit() function shall print ”amount withdrawn exceeds the current balance!” if the...
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This...
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This assignment will give you experience on the use of the while loop and the for loop. You will use both selection (if) and repetition (while, for) in this assignment. Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number...
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
Below is the C ++ code for a given assignment. An acre equal 43,560 sq ft....
Below is the C ++ code for a given assignment. An acre equal 43,560 sq ft. Im supposed to find the total number of acres within a tract of land that is 391,876 sq ft. the program compiles and runs just fine but I'm wondering if it would be better to use the int data type instead? I used double for more accuracy. The instructor didn't say we had to use any certain data type so I'm asking purely for...
Im asked to edit the C++ code given to me so that the user can enter...
Im asked to edit the C++ code given to me so that the user can enter as many courses as they would like. I've already built the array to house the courses, but Im a tiny bit stuck on writing the user input to the array every time a new line Is made. Code: //CSC 211 Spring 2019 //Program Description: // // Originally From: // CSC 211 Spring 2018 // Dr. Sturm // This program... // #include<iostream> #include<string> #include<fstream> using...
C++ ONLY -- LAB ASSIGNMENT DIFFICULT We have to turn in two files - List.h and...
C++ ONLY -- LAB ASSIGNMENT DIFFICULT We have to turn in two files - List.h and Lab5.cpp What should the code for those two files look like? INSTRUCTIONS AS FOLLOWS: What Should This Program Do? Linked List Class Design your own linked list class (List.h) to hold a series of strings. The linked list node should be implemented as a struct. The class should have member functions for appending, inserting, and deleting nodes. You should also have a display function...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT