0

For the most parts I believe my program is correct, when compiled it doesn't show any errors. The problem that I do have is that I cannot the information on my linked list to print. When the print out comes out it's suppose to show a list of names instead it shows the number "1".

header files (3)

#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;

the other files (4)

# 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;

}

Thank in advance everyone. I really need your help

2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by r.stiltskin
0

The correct syntax for calling a method of an instance of a class is:

objectName.method(args)

In your case:

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

That may or may not work, depending on whether there are other errors may be in the code.

By the way, that list constructor is ... astonishing is the nicest word I can think of. It makes very little sense to write a list class that is hard-coded to produce only a specific list of pre-defined items. I can honestly say I've never seen anything quite like that. A more normal approach is to have a constructor that produces an empty list containing 0 or 1 node, and another method that adds nodes (and their data contents) one at a time on demand with values that are determined at that time based on user input, file input, or data that is generated as a result of the program's processing.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.