I'm assigned to implement a linked list with the help of an external iterator class. I'm finding the assignment to be difficult and from what I can see, it's missing pieces of code. The header files were given by the teacher and we have to implement them. Can't use standard library.
DRIVER PROVIDED BY TEACHER
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// FILE: sequence_test.cxx
// An interactive test program for the new sequence class
#include <cctype> // Provides toupper
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "list.h" // With value_type defined as int
#include "Iterator.h" // ERROR1: PCH warning: header stop not at file scope.
// An Intellisense PCH file was not generated.
// Don't really understand the error. When I created the blank project, I added // all the files given to me by my teacher.
usingnamespace std;
usingnamespace list_1;
List.h File Class is implemented after the header portion.
#ifndef LIST_H
#define LIST_H
#include "Node.h"
#include "Iterator.h"
namespace list_1
{
class list
{
public:
// CONSTRUCTOR
list( );
// postcondition: all nodes in the list are destroyed.
~list();
// MODIFICATION MEMBER FUNCTIONS
//postcondition: entry is added to the front of the list
void insert_front(constint& entry);
//postcondition: entry is added to the back of the list
void add_back(constint& entry);
// postcondition: all nodes with data == entry are removed from the list
void remove_all(constint& entry);
// postcondition: an iterator is created pointing to the head of the list
Iterator begin(void);
// CONSTANT MEMBER FUNCTIONS
// postcondition: the size of the list is returned
int size( ) const;
private:
Node* head;
};
// IMPLEMENTATION OF LIST CLASS
//Default Constructor
list::list()
{
head ==NULL;
}
list::~list()
{
Node *p = head;
while(p != NULL)
{
Node *pnext =p->next;
delete p;
p=pnext;
}
}
void list::insert_front(constint& entry)
{
if(head==NULL)
{
Node *nn=new Node(entry);
return;
}
Node *nn=new Node(entry);
nn->next=head;
head=nn;
}
void list::add_back(constint& entry)
{
if(head==NULL)
{
Node *nn=new Node(entry);
return;
}
Node *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next=new Node(entry);
}
void list::remove_all(constint& entry)
{
// Haven't started this part yet
}
// postcondition: an iterator is created pointing to the head of the list
Iterator begin(void)
{
Iterator I(Node head)
return I; // ERROR2: no suitable constructor exists to convert
// from "list_1::Iterator (list_1::Node head)" to
// "list_1::Iterator"
}
// postcondition: the size of the list is returned
int size() const
{
}
#endif
Iterator.h file. Class implemented after header portion
// Template CLASS PROVIDED: Iterator
#pragma once
#include "Node.h"
namespace list_1
{
class Iterator
{
public:
// Constructor
Iterator (Node *np);
// precondition: is_item is true
// post condition n points to the next item in the list
voidoperator++();
// precondition:
// postcondition: returns true if there is a valid item
bool is_item();
// precondition: is_item == true
// postcondition returns data that n is pointing at
intoperator* ();
private:
Node* n;
};
// IMPLEMENTATION CODE FOR ITERATOR CLASS
// Constructor
Iterator::Iterator(Node *np)
{
n=np;
}
// Operator ++ Overload Function
void Iterator::operator++()
{
if(is_item())
{
n++;
}
}
// precondition: is_item == true
// postcondition returns data that n is pointing at
int Iterator::operator* ()
{
if(is_item())
{
return n; // ERROR3: return value type does not match
// the function type
}
else
{
return -1;
}
}
// purpose: checks current position for valid item
// postcondition: returns true if there is a valid item
bool is_item()
{
return np !=NULL; //ERROR4: np is listed as undefined.
}
}
This is what the teacher has placed on the assignment:
You may not use the standard template library for this or any other project this semester unless specifically mentioned in the instructions
Pretty much states that I can't use any standard pre-built template within any code I write (The List.h, Node.h, Iterator.h). It has to be written out. The driver wasn't written by me. That came straight from the teacher.
Sorry I've been picking at it a piece at a time. Guess I'll just start here.
1 2 3 4 5 6 7 8
// postcondition: an iterator is created pointing to the head of the list
Iterator begin(void)
{
Iterator I(Node head)
return I; // ERROR2: no suitable constructor exists to convert
// from "list_1::Iterator (list_1::Node head)" to
// "list_1::Iterator"
}
Post Condition states what Iterator begin(void) is suppose to do. I'm thinking that I've probably gotten the code all wrong. We did something similar in class but that didn't involve a linked list like this one does.
sheesh, I would just write my own class to handle a linked list I write... this is just... dumb...
Did your professor tell you that you had to use the provided header files?
Also, the beginning/ending nodes would be better initiated at construction and deleted at destruction (along with all the other nodes)... and your professor wrote this??
I also wouldn't even use an iterator... next() and prev() would be functions I write to handle the task... or better yet just overload operators for that.
yeah I did a linked list in my CS1410 class in the spring and that one was less confusing than this one. It's like code has been omitted on purpose/forgotten. But we can't anything more/less than what's stated in the class definitions. Hence why I've called this blog Vague Assignment.
This happens to me a few times. Iterator I(Node head); is a function prototype and not a call to the constructor. The compiler thought you were trying to returna function pointer.
This is because you wrote the type name "Node" in the call to the constructor. So it was like you were declaring the parameter list of the function and not passing arguments.