void ListAsArray::Accept(Visitor& visitor) const { for(unsigned int i = 0; i < nCount && !visitor.IsDone(); i++) { visitor.Visit(*(array[i])); } }
void StackAsArray::Accept (Visitor& visitor) const { for (unsigned int i = 0; i < count && !visitor.IsDone (); ++i) { visitor.Visit (*array [i]); } }
// // This file contains the C++ code from Program 16.9 of // "Data Structures and Algorithms // with Object-Oriented Design Patterns in C++" // by Bruno R. Preiss. // // Copyright (c) 1998 by Bruno R. Preiss, P.Eng. All rights reserved. // // http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/programs/pgm16_09.cpp // void Digraph::TopologicalOrderTraversal (Visitor& visitor) const { Array<unsigned int> inDegree (numberOfVertices); for (Vertex::Number v = 0; v < numberOfVertices; ++v) inDegree [v] = 0; Iterator& p = Edges (); while (!p.IsDone ()) { Edge& edge = dynamic_cast<Edge&> (*p); ++inDegree [edge.V1 ()]; ++p; } delete &p; Queue& queue = *new QueueAsLinkedList (); queue.RescindOwnership (); for (Vertex::Number v = 0; v < numberOfVertices; ++v) if (inDegree [v] == 0) queue.Enqueue (SelectVertex (v)); while (!queue.IsEmpty () && !visitor.IsDone ()) { Vertex& vertex = dynamic_cast<Vertex&> (queue.Dequeue ()); visitor.Visit (vertex); Iterator& q = EmanatingEdges (vertex); while (!q.IsDone ()) { Edge& edge = dynamic_cast<Edge&> (*q); Vertex& to = edge.V1 (); if (--inDegree [to] == 0) queue.Enqueue (to); ++q; } delete &q; } delete &queue; }
void ListAsLinkedList::Accept(Visitor& visitor) const { DListElement<Object*> const* ptr; for(ptr = linkedList.GetHeadNode(); ptr != NULL && !visitor.IsDone(); ptr = ptr->GetNextNodeRight()) { visitor.Visit(*ptr->GetData()); } }
void BagAsLinkedList::Accept (Visitor& visitor) const { ListElement<Object*> const* ptr; for (ptr = list.Head (); ptr != 0 && !visitor.IsDone (); ptr = ptr->Next()) { visitor.Visit (*ptr->Datum ()); } }
void Tree::BreadthFirstTraversal (Visitor& visitor) const { Queue& queue = *new QueueAsLinkedList (); queue.RescindOwnership (); if (!IsEmpty ()) queue.Enqueue (const_cast<Tree&> (*this)); while (!queue.IsEmpty () && !visitor.IsDone ()) { Tree const& head = dynamic_cast<Tree const &> (queue.Dequeue ()); visitor.Visit (head.Key ()); for (unsigned int i = 0; i < head.Degree (); ++i) { Tree& child = head.Subtree (i); if (!child.IsEmpty ()) queue.Enqueue (child); } } delete &queue; }