Question

// Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

// Header code for stack // requesting to create source code using C++

#ifndef Stack_h

#define Stack_h

#include <stdio.h>

#include <string>

#include <iostream>

using namespace std;

class Stack

{

public:

Stack();

~Stack();

bool empty();

string top();

void push(const string &val);

void pop();

void display(ostream &out);

private:

class Node

{

public:

string word;

Node *next;

};

Node *tos;

};

#endif

Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in the header file, pls provide the code. For this project, it has to be linked list method using pointers, not array method stack. Please provide codes clearly.

Stack::Stack()

{

//create a constructor according to header file

}

Stack::~Stack()

{

//create a destructor according to header file

}

bool empty()

{

//Check if the stack is empty using Linked list method with pointers (!!not array type)

}

string top()

{

//Read the top of the stack // use pointers

}

void push(const string &val)

{

//store the data into top of the stack // use pointers //Linked list method // Not array

}

void pop()

{

//Remove or pop the data from the top of the stack and point to the next data stored in the stack //point to NULL if nothing left   

}

void display(ostream &out)

{

// print everything stored in the stack

}

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

Stack.h

#ifndef Stack_h

#define Stack_h

#include <stdio.h>

#include <string>

#include <iostream>

using namespace std;

class Stack

{

public:

Stack();

~Stack();

bool empty();

string top();

void push(const string &val);

void pop();

void display(ostream &out);

private:

class Node

{

public:

string word;

Node *next;

};

Node *tos;

};

#endif




==================================================================
Stack.cpp

#include "Stack.h"

Stack::Stack()

{

//create a constructor according to header file

tos = NULL;

}

Stack::~Stack()

{

//create a destructor according to header file

delete tos;

}

bool Stack::empty()

{

return tos==NULL;

}

string Stack::top()

{

return tos->word;

}

void Stack::push(const string &val)

{

Node* temp;

temp = new Node();

temp->word = val;

temp->next = tos;

tos = temp;


}

void Stack::pop()

{

Node* temp;

if (tos == NULL)

cout << "The stack is Empty" << endl;

else {

temp = tos;

cout << "Element has been Popped " << temp->word << endl;

tos = tos->next;

}

}

void Stack::display(ostream &out)

{

Node* pointer;

pointer = tos;

if (tos == NULL)

return;

else {

while (pointer != NULL) {

cout << pointer->word << endl;

pointer = pointer->next;

}

}

}



=======================================================================
#include <iostream>

#include <cstdlib>

using namespace std;

#include "Stack.h"

int main() {

int pick;

Stack bar;

string object;

while (1) {

cout << "Press #1. Push" << endl;

cout << "Press #2. Pop" << endl;

cout << "Press #3. Elements" << endl;

cout << "Press #4. Exit" << endl;

cout << "Enter an option";

cout << " \n " << endl;

cin >> pick;

switch (pick) {

case 1:

cout << "Enter value to be pushed into the stack: ";

cin >> object;

bar.push(object);

break;

case 2:

bar.pop();

break;

case 3:

bar.display(std::cout);

break;

case 4:

exit(0);

break;

default:

cout << "Invalid Option" << endl;

}

}

return 0;

}

======================================================================
See OUTPUT


Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
// Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...
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
  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • 1. Here are codes to define a stack class based on dynamic array, please complete the...

    1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available                         // copy original's array member into this new array {              // Please complete the function here        } else {          cerr << "*Inadequate memory to allocate...

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • Write a program that opens a text file called input.txt and reads its contents into a...

    Write a program that opens a text file called input.txt and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file called output.txt. The order of the characters saved in the second file should be the reverse of their order in the first file. We don’t know the text file length. Input.txt This is a file is for test output.txt tset rof si elif...

  • 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