my header and implimentation file
#ifndef VIKTOR_H_
#define VIKTOR_H_
#include <iostream>
template <class DataType>
class viktor {
private:
template <class NodeType>
struct Node {
NodeType data;
Node<NodeType> * next;
};
Node<DataType> * backPtr;
int length;
public:
viktor();
~viktor();
DataType &operator [] (int) const;
void push(const DataType &);
friend std::ostream &operator << (std::ostream &strm, const
viktor<DataType> &A){
for(int i = 0; i < A.length; ++i) {
strm << A[i] << " " << std::flush;
}
strm << std::endl;
return strm;
}
};
template <class DataType>
viktor<DataType>::viktor() {
backPtr = nullptr;
length = 0;
}
template <class DataType>
viktor<DataType>::~viktor() {
if ( length == 0 ) return;
Node<DataType>* previousPtr = backPtr;
Node<DataType>* nextPtr = backPtr->next;
while( nextPtr != backPtr )
{
nextPtr = nextPtr->next;
previousPtr->next = nullptr;
delete previousPtr;
length--;
}
backPtr->next = nullptr;
delete backPtr;
}
template <class DataType>
void viktor<DataType>::push(const DataType &item) {
Node<DataType>* newNode = new Node<DataType>;
newNode->data = item;
std::cout << "data: " << newNode->data << std::endl;
if(length != 0) {
newNode->next = backPtr->next;
backPtr->next = newNode;
}
else {
newNode->next = newNode;
}
backPtr = newNode;
length += 1;
//std::cout << "Finished pushing..." << std::endl;
}
template <class DataType>
DataType &viktor<DataType>::operator [] (int i) const {
Node<DataType>* conductor = backPtr;
if (i > length) {
throw "Item is inaccessible";
}
for (int j = 0; j <= i; ++j) {
conductor = conductor->next;
}
return conductor->data;
}
#endif //VIKTOR_H_
i am having problems with this function
template <class DataType>
void viktor<DataType>::push(const DataType &item) {
Node<DataType>* newNode = new Node<DataType>;
newNode->data = item;
std::cout << "data: " << newNode->data << std::endl;
if(length != 0) {
newNode->next = backPtr->next;
backPtr->next = newNode;
}
else {
newNode->next = newNode;
}
backPtr = newNode;
length += 1;
//std::cout << "Finished pushing..." << std::endl;
}
i am using this test driver
int main() {
viktor<int> blah;
blah.push(2);
std::cout << blah << std::endl;
}
now it works if i only push once but if i do
blah.push(1);
blah.push(2);
it runs and prints all of the data that i want but at the end of the program it dumps my core (i can provide the specific error if you want but it's just a memory map and then aborted (core dumped))
i fixed it! thanks Some programmer dude! so the problem was my deconstructor, what i did was changed previousPtr->next = nullptr; to previousPtr = previousPtr->next;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
How come when I do Queue<listEntry<int> > queue2(intList); the copy constructor is being called twice?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class dataType>
class listEntry
{
private:
dataType data;
public:
listEntry *next;
listEntry *prev;
dataType getData() { return this->data; }
listEntry();
listEntry(dataType data) { this->data = data; }
~listEntry() {};
};
template <class dataType>
class List
{
private:
dataType *head;
dataType *tail;
int count;
public:
dataType *getHead() { return this->head; }
dataType *getTail() { return this->tail; }
void addToTail(dataType *newEntry);
void addToHead(dataType *newEntry);
int getCount() { return count; }
void printListForward();
List();
List(const List<dataType> &li);
~List();
};
template <class dataType>
class Queue: public List<dataType>
{
public:
void enQueue(dataType *newEntry);
Queue():List<dataType>() { return; }
Queue(List<dataType> li):List<dataType>(li) { return; }
};
void addIntegersToList(Queue<listEntry<int> > *queue);
int main()
{
cout << "Queue of Integers: " << endl;
cout << "Default Queue Constructor: " << endl;
//Create an intance of a queue using the default queue constructor
Queue<listEntry<int> > queue;
addIntegersToList(&queue);
queue.printListForward();
cout << endl;
cout << "List parameter Queue Constructor: " << endl;
//Create a new list that will be used to initiate an instance of a queue
List<listEntry<int> > intList;
//Add some data to that list
intList.addToTail(new listEntry<int>(11));
//Now create a new instance of a queue with a list as its creation parameter
Queue<listEntry<int> > queue2(intList);
//addIntegersToList(&queue2);
//queue2.printListForward();
while (true);
}
template <class dataType>
List<dataType>::List()
{
this->head = NULL;
this->tail = NULL;
this->count = 0;
}
template <class dataType>
void List<dataType>::addToTail(dataType *newEntry)
{
newEntry->next = NULL;
newEntry->prev = NULL;
if (this->head == NULL)
{
this->head = newEntry;
this->tail = this->head;
this->count = this->count + 1;
}
else
{
this->tail->next = newEntry;
newEntry->prev = this->tail;
this->tail = newEntry;
this->count = this->count + 1;
}
dataType *temp = this->tail;
}
template <class dataType>
void List<dataType>::addToHead(dataType *newEntry)
{
newEntry->next = NULL;
newEntry->prev = NULL;
if (this->head == NULL)
{
this->head = newEntry;
this->tail = this->head;
}
else
{
this->head->prev = newEntry;
newEntry->next = this->head;
this->head = newEntry;
}
this->count = this->count + 1;
}
template <class dataType>
void List<dataType>::printListForward()
{
cout << "Head: ";
dataType *temp = this->head;
while (temp)
{
cout << temp->getData() << " -> ";
temp = temp->next;
}
cout << " :Tail" << endl;
}
void addIntegersToList(Queue<listEntry<int> > *queue)
{
int i = 0;
for (i = 1; i <= 10; i++)
queue->enQueue(new listEntry<int>(i));
}
template <class dataType>
void Queue<dataType>::enQueue(dataType *newEntry)
{
this->addToTail(newEntry);
}
template <class dataType>
List<dataType>::~List()
{
while (this->head)
{
dataType *next = this->head->next;
delete this->head;
this->count = this->count - 1;
this->head = next;
}
}
template <class dataType>
List<dataType>::List(const List<dataType> &li)
{
tail = new dataType;
*tail = *li.tail;
head = new dataType;
*head = *li.head;
cout << "head: " << head << endl;
cout << "tail: " << tail << endl;
count = li.count;
cout << "count: " << count << endl;
}
template <class dataType>
listEntry<dataType>::listEntry()
{
this->next = NULL;
this->prev = NULL;
this->data = 0;
}
Because you are passing the List by value to the Queue constructor. Try this instead
Queue(const List<dataType>& li):List<dataType>(li) { return; }
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.
I'm trying to make a dynamic queue in C++ and I wrote most of the code, yet it doesn't work, could someone be kind enough to look through it and tell me what's wrong? :)
Also please comment on the coding style I wish to improve.
I wrote a main function to test the program and this should be the expected result:
data1=1
data1=1 data2=2
1 2 3 4 5 6
Del:1 Del:2 Del:3
This is my code:
#include <iostream>
template<typename T>
class Queue
{
struct Node
{
T data;
Node* next;
};
Node* head;
Node* tail;
int qsize;
public:
Queue()
{
head = NULL;
tail = NULL;
qsize = 0;
}
bool empty()
{
if(qsize = 0){return true;}
else {return false;}
}
void put(const T& data)
{
Node *newNode = new Node;
if(qsize)
{
tail->next = newNode;
newNode->data = data;
newNode->next = NULL;
tail = newNode;
}
else
{
head = tail = newNode;
newNode->data = data;
newNode->next = NULL;
}
qsize++;
}
T get()
{
T val;
Node *temp;
if(empty())
{
std::cout << "queue is empty" << std::endl;
}
else
{
val = head->data;
temp = head;
head = head->next;
delete temp;
qsize--;
return val;
}
}
void destroyQueue()
{
while(!empty())
{
std::cout<<"DEL:";
get();
}
}
~Queue()
{
destroyQueue();
}
};
int main()
{
int data1,data2;
Queue<int>* q = new Queue<int>();
q->put(1);
data1 = q->get();
std::cout << " data1=" << data1 << std::endl;
q->put(1);
q->put(2);
data1 = q->get();
data2 = q->get();
std::cout << " data1=" << data1
<< " data2=" << data2 << std::endl;
q->put(1);
q->put(2);
q->put(3);
q->put(4);
q->put(5);
q->put(6);
while (!q->empty()) std::cout << " " << q->get();
std::cout << std::endl;
q->put(1);
q->put(2);
q->put(3);
delete q;
}
if(qsize = 0) should be if(qsize == 0) - don't assign, compare!
I am trying to use a class Student and declare it as a list type. I can pushback but without changing the List.h or Node.h how can I print the data in list2? The given print() function within List..h does not work :(
Node.h
#ifndef NODE_H
#define NODE_H
#include <string>
#include <iostream>
using namespace std;
template <typename T>
class Node {
private:
T data;
Node<T>* next;
public:
Node(T);
virtual ~Node(); // base class destructor must be virtual
template <typename U> friend class List;
};
template <typename T>
Node<T>::Node(T d) {
data = d;
next = NULL;
}
template <typename T>
Node<T>::~Node() {
}
#endif /* STRNODE_H */
List.h
#ifndef LIST_H
#define LIST_H
#include "Node.h"
// Singly linked list
template <typename T>
class List {
private:
Node<T>* head; // pointer to the first node
Node<T>* tail; // pointer to the last node
int count; // number of nodes in the list
public:
class OutOfRangeException{ }; // empty inner class for exception handling
List();
virtual ~List();
void push_back(T item);
void insert(int index, T item);
void remove(int index);
int indexOf(T item);
T get(int position); // OutOfRangeException is generated
bool isEmpty();
int size();
void print();
};
template <typename T>
List<T>::List() {
head = tail = NULL;
count = 0;
}
template <typename T>
List<T>::~List() {
Node<T>* discard;
while (head != 0) {
discard = head;
head = head->next;
delete discard;
}
}
// append an item at the end of the StrList
template <typename T>
void List<T>::push_back(T item) {
try {
Node<T>* newNode = new Node<T>(item);
if (head == 0) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
++count;
} catch (bad_alloc &e) {
cout << "memory allocation exception: " << e.what() << endl;
exit(1);
}
}
// insert an item at the specified index
template <typename T>
void List<T>::insert(int index, T item) {
try {
if (index < 0 || index > count) // push_back() if index == count
throw OutOfRangeException();
Node<T>* newNode = new Node<T>(item);
if (head == 0) { // empty
head = tail = newNode;
} else if (index == 0) { // at the start
newNode->next = head;
head = newNode;
} else if (index == count) { // at the end
tail->next = newNode;
tail = newNode;
} else { // insert in the middle
Node<T>* prevNode;
Node<T>* currNode = head;
for (int i = 0; i < index; i++) {
prevNode = currNode;
currNode = currNode->next;
}
// insert between 'prevNode' and 'currNode'
prevNode->next = newNode;
newNode->next = currNode;
}
++count;
} catch (bad_alloc &e) {
cout << "memory allocation exception: " << e.what() << endl;
exit(1);
}
}
// is the StrList empty?
template <typename T>
bool List<T>::isEmpty() {
return count == 0;
}
// remove the item at specified index
template <typename T>
void List<T>::remove(int index) {
if (index < 0 || index >= count)
throw OutOfRangeException();
if (index == 0) { // at the start
Node<T>* discard = head;
head = head->next;
delete discard;
} else {
Node<T>* prevNode;
Node<T>* currNode = head;
for (int i = 0; i < index; i++) {
prevNode = currNode;
currNode = currNode->next;
}
// remove 'currNode'
prevNode->next = currNode->next; // bypass
delete currNode;
if (index == count - 1) // last node was removed. Update 'tail'
tail = prevNode;
}
--count;
if (count == 0)
tail = NULL;
}
// retrieve the item at the given position of the StrList. position starts from 0.
// throws OutOfRangeException if invalid position value is given.
template <typename T>
T List<T>::get(int position) {
if (position < 0 || position >= count)
throw OutOfRangeException();
int loc = 0;
Node<T>* curr = head;
while (loc < position) {
++loc;
curr = curr->next;
}
return curr->data;
}
// Requirement:
// != operator of <class T> is used
template <typename T>
int List<T>::indexOf(T item) {
if (head == 0) {
return -1; // not found
} else {
int index = 0;
Node<T>* currNode = head;
while (currNode->data != item && currNode != NULL) {
currNode = currNode->next;
++index;
}
if (currNode == NULL) // not found thru the end
return -1;
else
return index;
}
}
// number of nodes in the StrList
template <typename T>
int List<T>::size() {
return count;
}
// Requirement:
// << operator for <class T> is used.
template <typename T>
void List<T>::print() {
cout << "*** StrList contents ***" << endl;
for (int i = 0; i < count; i++) {
cout << i << ": " << get(i) << endl;
}
}
#endif
Student.h
#include "List.h"
class Student {
private:
string name;
int id;
public:
Student();
Student(string a);
virtual ~Student();
friend ostream& operator<<(ostream &os, const Student& p);
bool operator!=(const Student &p) const;
bool operator==(const Student &p) const;
};
Student::Student() {
}
Student::Student(string a) {
name = a;
}
Student::~Student() {
}
ostream& operator<<(ostream &os, const Student& p) {
return os << p.name;
}
bool Student::operator==(const Student &p) const {
// Compare the values, and return a bool result.
if (name == p.name)
return true;
else
return false;
}
bool Student::operator!=(const Student &p) const {
return !(*this == p);
}
main.cpp
#include <iostream>
using namespace std;
#include "Student.h"
int main() {
cout << "\n*** StrList Test ***" << endl;
List<string> list;
list.push_back("zero");
list.push_back("one");
list.push_back("two");
list.push_back("three");
list.push_back("four");
list.push_back("five");
list.print();
list.insert(1, "inserted at position 1");
list.insert(0, "inserted at position 0");
list.insert(4, "inserted at position 4");
list.print();
cout << "removing at indexes 3, 0" << endl;
list.remove(3);
list.remove(0);
list.print();
list.insert(2, "inserted at position 2");
list.print();
cout << "five is at index " << list.indexOf("five") << endl;
cout << "two is at index " << list.indexOf("two") << endl;
//Test for my Student class implementation
// Student<string> st1; //Create new student Ryan Martin with id of 1
List<Student> list2;
Student stu("Ryan Martin");
list2.push_back(stu);
//list2.print();
//list2.push_back("Ryan");
//list2.PrintStudents(); //Test that the Student class successfully stored and can access
return 0;
}
The << operator must be defined for your Student class. To quote List.h:
// Requirement:
// << operator for <class T> is used.
template <typename T>
void List<T>::print() {
cout << "*** StrList contents ***" << endl;
for (int i = 0; i < count; i++) {
cout << i << ": " << get(i) << endl;
}
}
So in your Student class, you need to implement the operator<<(ostream &out);
Do it as a friend (friends are fun!):
friend std::ostream& operator<< (std::ostream &out, const Student &stu)
{
return out << stu.name << " id: " << stu.id << std::endl;
}
Here is a good reference:
http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/
If I understood you correctly, then you want to define operator<< for your student class, which you can do for example like this:
friend std::ostream & operator<<(std::ostream & os, const Student & s)
{
return os << s.name << " " << s.id << std::endl;
}
Note however that I have not tested this code and I haven't read all the snippets you posted,
so I might have understood you wrongly.
EDIT:
So after trying it out with Visual Studio, the full version of you student class should be like this:
#include "List.h"
class Student {
private:
string name;
int id;
public:
Student();
Student(string a);
virtual ~Student();
friend std::ostream & operator<<(std::ostream & os, const Student & s)
{
return os << s.name << " " << s.id << std::endl;
}
};
Student::Student() {
}
Student::Student(string a) {
name = a;
}
Student::~Student() {
}
Also not that you do not have to make the destructor in Student virtual unless you plan on having it as a base class for other classes.
the print function require an operator << to be defined on your student class and it's not the case
so define how a student will be display by << and it should work!
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.