Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Specification for iterator class to be used with a linked lists.
- //Author: Judy Mullins
- #ifndef ListIterator_h
- #define ListIterator_h
- #include <ostream>
- #include <iostream>
- #include <cstddef> //for definition of NULL
- #include "list.h"
- using namespace std;
- template <class Type>
- class ListIterator
- {
- public:
- ListIterator(const List<Type> & L)
- {
- header = L.header;
- current = L.isEmpty() ? header : header->nextLink;
- }
- ~ListIterator() { cout << "calling iterator destructor" << endl;}
- Type getEltAtPos(int i);// returns a copy of the element in the ith position if there is one;else returns error
- Type getEltAtCur(); // returns a copy of the element in the current position if there is one
- void insert(int x); // inserts at the beginning of the list; current is new element
- void insertCur(int x );// inserts x after current position, if current not NULL, and sets current to new element
- void remove(); // deletes the first element from a non-empty list
- bool remove(const Type & x);// deletes first occurrence of X if X found. Returns TRUE if success; FALSE otherwise.
- void removeCur(); // deletes the current element and sets current to the next element,
- //or null if there is no current element
- void advance(); // sets current to its successor
- void start(); // sets current to header node
- void setFirst(); // sets current to the first element of the list
- bool isFirst(); // returns TRUE if current is the first element of the list; else FALSE
- bool isLast(); // returns TRUE if current is the last element of the list; else FALSE
- bool hasNext(); //returns TRUE if current not NULL or header; FALSE otherwise.
- bool find(const Type& x);//sets current position to first node containing x. Returns TRUE if x found, else returns FALSE.
- // current unchanged if x not found
- bool isFound(const Type & x)const; //returns TRUE if x is in list; FALSE otherwise
- void print(ostream& out); //prints contents of list to output stream
- private:
- typename List<Type>::Node* current; //current position
- typename List<Type>::Node* header; //List header
- };
- ///////////////////////////////////////////////////////////////////////////////////////////////////////
- // L I S T I T E R A T O R I M P L E M E N T A T I O N
- ///////////////////////////////////////////////////////////////////////////////////////////////////////
- template <class Type>
- Type ListIterator<Type> :: getEltAtCur()
- {
- // returns a copy of the element in the current position if there is one
- if ( hasNext() )
- return current->info;
- else
- return 0; //error: current=header or current=NULL
- }
- template <class Type>
- Type ListIterator<Type> :: getEltAtPos(int pos)
- {
- typename List<Type>::Node *temp = header;
- if (current==header || current==NULL)
- {
- cout << "List is empty!!!" << endl;
- return 0;
- }
- for (int i=0; i < pos; i++)
- {
- temp = temp->nextLink;
- }
- return temp->info;
- }
- template <class Type>
- void ListIterator<Type> :: insert(int elem)
- {
- // inserts at the beginning of the list; current is new element
- cout << "inserting at head: " << elem << endl;
- typename List<Type>::Node* newnode = new typename List<Type>::Node(elem,header->nextLink);
- header->nextLink = newnode;
- current = newnode;
- }
- template <class Type>
- void ListIterator<Type> :: insertCur(int elem)
- {
- //inserts x after current position, if current not NULL, and sets current to new element
- cout << "inserting after current: " << elem << endl;
- typename List<Type>::Node* newnode = new typename List<Type>::Node(elem,NULL);
- newnode->info = elem;
- if(header->nextLink == NULL)
- {
- header->nextLink = newnode;
- }
- else
- {
- newnode->nextLink = current->nextLink;
- current->nextLink = newnode;
- }
- current = newnode;
- }
- template <class Type>
- bool ListIterator<Type> :: remove(const Type & elem)
- {
- //deletes first occurrence of elem if elem found. Returns TRUE if success; FALSE otherwise.
- //current is reset to header if successful deletion.
- typename List<Type>::Node* temp = header->next;
- typename List<Type>::Node* prev = header;
- while (temp != NULL && temp->elem != elem)
- {
- temp = temp->nextLink;
- prev = prev->nextLink;
- }
- if(temp != NULL)
- {
- prev->nextLink = temp->nextLink;
- delete temp;
- current = header;
- count--;
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- template <class Type>
- void ListIterator<Type> :: remove()
- { // deletes the first element from a non-empty list
- cout << "removing first element" << endl;
- typename List<Type>::Node* temp;
- temp = header->nextLink->nextLink;
- delete header->nextLink;
- header->nextLink = temp;
- }
- template <class Type>
- void ListIterator<Type> :: removeCur()
- {
- // deletes the current element and sets current to the next element,
- //or null if there is no current element
- cout << "removing current element" << endl;
- typename List<Type>::Node* temp = header->nextLink;
- while (temp->nextLink != current)
- {
- temp = temp->nextLink;
- }
- current = current->nextLink;
- delete temp->nextLink;
- temp->nextLink = current;
- }
- template <class Type>
- bool ListIterator<Type> :: hasNext()
- {
- //returns TRUE if current not NULL or header. Means current no longer points to a
- //node, or the list is empty
- if (current != NULL && current != header)
- return 1;
- else
- return 0;
- }
- template <class Type>
- void ListIterator<Type> :: advance()
- {
- current = current->nextLink;
- }
- template <class Type>
- void ListIterator<Type> :: start()
- {
- current = header;
- }
- template <class Type>
- void ListIterator<Type> :: setFirst()
- {
- // sets current to the first element of the list
- current = header->nextLink;
- }
- template <class Type>
- bool ListIterator<Type> :: isFirst()
- {
- return current == header->nextLink;
- }
- template <class Type>
- bool ListIterator<Type> :: isLast()
- {
- return current->nextLink == NULL;
- }
- template <class Type>
- bool ListIterator<Type> :: find(const Type& elem)
- {
- //sets current position to first node containing elem. Returns TRUE if elem found, else returns FALSE.
- //current unchanged if elem not found
- typename List<Type>::Node* temp = current; //save current in case elem not found
- setFirst(); //start iterator at beginning of list
- while ( hasNext() )
- {
- if ( getEltAtCur() != elem )
- advance();
- else
- return TRUE; //current now points to elem
- }
- current = temp; //elem not found; set current back to original position
- return FALSE;
- }
- template <class Type>
- bool ListIterator<Type> :: isFound(const Type & elem)const
- {
- typename List<Type>::Node* temp = header->nextLink;
- while (temp != NULL && temp->elem != elem)
- temp = temp->nextLink;
- return temp != NULL;
- }
- template <class Type>
- void ListIterator<Type> :: print(ostream& out)
- {
- out << "List contents: "<< endl;
- typename List<Type>::Node* ptr = header->nextLink;
- if(header->nextLink == NULL)
- out <<"List is empty!!" <<endl;
- else
- while (ptr != NULL)
- {
- out << ptr->info << endl;
- ptr = ptr->nextLink;
- }
- return;
- }
- #endif
Add Comment
Please, Sign In to add comment