Alright, well I tried my first attempt at creating a linked list. It didn't work out so well.
There is the code. I know it's not going to be the most proficient way to do it, but just ignore that, if you please.Code:#include <iostream> using namespace std; struct node { int key_value; node *next; }; class list { public: list(); void list_insert(int); void list_output(); private: node *root; }; list::list() { root = new node; root->next = NULL; root->key_value = 0; } void list::list_insert(int value) { node *pos; pos = root; while (pos->next != NULL) { pos = pos->next; } if (pos->next == NULL) { pos->key_value = value; } } void list::list_output() { node *pos; pos = root; int count = 1; do { cout << "Position " << count << ": " << pos->key_value; count += 1; } while (pos->next != NULL); } int main() { list lista; lista.list_insert(3); lista.list_insert(4); lista.list_output(); return 0; }
If you run it, it only outputs the last value added to the list. Any ideas as to why?



LinkBack URL
About LinkBacks


