Stack ADT and linked list implementation.

#ifndef MYSTACK_H
#define MYSTACK_H
#include <iostream>
#include <stdlib.h>

using namespace std;

template <class Type>
class MyStack {

private:

// Declare data structure for linked list implementation of stack
struct MyStackNode {
Type data;
MyStackNode *next;
};

// Pointer to the top of stack
MyStackNode *top;

public:

// Constructor: initialize the stack

// Destructor: remove all elements from the stack

// isStackEmpty() - check if the stack is empty

// push()- push a new element to the stack

// pop() - pop an element from the stack

// view() - display the contents of the stack without removing the
// items

// peekStack() - display the top element without removing it


}; // MyStack

#endif // MYSTACK_H
I am trying to look some of the examples online , but I could not find one like mine
top won't be for the internal nodes. So that name might confuse you.
you could pull the struct out of the class. Then you could re-use it for a linked list or something.

all that aside, what is the trouble?

constructor...all it should do is set top to nullptr, I think.

push .. start here. Temp = new stacknode, temp.next = top (notice how this will fix itself if top is null!), top = temp I think is what you want. Data = data of course, don't forget the data.

after you think you have push correct, try view(). If you get both of those working, view will display what you put into validate push. From there its more of the same...

The problem is I am not allowed to change the skeleton of the header file .
Plus , I have to provide all the functions here , then I have to call the header file in the main cpp .
Topic archived. No new replies allowed.