Hi ...
i am implementing a simple linked list in c++. I have a mistake and i don't see it :(
#include <stdexcept>
#include <iostream>
struct Node {
Node(Node *next, int value):
next(next), value(value) {
}
Node *next;
int value;
};
class List {
Node *first;
int len;
Node *nthNode(int index);
public:
List():first(0),len(0){
}
// Copy - Konstruktor
List(const List & other){
};
// Zuweisungs - Operator O(len +other.len)
List &operator=(const List &other) {
clear();
if(!other.len) return *this;
Node *it = first = new Node(0,other.first->value);
for (Node *n = other.first->next; n; n = n->next) {
it = it->next = new Node(0, n->value);
}
len = other.len;
return *this;
}
// Destruktor
~List(){
};
void push_back(int value){
};
void push_front(int value){
Node* front = new Node(0,value);
if(first){
first = front;
front->next = 0;
}else{
front->next = first;
first = front;
}
len++;
};
int &at(int index){
int count = 0 ;
int ret ;
Node *it = first;
for (Node *n = first->next; n; n = n->next) {
if(count==index) ret = n->value;
count++;
}
return ret ;
};
void clear(){
};
void show() {
std::cout << " List [" << len << " ]:{ ";
for (int i = 0; i < len; ++i) {
std::cout << at(i) << (i == len - 1 ? '}' : ',');
}
std::cout << std::endl;
}
};
/*
*
*/
int main() {
List l;
// l. push_back(1);
// l. push_back(2);
l. push_front(7);
l. push_front(8);
l. push_front(9);
l.show();
// List(l). show();
}
it works ... but the output is :
List [3 ]:{ 0,134520896,9484585}