I'm trying to implement my own Stack in C++ but I keep getting this error when I try to use the method pop() in which what I'm trying to do is:
- Save element from the top in a variable called "res".
- Get the reference to the next element from the node class and set it as the top.
- size--
- Return the variable "res".
If you could help me I'd appreciate it. Thank you!
Node class:
#include <iostream>
using namespace std;
template<class T>
class Node {
private:
Node<T>* next;
T element;
public:
Node();
Node(const Node& orig);
~Node();
void setElement(T el);
T getElement();
Node<T>* getNext();
void setNext(Node<T>* ne);
};
template<typename T>
Node<T>::Node() {
next = nullptr;
}
template<typename T>
Node<T>::Node(const Node& orig) {}
template<typename T>
Node<T>::~Node() {}
template<typename T>
void Node<T>::setElement(T el) {
element = el;
cout << element << endl;
}
template<typename T>
T Node<T>::getElement() {
return element;
}
template<typename T>
Node<T>* Node<T>::getNext() {
return next;
}
template<typename T>
void Node<T>::setNext(Node<T>* ne) {
next = ne;
}
Stack class:
#include "EmptyStackException.cpp"
#include "Node.cpp"
#include <iostream>
using namespace std;
template<class T>
class LinkedStack {
private:
int siz;
Node<T>* first;
public:
LinkedStack();
~LinkedStack();
int size();
bool isEmpty();
void push(T e);
T top();
T pop();
};
template<class T>
LinkedStack<T>::LinkedStack() {
first = nullptr;
siz = 0;
}
template<class T>
LinkedStack<T>::~LinkedStack() {
}
template<class T>
int LinkedStack<T>::size() { return siz; }
template<class T>
bool LinkedStack<T>::isEmpty() { siz == 0; }
template<class T>
void LinkedStack<T>::push(T e) {
Node<T> node = Node<T>();
node.setNext(first);
node.setElement(e);
first = &node;
siz++;
}
template<class T>
T LinkedStack<T>::top() {
//to do after...
}
template<class T>
T LinkedStack<T>::pop() {
T res = first->getElement();
first = first->getNext();
siz--;
}
Aucun commentaire:
Enregistrer un commentaire