Linked List and Stack

profilestudent_01
Assignment_05.docx

COSC 2336 Homework Assignment 5

Due: Friday April 10, 2020

Write a program to do the following actions in the given order:

1. Create an empty stack

2. Push 100.0 to the stack

3. Push 200.0 to the stack

4. Pop the stack and display the value

5. Push 300.0 to the stack

6. Push 400.0 to the stack

7. Pop the stack and display the value

8. Pop the stack and display the value

9. Pop the stack and display the value

10. Pop the stack and display the value

Requirements:

· You must implement the stack using a linked list, not an array.

· You must implement at least 3 functions: push, pop, and isEmpty.

· The stack, which you create and operate on, should be an object of the above class.

· Your stack should notify the user an error message when the user tries to pop an empty stack.

You should see the result if your code works properly:

200

400

300

100

Error! Stack is empty.

Some code is provided below, so you can use it.

#include <iostream>

#include <assert.h>

using namespace std;

struct nodeType

{

double info;

nodeType * link;

};

class stack

{

private:

int stackSize; // current size of the stack

nodeType * stackTop; // pointer to the top of the stack

public:

void push(double data); // don't forget to increase stackSize

double pop(); // don't forget to decrease stackSize

bool isEmpty(); // returns true if the stack is empty

void initialize();

void destroyStack(); // destroy the stack

stack(); // constructor

~stack(); // destructor

};

void stack::initialize()

{

destroyStack();

stackTop = NULL;

}

stack::stack()

{

stackSize = 0;

stackTop = NULL;

}

void stack::destroyStack()

{

nodeType * toDelete;

while (stackTop != NULL)

{

toDelete = stackTop;

stackTop = stackTop->link;

delete toDelete;

}

stackSize = 0;

}

stack::~stack()

{

destroyStack();

}

void stack::push(double data)

{

// Logically, this stack will never overflow,

// so we can use a void function.

// Type your code here:

}

// Type your implementation of pop and isEmpty here: