So, I am trying to implement a stack using a linked list as the parent, and have written these two classes:
template <class T>
class LL {
public:
struct Node {
Node* next = nullptr;
T data = 0;
};
Node* head;
public:
LL();
~LL();
void insert(T item);
void append(T item);
virtual void disp();
};
template<class T>
LL<T>::LL(){ head = nullptr; }
template<class T>
void LL<T>::insert(T item)
{
Node* newNode = new Node;
newNode->data = item;
if (head == nullptr) head = newNode;
else
{
Node* loop = head;
Node* prev = nullptr;
while (loop != nullptr && loop->data < item) {
prev = loop;
loop = loop->next;
}
if (prev == nullptr) { //head
newNode->next = head;
head = newNode;
}
else { //insert
newNode->next = prev->next;
prev->next = newNode;
}
}
}
template<class T>
void LL<T>::append(T item)
{
Node* newNode = new Node;
newNode->data = item;
if (head == nullptr) head = newNode;
else
{
Node* loop = head;
while(loop->next != nullptr)
loop = loop->next;
loop->next = newNode;
}
}
template<class T>
void LL<T>::disp()
{
cout << "[";
if (head) {
if (head->next == nullptr)
cout << head->data;
else {
Node* loop = head;
while (loop->next != nullptr)
{
cout << loop->data << ",";
loop = loop->next;
}
cout << loop->data;
}
}
cout << "]" << endl;
}
template<class T>
LL<T>::~LL()
{
if (head != nullptr) {
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
temp = nullptr;
}
}
}
template<class T>
class stack: public LL<T> {
private:
public:
stack();
~stack();
virtual void push(T item);
void pop(T ret);
bool empty();
void disp();
};
template<class T>
stack<T>::stack():LL<T>()
{
}
template<class T>
stack<T>::~stack()
{
}
template<class T>
void stack<T>::push(T item){ this->append(item); }
template<class T>
void stack<T>::pop(T ret)
{
ret = this->head->data;
this->head = this->head->next;
}
template<class T>
bool stack<T>::empty() { return false;}
template<class T>
void stack<T>::disp()
{
Node<T>* loop = this->head;
cout << "-" << endl;
while (loop) {
cout << loop->data << endl;
loop = loop->next;
}
cout << "-";
}
The trouble comes in the very last function, "disp()", where it does not give me access to the Node struct. It gives me the error "'Node': identifier not found". This prevents me from declaring the needed "loop" variable. As far as I know, the Node struct should be inherited from the LL class and protected in the stack class. Thus, I should be able to access it directly.
Any ideas?