I am brand new to C++ and I need to make a doubly linked list using 3 classes. I was given the 3 header files for the classes, and I need to create 3 complete .cpp files for each of the classes. I have ListNode.h, List.h, and ListIterator.h. I am having trouble with where to start on the ListIterator and ListNode class. I don't really know how to make the constructors for those classes.
#ifndef LISTNODE_H
#define LISTNODE_H
// needed because NULL is part of std namespace
#include <iostream>
usingnamespace std;
class ListNode {
public:
ListNode(); //Constructor
private:
int value;
ListNode *next, *previous; //for doubly linked lists
friendclass List; // List needs to be able to access/change
// ListNode's next and previous pointers
friendclass ListItr; // ListItr needs to access/change
// Not writing a destructor is fine in this case since there is no
// dynamically allocated memory in this class
};
#endif
#include <iostream>
#include "ListNode.h"
#include "ListItr.h"
usingnamespace std;
class ListItr;
class List {
public:
List(); //Constructor
List(const List& source); //Copy Constructor
~List(); //Destructor
List& operator=(const List& source); //Equals Operator
bool isEmpty() const; //Returns true if empty; else false
void makeEmpty(); //Removes all items except blank head and tail
ListItr first(); //Returns an iterator that points to
//the ListNode in the first position
ListItr last(); //Returns an iterator that points to
//the ListNode in the last position
void insertAfter(int x, ListItr position);
//Inserts x after current iterator position p
void insertBefore(int x, ListItr position);
//Inserts x before current iterator position p
void insertAtTail(int x); //Insert x at tail of list
void remove(int x); //Removes the first occurrence of x
ListItr find(int x); //Returns an iterator that points to
// the first occurrence of x, else
// return a blank iterator
int size() const; //Returns the number of elements in the list
private:
ListNode *head, *tail; //indicates beginning and end of the list
int count; //#of elements in list
friendclass ListItr;
};
// printList: non-member function prototype
void printList(List& source, bool direction);
//prints list forwards when direction is true
//or backwards when direction is false
#endif
#ifndef LISTITR_H
#define LISTITR_H
#include "ListNode.h"
#include "List.h"
class ListItr {
public:
ListItr(); //Constructor
ListItr(ListNode* theNode); // One parameter constructor
bool isPastEnd() const; //Returns true if past end position
// in list, else false
bool isPastBeginning() const;//Returns true if past first position
// in list, else false
void moveForward(); //Advances current to next position in list
//(unless already past end of list)
void moveBackward(); //Moves current back to previous position
// in list (unless already past beginning of
// list)
int retrieve() const; //Returns item in current position
private:
ListNode* current; //holds the position in the list
friendclass List; // List class needs access to “current”
// ListNode's private data members
};
#endif