linked list printing

April 19, 2009 at 14:37:17
Specs: Windows XP
need help to print a list of name, when i print it just prints out the number "1" dont know why.

header files:
[code]
#ifndef NODE_H
#define NODE_H
# include <string>
using std::string;

class Node
{public:
Node();
private:
string data;
Node* previous;
Node* next;
friend class List;
friend class Iterator;};
#endif;


#ifndef ITERATOR_H
#define ITERATOR_H
# include <string>
#include "list.h"
#include "node.h"
using std::string;

class Iterator
{ public:
Iterator();
string get() const;
void next();
void previous();
bool equals() const;

private:
class Node* position;
class Node* last;
friend class List;};
#endif;

#ifndef LIST_H
#define LIST_H
# include <string>
# include "node.h"
#include"iterator.h"
#include<iostream>
using std::string;

class List
{ public:
List();
void push_back();
Iterator begin();
Iterator end();
void display();

private:
Node* first;
Node* last;
friend class Node;};
#endif;
[/code]

other files:
[code]# include "node.h"
#include "list.h"
using std::string;

Node::Node()
{Node* t1 = new Node();
t1->previous = NULL;
t1->data = "Allen";
t1->next =NULL;

Node *t2 = new Node();
t2->previous = t1;
t2->data = "Tom";
t2->next = NULL;
t1->next = t2;

Node *t3 = new Node();
t3->previous = t2;
t3->data = "John";
t3->next = NULL;
t2->next = t3;

Node *t4 = new Node();
t4->previous = t3;
t4->data = "Jerry";
t4->next = NULL;
t3->next = t4;

Node *t5 = new Node();
t5->previous = t4;
t5->data = "Fiona";
t5->next = NULL;
t4->next = t5;

Node *t6 = new Node();
t6->previous = t5;
t6->data = "Chris";
t6->next = NULL;
t5->next = t6;

Node *t7 = new Node();
t7->previous = t6;
t7->data = "Wolf";
t7->next = NULL;
t6->next = t7;

Node *t8 = new Node();
t8->previous = t7;
t8->data = "Lion";
t8->next = NULL;
t7->next = t8;

Node *t9 = new Node();
t9->previous = t8;
t9->data = "Tiger";
t9->next = NULL;
t8->next = t9;

Node *t10 = new Node();
t10->previous = t9;
t10->data = "Dog";
t10->next = NULL;
t9->next = t10;
}

# include "iterator.h"
# include "node.h"
# include "assert.h"
#include<iostream>
using std::string;

Iterator::Iterator()
{
position = NULL;
last = NULL;
}

string Iterator::get() const
{
assert(position != NULL);
return position->data;
}

void Iterator::next()
{
assert(position != NULL);
position = position->next;
}

void Iterator::previous()
{
if (position == NULL)
position = last;
else position = position->previous;
assert(position != NULL);
}


#include<iostream>
# include "node.h"
# include "list.h"
using namespace std;


List::List()
{
first = NULL;
last = NULL;
}

void List::push_back()
{
Node* newnode = new Node;
if (last == NULL)
{
first = newnode;
last = newnode;
}
else
{
newnode->previous = last;
last->next = newnode;
last = newnode;
}
}

Iterator List::begin()
{
Iterator iter;
iter.position = first;
iter.last = last;
return iter;
}

Iterator List::end()
{
Iterator iter;
iter.position = NULL;
iter.last = last;
return iter;
}
void List::display()
{
Node* n;
for(n = first; n != NULL; n = n->next)
{cout<<" "<<n->data;}

}


#include<iostream>
#include"node.h"
#include"list.h"
#include"Iterator.h"
#include<string>
using namespace std;

int main()

{
List mylist;

cout<<" I have used linked list to manage my soldiers."<<endl;
cout<<"Currently on the list are: "<<&List::display<<endl;

return 0;

}[/code]


See More: linked list printing

Report •


#1
April 20, 2009 at 02:19:14
cout<<"Currently on the list are: "<<&List::display<<endl; 

Should be:
std::cout << "Currently on the list are: ";
mylist.display();
std::cout << std::endl;


Report •
Related Solutions


Ask Question