CS 250 Computer Programming and Problem Solving - FALL 1998 |
---|
[ Home | Syllabus | Notes | Glossary | CS250 WEB Version Home Page ]
For each list object introduced earlier, write a program for outputing each element in the list.
I repeat the specifications below and them give the code for printing the list to the screen.
//*********************************
// This version allows to set the position of the current item
// and to retrieve that item, delete that item or insert a new item there.
// Uses a linked list implementation which can iterate efficiently over the
// list if done in sequential order (if the implementation so chooses)
class List
{
public:
//***** constructors and destructor:
List( );
List(const List& L); // copy constructor
~List( ); //destructor
//***** operations:
int Length();
//POST: returns length of this list
bool Insert(ItemType NewItem);
//POST: if true, NewItem is added to list after the current position
// changes the position of all items from the current postion +1 and up
// current position is that of newly inserted item.
// returns false if NewItem could not be inserted
bool Delete();
//PRE: list is not empty, Position is not 0
//POST: if true, deletes item in the current position
// reordering the position of the items following the deleted item
// current position is kept the same unless the last item is deleted
// in which case it is set to the new last element or 0 for empty list
// If false, List stays the unchanged (pre not met)
ItemType Retrieve();
//PRE: List is not Empty, Position is not 0
//POST: returns copy of item at the current position
bool SetPosition(int NewPosition);
//PRE: NewPosition is between 0 and length of list
//POST: if true, sets current position to NewPosition
// false if precondition not met. (current postion is unchanged)
List& operator=(const List& L);
//POST: does a deep copy assignment
private:
ptrType PtrTo(int Position);
// POST: returns pointer to the node in Position or NULL
// if Position is 0 or out of range
int Size;
ptrType Head;
int Position; // position between 0 and Size
ptrType Current; // pointer to item in
"Position"
};
List myList; // ... do something which puts elements in the list for(int i = 1; i <= myList.Length();i++) { myList.SetPosition(i); cout << myList.Retrieve( ); // assumes ItemType overloads insertion } |
//**********************************************************
// template class which uses a dynamically allocated array
// even though it uses the function names head and tail to set
// the current position
// Uses private function to dynamically grow the array as needed
// can insert, retrieve, delete and update item at the current position
// interesting feature is the overload of the postfix ++ and -- operators
// to move the current position
template <class T>
class TList
{
friend ostream& operator<< (ostream& os, TList<T> l);
public:
TList( ); // Construct an empty list.
TList(const TList<T>& l); // copy constructor
~TList( ); // destructor
TList<T>& operator= (const TList<T>& l); // assignment overload
void Insert(T x); //
Insert x at current position
void Remove( ); // Delete the node at the current position
void Head( ); // Move current location to the head
void Tail( ); // Move current location to the tail
TList<T>& operator++ (int); // Move current to next
position (postfix)
TList<T>& operator-- (int); // Move current to prior position (postfix)
T Retrieve( ) const; // Return the element at the current position
void Update(T x); // Store x in current location
int Length(); // Return the size of the list
private:
T *value; // pointer to the array
int size, // number of elements in the list (<= max)
max, // size of the array
current; // current index in the array (0 <= current < size)
void Grow( ); // Grow the array to twice its
size
};
TList<int> myList; // .... do something to put elements in the list myList.Head(); for(int i = 1; i <= myList.Length(); i++) { cout << myList.Retrieve(); myList++; // increment position in list } // here is another way, using the comma operator to have multiple inits/increments for(int i = 1,myList.Head(); i <= myList.Length(); i++,myList++) { cout << myList.Retrieve(); } |
// ********************************************************* // Ordered List Object // Insert stores item in sorted order // Assume that ItemType supports comparison operators (see Student) // Maintains a current position in the list. // Can set this position, can retrieve and delete at the current position // List Invariant, If the list is not empty, Position is between 1 and size of // the List (inclusive) // // Linked List Implementation (although the user doesn't need to know) // Modelled after implementation in Data Abstraction and Problem Solving in // c++ Frank Carrano // ********************************************************* #ifndef ORDERED_LIST_H #define ORDERED_LIST_H #include "Student.h" typedef Student ItemType; // desired-type struct listNode; // defined in implementation file typedef listNode* ptrType; // pointer to node class OrderedList { public: //***** constructors and destructor: OrderedList(); OrderedList(const OrderedList& L); // copy constructor ~OrderedList(); //destructor //***** operations: int Length(); //POST: returns length of this list bool Insert(ItemType NewItem); //PRE: OrderedList is ordered. // Ordering is defined by ItemTypes '<' operator //POST: if true, NewItem is added to list to keep list ordered // position is set to the position of the newly inserted item // returns false if NewItem could not be inserted bool Delete(); //PRE: list is not empty //POST: if true, deletes item in the current position // current position is kept the same unless the last item is deleted // in which case it is set to the new last element or 0 for empty list // If false, OrderedList stays the unchanged (pre not met) ItemType Retrieve(); //PRE: OrderedList is not Empty, //POST: returns copy of item at the current position bool SetPosition(int NewPosition); //PRE: NewPosition is between 1 and length of list //POST: if true, sets current position to NewPosition // false if precondition not met. (current postion is unchanged) OrderedList& operator=(const OrderedList& L); //POST: does a deep copy assignment private: ptrType PtrTo(int Position); // POST: returns pointer to the node in Position or NULL // if Position is out of range int Size; ptrType Head; int Position; // position between 0 and Size ptrType Current; // pointer to item in "Position" };
OrderedList myList; // this example is same as for the first LIST object // .... do something to put elements in the list for(int i = 1; i <= myList.Length();i++) { myList.SetPosition(i); cout << myList.Retrieve( ); // assumes ItemType overloads insertion } |
/ A node in a singly linked list. template <class T> class Tnode { friend class Tlist<T>; public: Tnode(); Tnode( const T & val ); Tnode<T> * Next() const; friend ostream & operator <<(ostream & os, const Tnode<T> & N); private: T value; // data stored in node Tnode * next; // points to next node }; // A singly linked list of Tnode objects. template <class T> class Tlist { public: Tlist(); ~Tlist(); int Advance(); // Return 0 if current position is already at // the end of the list; otherwise, increment // current and return 1. void Append( const T & nodeVal ); // Add a new node to the end of the list. void Clear(); // Remove all nodes. T Get() const; // Get the data at the current position. void GoLast(); // Set current to the last node in the list. void GoTop(); // Set current to the header node. void InsertAfter( const T & nodeVal ); // Insert new node after current one. int IsEmpty() const; // Return 1 if the list is empty; otherwise, // return 0. void Prepend( const T & nodeVal ); // Insert a node at the beginning of the list. void Replace( const T & newVal ); // Replace the data in the current node. friend ostream & operator <<(ostream &, const Tlist<T> &); private: Tnode<T> * head; // dummy head node Tnode<T> * tail; // dummy tail node Tnode<T> * current; // current position };
TList<int> myList; // ... do something to put items in the list if(!myList.IsEmpty() { myList.GoTop(); do { cout << myList.Get(); while(myList.Advance()); } |