Download presentation
Presentation is loading. Please wait.
Published byKathryn Paul Modified over 9 years ago
1
1 Data Structures and Algorithms Stacks and Queues
2
Guidelines for Selecting ADT Operations Completeness -- Every ADT must be complete. It must include enough operations, including ADT- constructors, to build every possible value of the domain. Testability -- The preconditions of all ADT operations must be testable. The ADT must provide enough test operations for the client to check all preconditions of the ADT operations. 2
3
Types of ADT Operations to Include ADT-Constructors -- to build values of the type Test Operations -- to test some condition about an ADT value I/O Operations -- to input or output an ADT value Copy Operations -- to copy one ADT value to another ADT object Selector Operations (Access Functions) -- to retrieve a portion of an ADT value Destructor Operations -- to deallocate any dynamic data created by an ADT 3
4
4 Definition of ADT Stack A stack is a linear data structure with homogeneous components (elements), in which all insertions and deletions occur at one end, called the top of the stack. A stack is a LIFO “Last In, First Out” structure.
5
StackType ADT Operations StackType -- (Constructor) Creates an empty stack. IsEmpty -- Determines whether the stack is currently empty. IsFull -- Determines whether the stack is currently full. Push (ItemType newItem) -- Adds newItem to the top of the stack. Pop -- Removes the item at the top of the stack. Top -- Returns a copy of item currently at top of stack. 5
6
ADT Stack Operations Transformers –StackType –Push –Pop Observers –IsEmpty –IsFull –Top change state observe state 6
7
7 Vector Implementation of Stack StackType class StackType IsEmpty Top Pop Push IsFull Private data: top [MAX_ITEMS-1]. [ 2 ] [ 1 ] items [ 0 ]
8
Stack of int items top 3 [MAX_ITEMS-1]. [ 3 ] 789 [ 2 ] -56 [ 1 ] 132 items [ 0 ] 5670 8
9
top 3 [MAX_ITEMS-1]. [ 3 ] 3456.8 [ 2 ] -90.98 [ 1 ] 98.6 items [ 0 ] 167.87 9 Stack of float items
10
top 3 [MAX_ITEMS-1]. [ 3 ] Bradley [ 2 ] Asad [ 1 ] Rodrigo items [ 0 ] Max 10 Stack of string items
11
11 //---------------------------------------------------------- // SPECIFICATION FILE (bstack.h) //---------------------------------------------------------- #include "bool.h" #include "ItemType.h" // for MAX_ITEMS and // class ItemType definition class StackType { public: StackType( ); // Default constructor. // POST: Stack is created and empty. Boolean IsEmpty( ) const; // PRE: Stack has been initialized. // POST: FCTVAL == (stack is empty) Boolean IsFull( ) const; // PRE: Stack has been initialized. // POST: FCTVAL == (stack is full) 11
12
12 // SPECIFICATION FILE continued (bstack.h) void Push( ItemType newItem ); // PRE: Stack has been initialized and is not full. // Assigned (newItem). // POST: newItem is at the top of the stack. void Pop( ); // PRE: Stack has been initialized and is not empty. // POST: Top element has been removed from stack. ItemType Top( ) const; // PRE: Stack has been initialized and is not empty. // POST: FCTVAL == copy of item at top of stack. private: int top;// subscript of current top item ItemType items[MAX_ITEMS]; // array of ItemType }; 12
13
13 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) { letter = charStack.Top( ); charStack.Pop( ); } charStack.Push(‘K’); Tracing Client Code letter ‘V’
14
14 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) { letter = charStack.Top( ); charStack.Pop( ); } charStack.Push(‘K’); Tracing Client Code letter ‘V’ Private data: top -1 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ]
15
15 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) { letter = charStack.Top( ); charStack.Pop( ); } charStack.Push(‘K’); Tracing Client Code letter ‘V’ Private data: top 0 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ] ‘V’
16
16 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) { letter = charStack.Top( ); charStack.Pop( ); } charStack.Push(‘K’); Tracing Client Code letter ‘V’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] ‘C’ items [ 0 ] ‘V’
17
17 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) { letter = charStack.Top( ); charStack.Pop( ); } charStack.Push(‘K’); Tracing Client Code letter ‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’
18
18 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) { letter = charStack.Top( ); charStack.Pop( ); } charStack.Push(‘K’); Tracing Client Code letter ‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’
19
19 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) { letter = charStack.Top( ); charStack.Pop( ); } charStack.Push(‘K’); Tracing Client Code letter ‘S’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’
20
20 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) { letter = charStack.Top( ); charStack.Pop( ); } charStack.Push(‘K’); Tracing Client Code letter ‘S’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’
21
21 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) { letter = charStack.Top( ); charStack.Pop( ); } charStack.Push(‘K’); letter ‘S’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ End of Trace
22
22 //------------------------------------------------------- // IMPLEMENTATION FILE (bstack.cpp) //------------------------------------------------------ // Private data members of class: //int top; //ItemType items[MAX_ITEMS]; //------------------------------------------------------- #include “bool.h” #include “ItemType.h” StackType::StackType( ) //------------------------------------------------ // Default Constructor //------------------------------------------------ { top = -1; } 22
23
23 // IMPLEMENTATION FILE continued (bstack.cpp) //---------------------------------------------------------- Boolean StackType::IsEmpty( ) const //--------------------------------------------------- // PRE: Stack has been initialized. // POST: FCTVAL == (stack is empty) //--------------------------------------------------- { return ( top == -1 ); } Boolean StackType::IsFull( ) const //--------------------------------------------------- // PRE: Stack has been initialized. // POST: FCTVAL == (stack is full) //--------------------------------------------------- { return ( top == MAX_ITEMS-1 ); } 23
24
24 // IMPLEMENTATION FILE continued (bstack.cpp) //---------------------------------------------------------- void StackType::Push ( ItemType newItem ) //--------------------------------------------------- // PRE: Stack has been initialized and is not full. // POST: newItem is at the top of the stack. //--------------------------------------------------- { top++; items[top] = newItem; }
25
25 // IMPLEMENTATION FILE continued (bstack.cpp) //---------------------------------------------------------- ItemType StackType::Top( ) const //--------------------------------------------------- // PRE: Stack has been initialized && top >= 0. // POST: FCTVAL == copy of item at top of stack. //--------------------------------------------------- { return items[top]; }
26
26 // IMPLEMENTATION FILE continued (bstack.cpp) //---------------------------------------------------------- void StackType::Pop ( ) //--------------------------------------------------- // PRE: Stack has been initialized && top >= 0. // POST: Top element has been removed from stack. //--------------------------------------------------- { top--; }
27
27 Another Stack Implementation One advantage of an ADT is that the kind of implementation used can be changed. The array implementation of the stack has a weakness -- the maximum size of the stack is fixed at compile time. Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack.
28
28 class StackType StackType Pop Push IsFull IsEmpty Private data: topPtr cat dog ~StackType Top
29
29 Tracing Client Code letter ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) { letter = myStack.Top( ); myStack.Pop( ); } myStack.Push(‘K’);
30
30 Tracing Client Code letter ‘V’ Private data: topPtr NULL charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) { letter = myStack.Top( ); myStack.Pop( ); } myStack.Push(‘K’);
31
31 Tracing Client Code letter ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) { letter = myStack.Top( ); myStack.Pop( ); } myStack.Push(‘K’); Private data: topPtr
32
32 Tracing Client Code letter ‘C’ ‘V’ ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) { letter = myStack.Top( ); myStack.Pop( ); } myStack.Push(‘K’); Private data: topPtr
33
33 Tracing Client Code letter ‘S’ ‘C’ ‘V’ ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) { letter = myStack.Top( ); myStack.Pop( ); } myStack.Push(‘K’); Private data: topPtr
34
34 Tracing Client Code letter ‘V’ ‘S’ ‘C’ ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) { letter = myStack.Top( ); myStack.Pop( ); } myStack.Push(‘K’); Private data: topPtr
35
35 Tracing Client Code letter ‘S’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) { letter = myStack.Top( ); myStack.Pop( ); } myStack.Push(‘K’); Private data: topPtr ‘S’ ‘C’ ‘V’
36
36 Tracing Client Code letter ‘C’ ‘V’ ‘S’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) { letter = myStack.Top( ); myStack.Pop( ); } myStack.Push(‘K’); Private data: topPtr
37
37 Tracing Client Code letter ‘K’ ‘C’ ‘V’ ‘S’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) { letter = myStack.Top( ); myStack.Pop( ); } myStack.Push(‘K’); Private data: topPtr
38
38 // SPECIFICATION OF STACK (ustack.h) #include "bool.h" #include "ItemType.h" // for ItemType struct NodeType { ItemType info; NodeType* next; } Dynamically Linked Stack. info. next ‘A’ 6000
39
39 Pointer Dereferencing and Member Selection. info. next ‘A’ 6000 ptr. info. next ‘A’ 6000 *ptr ptr. info. next (*ptr).info ptr->info ‘A’ 6000
40
40 // SPECIFICATION OF STACK continued (ustack.cpp) class StackType { public: StackType( ); // constructor // Default constructor. // POST: Stack is created and empty. Boolean IsEmpty( ) const; // PRE: Stack has been initialized. // POST: FCTVAL == (stack is empty) Boolean IsFull( ) const; // PRE: Stack has been initialized. // POST: FCTVAL == (stack is full) StackType(const StackType& otherStk ); // Copy-constructor // POST: Created stack as deep copy of otherStk
41
41 // SPECIFICATION OF STACK continued (ustack.cpp) void Push( ItemType item ); // PRE: Stack initialized && Stack is not full. // POST: newItem is at the top of the stack. void Pop( ); // PRE: Stack initialized && Stack is not empty. // POST: Top element has been removed from stack. ItemType Top( ) const; // PRE: Stack initialized && Stack is not empty. // POST: FCTVAL == copy of item currently at top. ~StackType( ); // destructor // PRE: Stack has been initialized. // POST: Memory allocated for nodes has been // deallocated. private: NodeType char* topPtr ; }; 41
42
42 // DYNAMIC LINKED IMPLEMENTATION OF STACK (ustack.cpp) // member function definitions for class StackType StackType::StackType( ) // constructor { topPtr = NULL; } void StackType::IsEmpty( ) const // Returns true if there are no elements // on the stack; false otherwise { return ( topPtr == NULL ); }
43
Using operator new If memory is available in an area called the free store (or heap), operator new allocates the requested object, and returns a pointer to the memory allocated. The dynamically allocated object exists until the delete operator destroys it. 43
44
44 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem
45
45 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location
46
46 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location
47
47 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location ‘B’
48
48 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location ‘B’
49
49 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location ‘B’
50
50 void StackType::Push ( ItemType newItem ) // Adds newItem to the top of the stack. { NodeType* location; location = new NodeType; location->info = newItem; location->next = topPtr; topPtr = location; } Implementing Push
51
The object currently pointed to by the pointer is deallocated, and the pointer is considered unassigned. The memory is returned to the free store. Using operator delete 51
52
52 Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr
53
53 Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’
54
54 Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’
55
55 Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’
56
56 Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘X’ ‘C’ ‘L’ tempPtr ‘B’
57
57 void StackType::Pop ( ) // Removes element at the top of the stack. { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } void StackType::Top ( ) const // FCTVAL == copy of element at the top of stack. { return topPtr->info; }
58
58 Boolean StackType::IsFull( ) const // Returns true if there is no room for // another NodeType node on the free store; // false otherwise. { location = new NodeType ; if ( location == NULL ) return true; else { delete location; return false; }
59
59 Why is a destructor needed? When a local stack variable goes out of scope, the memory space for data member topPtr is deallocated. But the nodes that topPtr points to are not automatically deallocated. A class destructor is used to deallocate the dynamic memory pointed to by the data member.
60
60 StackType::~StackType( ) // destructor // Post: Stack is empty; all elements deallocated. { NodeType* tempPtr;; while ( topPtr != NULL ) { tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; }
61
61 class StackType StackType Pop Push IsFull IsEmpty Private data: topPtr ~StackType 20 30 Top
62
62 What happens... When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push IsFull IsEmpty Private data: topPtr ~StackType 20 30
63
63 void MyFunction( StackType SomeStack ) // FUNCTION CODE // Uses pass by value {. } Passing a class object by value
64
64 Pass by value makes a shallow copy 20 30 StackType MyStack; // CLIENT CODE. MyFunction( MyStack ); // function call Private data: topPtr 7000 MyStack SomeStack shallow copy Private data: 7000 6000 topPtr 7000
65
65 Shallow Copy vs. Deep Copy A shallow copy copies only the class data members, and does not make a copy of any pointed-to data. A deep copy copies not only the class data members, but also makes separately stored copies of any pointed-to data.
66
66 What’s the difference? A shallow copy shares the pointed to data with the original class object. A deep copy stores its own copy of the pointed to data at different locations than the data in the original class object.
67
67 Making a deep copy 20 30 Private data: 7000 6000 topPtr 7000 SomeStack 20 30 Private data: 5000 2000 topPtr 5000 MyStack deep copy
68
68 // FUNCTION CODE void MyFunction( StackType SomeStack ) // Uses pass by value { SomeStack.Pop( );. } WHAT HAPPENS IN THE SHALLOW COPY SCENARIO? Suppose MyFunction Uses Pop
69
69 MyStack.topPtr is left dangling StackType MyStack; // CLIENT CODE. MyFunction( MyStack ); Private data: topPtr 6000 MyStack SomeStack shallow copy ? 30 Private data: 7000 6000 topPtr 7000
70
70 MyStack.topPtr is left dangling ? 30 Private data: topPtr 6000 MyStack SomeStack shallow copy Private data: 7000 6000 topPtr 7000
71
71 As a result... This default method used for pass by value is not the best way when a data member pointer points to dynamic data. Instead, you should write what is called a copy constructor, which makes a deep copy of the dynamic data in a different memory location.
72
72 More about copy constructors When there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value. You do not call the copy constructor. Like other constructors, it has no return type. Because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition.
73
73 Copy Constructor Copy constructor is a special member function of a class that is implicitly called in these 3 situations: – passing object parameters by value, – initializing an object variable in its declaration, – returning an object as the return value of a function.
74
74 // DYNAMIC LINKED IMPLEMENTATION OF STACK class StackType { public: StackType( ); // Default constructor. // POST: Stack is created and empty. StackType( const StackType& otherStk ); // Copy constructor. // POST: this Stack is a deep copy of otherStk // Is implicitly called for pass by value.. ~StackType( ); // Destructor. // POST: Memory for nodes has been deallocated. private: NodeType* topPtr ; }; 74
75
75 CONSTRUCTOR COPY CONSTRUCTOR DESTRUCTOR Classes with Data Member Pointers Need
76
76 // COPY CONSTRUCTOR StackType::StackType( const StackType& otherStk ) { NodeType* fromPtr ; // traverses otherStk nodes NodeType* toPtr ; // traverses this Stack nodes if (otherStk.topPtr == NULL ) topPtr = NULL ; else // allocate memory to copy first node { topPtr = new NodeType; topPtr->info = otherStk.topPtr->info ; fromPtr = otherStk.topPtr->next ; toPtr = topPtr ; while ( fromPtr != NULL )// deep copy other nodes { toPtr->next = new NodeType; toPtr = toPtr->next ; toPtr->info = fromPtr->info ; fromPtr = fromPtr->next ; } toPtr->next = NULL ; } 76
77
77 What about the assignment operator? The default method used for assignment of class objects makes a shallow copy. If your class has a data member pointer to dynamic data, you should write a member function to overload the assignment operator to make a deep copy of the dynamic data.
78
78 // DYNAMIC LINKED IMPLEMENTATION OF STACK class StackType { public: StackType( ); // Default constructor. StackType( const StackType& otherStk ); // Copy constructor. void operator= ( StackType otherStk); // Overloads assignment operator.. ~StackType( ); // Destructor. private: NodeType* topPtr ; }; 78
79
79 // DYNAMIC LINKED IMPLEMENTATION OF STACK void StackType::operator= ( StackType otherStk ) // Overloads assignment operator // to create a deep copy of otherStk. {. } Overloading the assignment operator
80
80 C++ Operator Overloading Guides 1All operators except ::. sizeof ?: may be overloaded. 2At least one operand must be a class instance. 3You cannot change precedence, operator symbols, or number of operands. 4Overloading ++ and -- requires prefix form use by default, unless special mechanism is used. 5To overload these operators = ( ) [ ] member functions (not friend functions) must be used. 6An operator can be given multiple meanings if the data types of operands differ for different meanings.
81
Using overloaded binary operator= After defining member function operator= myStack = yourStack; Is translated by compiler into function call myStack.operator=(yourStack); 81
82
Using overloaded binary operator+ When a Member Function was defined myStack + yourStack myStack.operator+(yourStack) When a Friend Function was defined myStack + yourStack operator+(myStack, yourStack) 82
83
83 What is ADT Queue? A queue is a linear data structure with homogeneous components (elements), in which all insertions occur at the rear, and all deletions occur at the front. A queue is a FIFO “First In, First Out” structure.
84
Queue ADT Operations QueueType -- (Constructor) Creates an empty queue. IsEmpty -- Determines whether queue is currently empty. IsFull -- Determines whether queue is currently full. Enqueue (ItemType newItem) -- Adds newItem to the rear of queue. Dequeue -- Removes the item at the front of queue. Front -- Returns a copy of item at the front of queue. 84
85
ADT Queue Operations Transformers –QueueType –Enqueue –Dequeue Observers –IsEmpty –IsFull –Front change state observe state 85
86
86 class QueueType QueueType ~QueueType Enqueue Dequeue. Private Data: front rear ‘C’‘Z’ ‘T’
87
87 // SPECIFICATION OF QUEUE (uqueue.cpp) #include "ItemType.h" // for ItemType class QueueType { public: QueueType( ); // CONSTRUCTOR ~QueueType( ) ;// DESTRUCTOR Boolean IsEmpty( ) const; Boolean IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ); ItemType Front( ) const; QueueType( const QueueType& otherQue ); // COPY-CONSTRUCTOR private: NodeType* front; NodeType* rear; }; 87
88
88 // DYNAMIC LINKED IMPLEMENTATION OF QUEUE (uqueue.cpp) // member function definitions for class QueueType QueueType::QueueType( ) // CONSTRUCTOR { front = NULL; rear = NULL; } Boolean QueueType::IsEmpty( ) const { return ( front == NULL ) }
89
89 void QueueType::Enqueue( ItemType newItem ) // Adds newItem to the rear of the queue. // Pre: Queue has been initialized. // Queue is not full. // Post: newItem is at rear of queue. { NodeType* ptr; ptr = new NodeType; ptr->info = newItem; ptr->next = NULL; if ( rear == NULL ) front = ptr; else rear->next = ptr; rear = ptr; }
90
90 void QueueType::Dequeue( ) // Removes element from from front of queue // Pre: Queue has been initialized. // Queue is not empty. // Post: Front element has been removed from queue. { NodeType* tempPtr; tempPtr = front; front = front->next; if ( front == NULL ) rear = NULL; delete tempPtr; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.