please don't dig into me too hard, I am still steadily learning and ran into an issue when trying to construct an AVL tree. When iterating through the tree on insert, I go until I reach a nullptr, create a new node, and assign that ptr to the nullptr. The value is never accepted though. Can someone find the error and explain it to me? ty!
#ifndef AVLTree_hpp
#define AVLTree_hpp
#include <stdio.h>
#include <stack>
template<typename T>
class AVLTree{
private:
struct Node{
T val;
Node* left;
Node* right;
int height;
Node(T V)
:left{nullptr},right{nullptr}
{
val = V;
}
~Node(){
}
};
Node* head;
void rightRotate(Node*& node);
void leftRotate(Node*& node);
void leftRight(Node*& node);
void rightLeft(Node*& node);
public:
AVLTree();
~AVLTree();
AVLTree(const AVLTree &c);
AVLTree(AVLTree &&c);
AVLTree &operator=(const AVLTree &c);
AVLTree &operator=(AVLTree &&c);
void add(T value);
int getHeight(Node* n);
};
template <typename T>
AVLTree<T>::AVLTree()
:head{nullptr}{
}
template <typename T>
AVLTree<T>::~AVLTree(){
}
template <typename T>
void AVLTree<T>::rightRotate(Node*& node){
Node* temp = node;
node = node->left;
Node* leftLL = node->right;
temp->left = leftLL;
node->right = temp;
}
template <typename T>
void AVLTree<T>::leftRotate(Node*& node) {
Node* temp = node;
node = node->right;
Node* yL = node->left;
temp->right = yL;
node->left = temp;
}
//left right condition
template <typename T>
void AVLTree<T>::leftRight(Node*& node) {
leftRotate(node->left);
rightRotate(node);
}
//right left condition
template <typename T>
void AVLTree<T>::rightLeft(Node*& node){
rightRotate(node->right);
leftRotate(node);
}
template <typename T>
void AVLTree<T>::add(T value){
if(head==nullptr){
head = new Node(value);
return;
}
std::stack<Node*> st;
Node* it = head;
while(it!=nullptr){
st.push(it);
if(value <= it->val){
it = it->left;
}else{
it=it->right;
}
}
//here is where the it is not assigned to the new node pointer.
//I have tested it and the node is created, "it" just does not hold the value at any point.
it = new Node(value);
int count = 0;
while(!st.empty()){
int balance = getHeight(st.top()->left) - getHeight(st.top()->right);
if(balance > 1){
if(st.top()->left!= nullptr&&st.top()->left!=nullptr){
leftRotate(st.top());
}else{
leftRight(st.top());
}
}else if(balance<-1){
if(st.top()->right!=nullptr&&st.top()->right!=nullptr){
rightRotate(st.top());
}else{
rightLeft(st.top());
}
}
st.pop();
if(++count==4){
break;
}
}
}
template <typename T>
int AVLTree<T>::getHeight(Node* n){
int max =0;
if(n!=nullptr){
max = std::max(getHeight(n->left),getHeight(n->right))+1;
}
return max;
}
#endif /* AVLTree_hpp */
It is a copy of the pointer and updating it has no effect on the original pointer. You need to do something like this instead:
Node* it = head;
bool left = true;
while(it!=nullptr){
st.push(it);
left = value <= it->val;
if(left){
it = it->left;
}else{
it=it->right;
}
}
it = new Node(value);
if (left){
stack.top()->left = it;
} else {
stack.top()->right = it;
}
Consider this simplified version of your code:
#include <iostream>
struct Linked {
Linked* next;
};
int main(int argc, char** argv) {
Linked l0 {nullptr};
// Case 1: Does not work
std::cout << "case 1" << std::endl;
Linked* node = l0.next;
node = new Linked {nullptr};
std::cout << "node=" << std::hex << node << std::endl;
std::cout << "l0.next=" << std::hex << l0.next << std::endl;
free(node);
std::cout << std::endl;
// Case 2: Works
std::cout << "case 2" << std::endl;
l0.next = new Linked {nullptr};
std::cout << "l0.next=" << std::hex << l0.next << std::endl;
free(l0.next);
l0.next = nullptr;
std::cout << std::endl;
// Case 3: Works
std::cout << "case 3" << std::endl;
Linked** nodeP = &(l0.next);
*nodeP = new Linked {nullptr};
std::cout << "*nodeP=" << std::hex << *nodeP << std::endl;
std::cout << "l0.next=" << std::hex << l0.next << std::endl;
free(l0.next);
l0.next = nullptr;
}
Which outputs:
$ ./main
case 1
node=0x7fba0d400620
l0.next=0x0
case 2
l0.next=0x7fba0d400620
case 3
*nodeP=0x7fba0d400620
l0.next=0x7fba0d400620
Case 1: does not work because the new Node is assigned to a copy of the left/right child pointer (i.e. not the actual child node from the parent node)
Case 2: works as expected because the new node is assigned directly to one of the parent's child node.
Case 3: also works because instead of assigning the new node to a copy of the child pointer, you assign it to a pointer referencing the pointer to child itself. In this respect, case 2 and 3 are equivalent.
Related
I have this homework for my C++ class. I have to implement a list class, where I can add from the front or the back. I have implemented it, but there is a big problem.
collectiontest.cpp
#include "list.hpp"
#include <iostream>
int main(void) {
std::cout << "--- Test List ---" << std::endl;
List<int> l;
std::cout << l;
l.add(2);
l.add_front(3);
l.add(1);
std::cout << l;
std::cout << "remove_front: " << l.remove_front() << std::endl;
std::cout << "remove_front: " << l.remove_front() << std::endl;
l.add(5);
std::cout << l;
std::cout << "Contains 5? " << l.contains(5) << std::endl;
std::cout << "remove_back: " << std::endl;
std::cout << l.remove() << std::endl;
std::cout << "remove_back: " << l.remove() << std::endl;
std::cout << l;
return 0;
}
list.hpp
#pragma once
#include "collection.hpp"
#include "node.hpp"
#include <iostream>
#include <string>
template <typename T>
class List : public Collection<T> {
private:
Node<T>* first;
Node<T>* last;
public:
~List() {
if (first != nullptr) {
}
}
void add_front(T value) {
if (first == nullptr) {
std::cout << "First element front" << std::endl;
Node<T> temp(value, nullptr, nullptr);
first = &temp;
last = &temp;
}
else {
if (first == last) {
std::cout << "First and last are same! front-> " << first << "-" << last << std::endl;
Node<T> temp(value, last, nullptr);
last->previous = &temp;
first = &temp;
std::cout << first << "-" << last << std::endl;
}
else {
Node<T> temp(value, first, nullptr);
first->previous = &temp;
first = &temp;
}
}
}
void add(T value) override {
std::cout << "LOL" << std::endl;
if (first == nullptr) {
std::cout << "First element add" << std::endl;
Node<T> temp(value, nullptr, nullptr);
first = &temp;
last = first;
std::cout << "end of add first item" << std::endl;
}
else {
if (first == last) {
std::cout << "First and last are same! add-> " << first << "-" << last << std::endl;
Node<T> temp(value, nullptr, first);
first->next = &temp;
last = &temp;
}
else {
Node<T> temp(value, nullptr, last);
last->next = &temp;
last = &temp;
}
}
}
T remove_front() {
if (first != nullptr) {
T te = first->content;
Node<T>* temp = first;
first = first->next;
return te;
}
return NULL;
}
T remove() override {
if (last != nullptr) {
Node<T>* temp = last;
T te = last->content;
last = last->previous;
last->next = nullptr;
return te;
}
return NULL;
}
bool isEmpty() override {
return (first == nullptr);
}
bool contains(T value) override {
Node<T>* temp = first;
while (temp != nullptr) {
if (temp->content == value) return true;
temp = temp->next;
}
return false;
}
void clear() override {
Node<T>* temp = first->next;
while (temp != nullptr) {
first = temp;
temp = temp->next;
}
}
int getSize() override {
int counter = 0;
Node<T>* temp = first;
while (temp != nullptr) {
counter++;
temp = temp->next;
}
return counter;
}
Node<int>* getFirst() const {
return first;
}
Node<int>* getLast() const {
return last;
}
friend std::ostream& operator<<(std::ostream& os, const List<int>& l);
};
std::ostream& operator<<(std::ostream& stream, const List<int>& l) {
std::cout << "Output called" << std::endl;
Node<int>* temp = l.getLast();
while (temp != nullptr) {
std::cout << "Schleife betreten" << std::endl;
stream << temp->content << " ";
temp = temp->previous;
}
stream << std::endl;
return stream;
}
collection.hpp
#pragma once
template <typename T>
class Collection {
virtual void add(T value) = 0;
virtual int remove() = 0;
virtual bool isEmpty() = 0;
virtual bool contains(T obj) = 0;
virtual void clear() = 0;
virtual int getSize() = 0;
};
node.hpp
#pragma once
template <typename T>
class Node {
public:
Node<T>* next;
Node<T>* previous;
T content;
Node(T value, Node<T>* next, Node<T>* previous) {
content = value;
this->next = next;
this->previous = previous;
}
~Node() {
}
};
I have looked over it several times, but I get the same error.
I debugged my program with Visual Studio, and what happens is collectortest.cpp creates the list and adds the first value with the add() function. In the debugger, I see how the temp node is created, how first is set to the address of temp, and how last is set to the same address. So, when we reach the line where "end of add first item" is supposed to be printed to the console, the variables changes, why??
You can see in this image how the variables all have appropriate values:
Before the output:
But then, when I leave the breakpoint I set at "end of add first item", this happens:
After the output:
There must be something I don't see, because this makes literally no sense. I have looked everywhere, maybe it is just something really dumb, but I am really at my limits here.
Can one of you help and explain what the problem is?
I tried to execute my program and create the nodes appropriately, but I get a strange error in my memory.
From #Wyck:
Node<T> temp(value, nullptr, nullptr);
first = &temp;
You're using a pointer to something that is allocated on the stack and will be destroyed/invalid when it goes out of scope whether you have taken note of the address it used to be at or not.
From #user4581301:
Node<T> temp(value, nullptr, nullptr);
first = &temp;
is one of the increasingly rare times you want to use new.
first = new Node<T>(value, nullptr, nullptr);
From #TedLyngmo:
For the problem #Wyck mentioned, you need to allocate and release memory dynamically using new/delete.
These were the answers posted as comments to the question. My Nodes always got destroyed because they were out of scope. I needed to use new so they got stored in the heap and weren't automatically destroyed if they went out of scope.
Edit: I can't choose my own answer as correct for 2 days, so the question sadly stays open.
I was trying to implement the linked list stl but I always get runtime error if someone can help me solve this problem please.
What I was doing is when inserting an element I check if it's the first element if it is I make the head and tail point to it if not I use the node's next pointer to point to the new element and move the tail so it point to the last inserted element the error appears in the insert function.
#include <iostream>
using namespace std;
template<typename T>
struct Node {
T item;
Node *next = nullptr;
};
template<typename T>
class list {
static int sz;
Node<T> *head, *tail;
public:
void insert(T x) {
Node<T> new_node;
new_node.item = x;
if (!sz) {
*head = new_node;
*tail = new_node;
} else {
tail->next = &new_node;
*tail = new_node;
}
sz++;
}
int size() {
return sz;
}
friend ostream &operator<<(ostream &os, list<T> &o) {
while (o.head->next != nullptr) {
os << o.head->item << " ";
o.head = o.head->next;
}
return os;
}
};
template<typename T>
int list<T>::sz = 0;
int main() {
list<int> l;
l.insert(5);
l.insert(6);
l.insert(7);
cout << l.size() << endl;
cout << l << endl;
return 0;
}
I'm running some tests on a simple 3 node binary search tree. Root node has a value of 1 and its left and right children have values of 0 and 2, respectively.
Here's the source code (3 files):
File name: bst.cpp
#include <iostream>
#include "bst.h"
template <typename T>
void binary_search_tree<T>::insert(const T val2ins)
{
num_nodes++;
if(!root)
{
root = new tree_node<T>(val2ins, nullptr, nullptr, nullptr);
return;
}
//loop from root until we find where to insert val2ins; seek to find a suitable parent with a nullptr
auto curr_node = root;
tree_node<T> *prev_node = nullptr;
while(curr_node)
{
prev_node = curr_node;
if(val2ins >= curr_node->val) //assign equalities on right children
{
curr_node = curr_node->right;
}
else
{
curr_node = curr_node->left;
}
}
//prev_node is the parent of curr_node
curr_node = new tree_node<T>(val2ins, prev_node, nullptr, nullptr);
//curr_node now points to a tree_node that contains a pointer to to the previous node
//we also need to go to previous_node and set its left/right children to curr_node
if(curr_node->val < prev_node->val)
{
prev_node->left = curr_node;
}
else
{
prev_node->right = curr_node;
}
}
template <typename T>
tree_node<T> *binary_search_tree<T>::get_root()
{
return root;
}
File name: bst.h
#ifndef _BST_H_
#define _BST_H_
template<typename T>
struct tree_node
{
T val;
tree_node *parent;
tree_node *left;
tree_node *right;
tree_node() : val(0), parent(nullptr), left(nullptr), right(nullptr) {}
tree_node(T val2ins, tree_node *p_ptr, tree_node *l_ptr, tree_node *r_ptr)
{
val = val2ins;
parent = p_ptr;
left = l_ptr;
right = r_ptr;
}
};
template<typename T>
class binary_search_tree
{
private:
int num_nodes;
tree_node<T> *root;
//helper function for deletion
void transplant(const tree_node<T> *node2replace, const tree_node<T> *node2insert);
public:
binary_search_tree() : num_nodes(0), root(nullptr) {}
binary_search_tree(int N, tree_node<T> *ptr) : num_nodes(N), root(ptr) {}
void insert(const T val2ins);
void delete_node(const tree_node<T> *node2del);
tree_node<T> *get_root();
// void
};
#endif
File name: main.cpp
#include <iostream>
#include "bst.h"
#include "bst.cpp"
template <typename T>
class Solution {
public:
tree_node<T> *trimBST(tree_node<T> *root, int L, int R) {
search_and_delete(root, L, R);
return root;
}
void search_and_delete(tree_node<T> *&node, const int L, const int R)
{
if(!node)
{
return;
}
if(node && node->val >= L && node->val <= R)
{
trimBST(node->right, L, R);
std::cout << node->left << std::endl;
trimBST(node->left, L, R);
std::cout << node->left << std::endl;
std::cout << node->left << std::endl;
}
else if(node && node->val > R)
{
//delete right sub tree
//then check left sub tree
//Also need to delete current node and link left (if needed)
//this can be done by simply setting current node to its left
if(node->left == nullptr && node->right == nullptr)
{
delete node;
node = nullptr;
return;
}
if(node->right)
{
delete node->right;
node->right = nullptr;
}
if(node->left)
{
node = node->left;
}
}
else if(node && node->val < L)
{
//delete left sub tree
//then check right sub tree
//Also need to delete current node and link right (if needed)
//this can be done by simply setting current node to
//its right
if(node->left == nullptr && node->right == nullptr)
{
std::cout << "deleting node 0" << std::endl;
std::cout << "Address prior to freeing: " << node << std::endl;
delete node;
node = nullptr;
std::cout << "Address after freeing: " << node << std::endl;
return;
}
if(node->left)
{
delete node->left;
node->left = nullptr;
}
if(node->right)
{
node = node->right;
}
std::cout << "done" << std::endl;
}
std::cout << "end" << std::endl;
}
};
int main(int argc, char const *argv[])
{
/* code */
binary_search_tree<int> my_tree;
Solution<int> soln;
my_tree.insert(1);
my_tree.insert(0);
my_tree.insert(2);
soln.trimBST(my_tree.get_root(), 1, 2);
return 0;
}
When I execute this code I get the following output:
0x0
0x0
0x0
end
0x7fdeaec02af0
deleting node 0
Address prior to freeing: 0x7fdeaec02af0
Address after freeing: 0x0
0x7fdeaec02af0
0x7fdeaec02af0
end
The pointer pointing to the node with value 0 is being deleted during the recursive call and set to nullptr. However, when it returns from the recursive call (where the pointer was passed by reference), the pointer is still pointing to the same memory address as it did prior to being deleted and set to nullptr.
I cannot figure out why this is happening. My only guess is that I have a memory leak somewhere that's causing issues with the pointer that I supposedly applied delete to.
At first let me to say you that your node struct just should have one content and two pointer of himself that will show the right and left children.
then for showing the BST you should cout the data NOT this pointers
class node
{
friend class BST;
public:
node(int Content=0,node* R_child = NULL, node*L_child = NULL)
{
this->R_child = R_child;
this->L_child = L_child;
this->Content = Content;
}
private:
int Content;
node*R_child;
node*L_child;
};
check this code for node class you can use template instead of integer.
Good Luck
I'm new with data structures in C++, and i'm trying to do a doubly linked list with templates. All the examples that i have seen are only for 1 element of the template node, so i'm trying to put 2 elements in the template node in the list, but i don't know how to do it, anyway, i tried to make the list.
Here's the code:
#include<iostream>
#include<cstring>
using namespace std;
template<class T>
// node class
class node
{
public:
node();
node(T);
~node();
node *next;
T data[2];
void borra_todo();
void print();
};
// by defect
template<typename T>
node<T>::node()
{
data[0] = NULL;
data[1] = NULL;
next = NULL;
}
// by parameter
template<typename T>
node<T>::node(T data_)
{
data[0] = data_[0];
data[1] = data_[1];
next = NULL;
}
// delete nodes
template<typename T>
void node<T>::borra_todo()
{
if (next)
next->borra_todo();
delete this;
}
// node printing
template<typename T>
void node<T>::print()
{
cout << data[0] << " " << data[1] << "->";
}
template<typename T>
node<T>::~node() {}
// list
template <class T>
class list
{
private:
node<T> *m_head;
int m_num_nodes;
public:
list();
~list();
void add_head(T);
void add_end(T);
void add_sort(T);
void fill(char r[30], char n[30]);
void search(T);
void del_by_data(T);
void print();
};
template<typename T>
list<T>::list()
{
m_num_nodes = 0;
m_head = NULL;
}
//add in the beginning
template<typename T>
void list<T>::add_head(T data_)
{
node<T> *new_node = new node<T>(data_);
node<T> *temp = m_head;
if (!m_head)
{
m_head = new_node;
}
else
{
new_node->next = m_head;
m_head = new_node;
while (temp)
{
temp = temp->next;
}
}
m_num_nodes++;
}
// add to the last
template<typename T>
void list<T>::add_end(T data_)
{
node<T> *new_node = new node<T> (data_);
node<T> *temp = m_head;
if (!m_head)
{
m_head = new_node;
}
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = new_node;
}
m_num_nodes++;
}
// it is supposed that sorts items in the list ...
template<typename T>
void list<T>::add_sort(T data_)
{
node<T> *new_node = new node<T> (data_);
node<T> *temp = m_head;
if (!m_head)
{
m_head = new_node;
}
else
{
for (int i =0; i <= 1; i++)
{
if (m_head->data[0] > data_[i])
{
new_node->next = m_head;
m_head = new_node;
}
else
{
while ((temp->next != NULL) && (temp->next->data[0] < data_[i]))
{
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
}
}
m_num_nodes++;
}
}
// sort adding ...
template<typename T>
void list<T>::fill(char rfc[30])
{
char temprfc[30];
char tempnombre[30];
temprfc = "DUDE010101R0";
tempnombre = "Dude";
add_sort(temprfc, tempnombre);
temprfc = "AUDE010101R1";
tempnombre = "Commander";
add_sort(temprfc, tempnombre);
}
// print list
template<typename T>
void list<T>::print()
{
node<T> *temp = m_head;
if (!m_head)
{
cout << "List is empty" << endl;
}
else
{
while (temp)
{
temp->print();
if (!temp->next)
cout << "NULL\n";
temp = temp->next;
}
}
cout << endl;
}
// search the list
template<typename T>
void list<T>::search(T data_)
{
node<T> *temp=m_head;
int cont=1;
int cont2=0;
while(temp)
{
if(strcmp(temp->data,data_[0]))
{
cout<<"Element found " << temp->data;
cout << " in position: " << cont << endl;
cont2++;
}
temp=temp->next;
cont++;
}
if(cont2==0)
{
cout << "Element not found"<<endl;
}
}
// ... delete by data
template<typename T>
void list<T>::del_by_data(T data_)
{
node<T> *temp = m_head;
node<T> *temp1 = m_head->next;
int cont =0;
if (m_head->data == data_)
{
m_head = temp->next;
}
else
{
while (temp1)
{
if (temp1->data == data_)
{
node<T> *aux_node = temp1;
temp->next = temp1->next;
delete aux_node;
cont++;
m_num_nodes--;
}
temp = temp->next;
temp1 = temp1->next;
}
}
if (cont == 0)
{
cout << "No data" << endl;
}
}
// destroy the constructor
template<typename T>
list<T>::~list() {}
int main()
{
list<char> list1;
char element1[30];
char element2[30];
int dim, choice, pos;
do{
cout << "Select a choice.\n";
cout << "1. Print list\n";
cout << "2. Delete an element of the list\n";
cout << "3. Search an element of the list\n";
cout << "4. Exit\n";
cin >> choice;
switch(choice)
{
case 1:
{
cout << "Printing list:\n";
list1.fill("1","2");
list1.print();
break;
}
case 2:
{
cout << "Element to delete: ";
cin >> element1;
list1.search(element1);
element1 = "";
break;
}
case 3:
{
cout << "Element to search: ";
cin >> element1;
list1.search(element1);
element1 = "";
break;
}
}
}while(choice != 4);
return 0;
}
The code doesn't compile, it marks errors like:
" error: prototype for ‘void list::fill(char*)’ does not match any in class ‘list’
void list::fill(char rfc[30])
^
t3b.cpp:79:8: error: candidate is: void list::fill(char*, char*)
void fill(char r[30], char n[30]);"
Any ideas on how to fix it? Or any ideas on how to put 2 elements in a node using templates?
Thanks in advance.
Dude, you should really try to isolate the error a little bit before posting it. This is 500 lines of code, I had to copy and paste it all into an editor before I could even look at it.
When you declared fill, it had two arguments, when you defined it, it had one. Also, I would avoid arrays of characters for numerous reasons, instead use std::string.
my data structure teacher gave us the task to implement lists and stacks manually and it worked just fine, but then we had to reimplement it to accept any type of parameters using template. Since then, I've been facing lots of different errors. Right now, I'm having a c2019 error, but I can't see what's wrong with my code.
Tried this:pilha.getTopo<int>();, this: Lista<int> lista;, this: typedef Lista<int> Lista_int;, but no luck.
Now, my code:
######UPDATE: The code bellow is working
Listas.h
#pragma once
#include "stdafx.h"
#include <iostream>
using namespace std;
/*============================Node================================*/
template <typename N>
class Node
{
N value;
Node *next, *prev;
public:
Node(void) {};
Node(N _value){
this->value = _value;
this->prev = NULL;
this->next = NULL;
};
~Node(void) {};
void setValue(int _value) { this->value = _value; }
void setNext(Node *_next) { this->next = _next; }
void setPrev(Node *_prev) { this->previous = _prev; }
int getValue() { return this->value; }
Node *getNext() { return this->next; }
Node *getPrev() { return this->prev; }
};
/*===========================List===============================*/
template <typename T>
class List
{
Node<T> *begin, *end;
int list_size;
public:
List(void){
this->begin = NULL;
this->end = NULL;
this->list_size = 0;
};
~List(void) {};
template <typename T> void insertBegin(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = newNode;
this->list_size += 1;
}else{
newNode->setNext(begin);
this->begin = newNode;
this->list_size += 1;
}
};
template <typename T> void removeBegin(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = NULL;
this->list_size = 0;
}else{
if(begin == end)
this->end = NULL;
this->begin = newNode->getNext();
newNode = begin;
newNode->setPrev(NULL);
this->list_size -= 1;
}
};
template <typename T> void insertEnd(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = newNode;
this->list_size += 1;
}else{
newNode->setPrev(end);
this->end = newNode;
this->list_size += 1;
}
};
template <typename T> void removeEnd(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = NULL;
this->list_size = 0;
}else{
if(begin == end)
this->end = NULL;
this->end = newNode->getPrev();
newNode = end;
newNode->setNext(NULL);
this->list_size -= 1;
}
};
template <typename T> void exibeList(){
Node<T> *node;
cout << "Begin: " << begin << endl
<< "End: " << end << endl
<< "Size: " << list_size << endl << endl;
if(begin != NULL){
node = begin;
do{
cout << "Mem. adress: " << &node << endl
<< "Value: " << node->getValue() << endl
<< "Previous: " << node->getPrev() << endl
<< "Next: " << node->getNext() << endl
<< endl;
node = node->getNext();
}while(node != NULL);
}
};
};
/*===========================Stack==============================*/
template <typename T>
class MyStack
//: public List<T>
{
Node<T> *top, *next;
int my_stack_size;
public:
MyStack<T>(void){
this->my_stack_size = 0;
this->top = NULL;
this->next = NULL;
};
~MyStack<T>(void) {};
template <typename T> void insertTop(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->my_stack_size == 0){
this->top = this->next = newNode;
this->my_stack_size += 1;
}else{
newNode->setNext(top);
this->next = top;
this->top = newNode;
this->my_stack_size += 1;
}
};
template <typename T> void removeTop(){
Node<T> *node;
if(this->my_stack_size > 0){
node = top;
this->top = next;
delete node;
this->my_stack_size -= 1;
}else
cout << "Stack underflow." << endl;
};
template <typename T> void getTop(){
Node<T> *node = new Node<T>;
node = top;
if(node->getPrev() == NULL)
cout << node->getValue() << endl;
else
cout << "Error. Node isn't the first." << endl;
};
template <typename T> void show(){
Node<T> *node;
cout << "Top: " << top << endl
<< "Next: " << next << endl
<< "Size: " << my_stack_size << endl << endl;
if(top != NULL){
node = top;
do{
cout << "Mem. adress: " << &node << endl
<< "Value: " << node->getValue() << endl
<< "Previous: " << node->getPrev() << endl
<< "Next: " << node->getNext() << endl
<< endl;
node = node->getNext();
}while(node != NULL);
}
};
};
and finally, Lista.cpp
// Lista.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Listas.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
/* ERROR C2019
//typedef Pilha<int> Pilha_int;
//typedef Nodo<int> Nodo_int;
//
//Lista_int lista;
//Pilha_int pilha;
//Nodo_int nodo(25), nodo2(40), nodo3(55), nodo4(70);
*/
/* C2019
Pilha<int> pilha;
Nodo<int> nodo(25), nodo2(40);
/*error C2955: use of class template requires template argument list
//Pilha pilha;
//Nodo nodo(25), nodo2(40), nodo3(55), nodo4(70);
pilha.insereTopo(nodo);
pilha.getTopo(); //C2783
pilha.insereTopo(nodo2);
pilha.getTopo(); //C2783
pilha.exibe(); //error C2783: could not deduce template argument for 'T'
pilha.exibe<int>(); //C2019
system("pause");
return 0;
}
Thanks in advance.