//--------------------------------------------
// class DoubleEndedList
// a variation on Lists - can add elements
// to the end as well as to front
//--------------------------------------------
#ifndef DoubleEndedList_H
#define DoubleEndedList_H
#include "list.h"
#include <iostream>
usingnamespace std;
template <class T>
class DoubleEndedList : public List<T>
{
public:
// constructors
DoubleEndedList ();
// override the following methods from class List
void add(int value);
void clear();
void removeFirst();
// add a new element to the end of the List
void addToEnd(int value);
//******************* iterator *******************
class iterator;
friendclass iterator;
class iterator
{
//DoubleEndedList & d;
typename DoubleEndedList::Link *link;
public:
//iterator(DoubleEndedList<T> &_d) : d(_d) {}
iterator(typename DoubleEndedList<T>::Link *_d) : link(_d) {}
iterator(typename DoubleEndedList<T>::Link *_d, bool) : link(_d) {}
voidoperator++() { // Prefix form
//require(index < s.top,
//"iterator moved out of range");
//return s.stack[++index];
d.removeFirst();
}
//T operator++(int) { // Postfix form
// require(index < s.top,
// "iterator moved out of range");
// return s.stack[index++];
//}
};
//******************* end iterator ***************
iterator begin() { return iterator(*this->first); }
iterator end() { return iterator(*this->last); }
protected:
// data area -- Link to end
Link * last;
};
#endif
//------------------------------------------------
// class DoubleEndedList implementation
//------------------------------------------------
template <class T>
DoubleEndedList<T>::DoubleEndedList() :List<T>(), last(NULL)
{}
template <class T>
void DoubleEndedList<T>::add(int val)
{
// add an element to the front of a double
// ended List only need to handle addition to
// empty List
if (isEmpty()) {
List<T>::add(val);
last=first;
}
else
List<T>::add(val);
}
template <class T>
void DoubleEndedList<T>::clear()
{
// delete all values from collection
List<T>::clear();
// then set the pointer to the last element to z
last = NULL;
}
template <class T>
void DoubleEndedList<T>::removeFirst(){
// remove the first element
List<T>::removeFirst();
// if we remove last element
if (isEmpty())
last = NULL;
}
template <class T>
void DoubleEndedList<T>::addToEnd(int val)
{
// add a new element to end of a double ended List
if (last != NULL)
{
last->next = new Link(val,NULL);
last = last->next;
}
// otherwise, just add to front
else
add(val);
}
//------------------------------------------------
// class List
// arbitrary size Lists
// permits insertion and removal
// only from the front of the List
//------------------------------------------------
#ifndef List_H
#define List_H
#define NULL 0
template <class T>
class List
{
protected:
//--------------------------------------------
// inner class link
// a single element for the linked List
//--------------------------------------------
class Link
{
public:
// constructor
Link( int linkValue, Link* nextPtr);
Link(const Link &);
// data areas
int value;
Link * next;
}; //end of class Link
public:
// constructors
List();
List(const List&);
~List();
// operations
void add( int value);
int firstElement() const;
int includes(constint &value) const;
bool isEmpty() const;
void removeFirst();
void clear();
protected:
// data field
Link* first;
};
#endif
//------------------------------------------------
// class Link implementation
//------------------------------------------------
template <class T>
List<T>::Link::Link(int val, Link* nxt) : value(val), next(nxt) {}
template <class T>
List<T>::Link::Link(const Link& source) : value(source.value),next(source.next) {}
//--------------------------------------------
// class List implementation
//--------------------------------------------
template <class T>
List<T>::List(): first(NULL)
{
// no further initialization
}
template <class T>
List<T>::List(const List &l)
{
Link *src, *trg;
if(l.first==NULL)
first=NULL;
else
{
first= new Link((l.first)->value, NULL);
src=l.first;
trg=first;
while(src->next!=NULL)
{
trg->next= new Link((src->next)->value, NULL);
src=src->next;
trg=trg->next;
}
}
}
template <class T>
List<T>::~List()
{
clear();
}
template <class T>
void List<T>::clear()
{
// empty all elements from the List
Link* next;
for (Link * p=first; p != NULL; p=next)
{
// delete the element pointed to by p
next=p->next;
p->next=NULL;
delete p;
}
// mark that the List contains no elements
first=NULL;
}
template <class T>
bool List<T>::isEmpty() const
{
// test to see if the List is empty
// List is empty if the pointer to the first
// Link is null
return first == NULL;
}
template <class T>
void List<T>::add( int val)
{
//Add a new value to the front of a Linked List
first=new Link(val, first);
if(first==NULL)
throw"failed in memory allocation";
}
template <class T>
int List<T>::firstElement() const
{
// return first value in List
if (isEmpty())
throw"the List is empty, no first Element";
return first->value;
}
template <class T>
int List<T>::includes(constint &val) const
{
// loop to test each element
for (Link* p=first; p!=NULL ; p=p->next)
if (val == p->value)
returntrue;
// not found
returnfalse;
}
template <class T>
void List<T>::removeFirst()
{
// make sure there is a first element
if(isEmpty())
throw"the List is empty, no Elements to move";
// save pointer to the removed node
Link* p=first;
// reassign the first node
first= p->next;
p->next = NULL;
// recover memory used by the first element
delete p;
}
//--------------------------------------------
// class DoubleEndedList
// a variation on Lists - can add elements
// to the end as well as to front
//--------------------------------------------
#ifndef DoubleEndedList_H
#define DoubleEndedList_H
#include "list.h"
#include <iostream>
usingnamespace std;
template <class T>
class DoubleEndedList : public List<T>
{
public:
// constructors
DoubleEndedList ();
// override the following methods from class List
void add(int value);
void clear();
void removeFirst();
// add a new element to the end of the List
void addToEnd(int value);
//******************* iterator *******************
class iterator;
friendclass iterator;
class iterator
{
//DoubleEndedList & d;
typename DoubleEndedList::Link *link;
public:
//iterator(DoubleEndedList<T> &_d) : d(_d) {}
iterator(typename DoubleEndedList::Link *_d) : link(_d) {} //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
iterator(typename DoubleEndedList::Link *_d, bool) : link(_d) {} //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
voidoperator++() { // Prefix form
//require(index < s.top,
//"iterator moved out of range");
//return s.stack[++index];
//d.removeFirst();//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<I comment this line out _ I think it is a typo
}
//T operator++(int) { // Postfix form
// require(index < s.top,
// "iterator moved out of range");
// return s.stack[index++];
//}
};
//******************* end iterator ***************
iterator begin() { return iterator(this->first); } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
iterator end() { return iterator(this->last); }//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
protected:
// data area -- Link to end
typename List<T>::Link * last; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
};
#endif
//------------------------------------------------
// class DoubleEndedList implementation
//------------------------------------------------
template <class T>
DoubleEndedList<T>::DoubleEndedList() :List<T>(), last(NULL)
{}
template <class T>
void DoubleEndedList<T>::add(int val)
{
// add an element to the front of a double
// ended List only need to handle addition to
// empty List
if (List <T>::isEmpty()) { //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
List<T>::add(val);
last= List<T>::first;
}
else
List<T>::add(val);
}
template <class T>
void DoubleEndedList<T>::clear()
{
// delete all values from collection
List<T>::clear();
// then set the pointer to the last element to z
last = NULL;
}
template <class T>
void DoubleEndedList<T>::removeFirst(){
// remove the first element
List<T>::removeFirst();
// if we remove last element
if (List<T>::isEmpty()) //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
last = NULL;
}
template <class T>
void DoubleEndedList<T>::addToEnd(int val)
{
// add a new element to end of a double ended List
if (last != NULL)
{
last->next = newtypename DoubleEndedList::Link(val,NULL); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
last = last->next;
}
// otherwise, just add to front
else
add(val);
}