CS 250 Computer Programming and Problem Solving - FALL 1998 

[ Home | Syllabus | Notes | Glossary | CS250 WEB Version Home Page ]


 

Lists of Things


 

Collections of similar Objects

Problem: How to maintain Student Lists and Doctor Lists and whatever else Lists?


Arrays

Advantages:

Disadvantages:


 

Linked Lists

Advantages:

Disadvantages:


List Object

Whatever you want it to be.

Here are some specifications for Lists.

 


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"
};

 


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
};

 


// *********************************************************
// 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"
};

 


/ 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
};

Copyright chris wild 1998.
For problems or questions regarding this website contact [Chris Wild (e-mail:wild@cs.odu.edu].
Last updated: October 21, 1998.