I have a class definition for Node, LinkedList and Stack (stack of ints). I want to implement the stack using a linked list object but need to learn how it is initialized within the dynamic stack class definition. I don't think I need a top pointer since LinkedList class has a head and tail pointer.
I was wondering in this case, do I need any private members for class Stack at all?
#pragma once
#include "LinkedList.h"
// Specification file for the Stack class
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H
class Stack
{
private:
LinkedList list;//VS is telling me this is necessary to make list functions accessible in the
//public member methods in this class definition of Stack.
public:
// Constructor
Stack()
{
LinkedList::LinkedList();//call constructor of LinkedList.
}
// Destructor
~Stack()
{
list.~LinkedList();//destructor for LinkedList.h
}
// Stack operations
void push(int num)
{
//call the prepend member function of LL
list.prepend(num);
}
void pop(int &num)
{
//call the removeHead member function of LL
list.removeHead();
//code to catch the removed value and return it. num=something
}
bool isEmpty()
{
//call the empty member function of LL
list.empty();
//var to catch returning value if LL is empty and code to return it
}
};
#endif
Just calling constructor of LinkedList object in the Stack constructor.
list is already a member of Stack, so the default constructor is used automatically, the destructor too, so you don't need to mention them in the Stack constructor/destructor. So line 19 and 25 need to removed.
Loot at it this way. C has some built in types, int, char, float and so on. C++ allows you to create user defined types. Somewhere, you've found a type LinkedList and used that to implement a your own Stack.
Just because Stack is implemented with LinkedList, doesn't mean that anyone using Stack can see that. From the outside, there's no relationship between the two types. If you want a stack, use Stack stack;, but LinkedList* stack doesn't make a whole lot of sense.