Witam, mam problem z zadaną pracą domową. Polega ona na zaimplementowaniu listy jednokierunkowej. Napisałem już sporą część kodu, lecz ciągle stoję w miejscu. Obstawiam, że błąd leży w klasie list na samym początku. Proszę o poradę.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Node {
public:
int data;
Node* next = NULL;
/*
* Konstruktory
*/
Node()
{
this->data = 0;
}
Node(int data)
{
this->data = data;
}
/*
* Metoda wyświetlająca informacje o węźle
*/
void print()
{
cout << "-¦" << endl
<< "- &node = " << this << endl
<< "- node.data = " << data << endl
<< "- node.next = " << next << endl
<< "L¦" << endl;
}
};
class List {
public:
Node* head;
Node* tail;
Node* current;
void addToHead( Node* node){
head->next = node;
head = node;
if(!tail) {
tail = node;
}
}
void addToTail(Node* node) {
tail->next = node;
tail = node;
if(!head) {
head = node;
current = node;
}
}
void print(){
Node* current = head;
while(current!=NULL) {
current->print();
current = current ->next;
}
}
int next() {
int d = current->data;
current = current->next;
return d;
}
bool hasNext()
{
return current != NULL;
}
void removeFromHead()
{
Node* node = head->next;
delete head;
head = node;
}
};
int main()
{
List* list = new List();
Node* n1 = new Node(33);
list->addToHead(n1);
Node* n2 = new Node(128);
list->addToHead(n2);
list->addToTail( new Node(999));
list->print();
while(list->hasNext()) {
cout<<list->next()<<endl;
}
delete list;
return 0;
}