Download presentation
Presentation is loading. Please wait.
Published byMeghan Scott Modified over 8 years ago
1
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta Barneva, SUNY Fredonia for CSIT 341
2
2 Topics l Stacks l More generics: templates l Pointers: an introduction l Dynamically allocated arrays l Queues
3
3 What is a Stack? l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of stack items can take place only at the top of the stack. l A stack is a LIFO “last in, first out” structure.
4
4 Stacks of Boxes and Books TOP OF THE STACK
5
Stack ADT Operations l MakeEmpty -- Sets stack to an empty state. l IsEmpty -- Determines whether the stack is currently empty. l IsFull -- Determines whether the stack is currently full. l Push (ItemType newItem) -- Adds newItem to the top of the stack. l Pop (ItemType& item) -- Removes the item at the top of the stack and returns it in item. 5
6
ADT Stack Operations Transformers n MakeEmpty n Push n Pop Observers n IsEmpty n IsFull change state observe state 6
7
7 Class Interface Diagram StackType class StackType MakeEmpty Pop Push IsFull IsEmpty Private data: top [MAX_ITEMS-1]. [ 2 ] [ 1 ] items [ 0 ]
8
//---------------------------------------------------------- // SPECIFICATION FILE (stack.h) //---------------------------------------------------------- #include "ItemType.h" // for MAX_ITEMS and // class ItemType definition class StackType { public: StackType( ); // Default constructor. // POST: Stack is created and empty. void MakeEmpty( ); // PRE: None. // POST: Stack is empty. bool IsEmpty( ) const; // PRE: Stack has been initialized. // POST: Function value = (stack is empty) 8
9
// SPECIFICATION FILE continued (Stack.h) bool IsFull( ) const; // PRE: Stack has been initialized. // POST: Function value = (stack is full) void Push( ItemType newItem ); // PRE: Stack has been initialized and is not full. // POST: newItem is at the top of the stack. void Pop( ItemType& item ); // PRE: Stack has been initialized and is not empty. // POST: Top element has been removed from stack. // item is a copy of removed element. private: int top; ItemType items[MAX_ITEMS];// array of ItemType }; 9
10
10 Private data value ComparedTo Print Initialize class ItemType ItemType Class Interface Diagram
11
//------------------------------------------------------- // IMPLEMENTATION FILE (Stack.cpp) //------------------------------------------------------ // Private data members of class: //int top; //ItemType items[MAX_ITEMS]; //------------------------------------------------------- #include "ItemType.h " StackType::StackType( ) //------------------------------------------------ // Default Constructor //------------------------------------------------ { top = -1; } 11
12
// IMPLEMENTATION FILE continued (Stack.cpp) //---------------------------------------------------------- void StackType::MakeEmpty( ) //--------------------------------------------------- // PRE: None. // POST: Stack is empty. //--------------------------------------------------- { top = -1; } 12
13
// IMPLEMENTATION FILE continued (Stack.cpp) //---------------------------------------------------------- bool StackType::IsEmpty( ) const //--------------------------------------------------- // PRE: Stack has been initialized. // POST: Function value = (stack is empty) //--------------------------------------------------- { return ( top == -1 ); } bool StackType::IsFull( ) const //--------------------------------------------------- // PRE: Stack has been initialized. // POST: Function value = (stack is full) //--------------------------------------------------- { return ( top == MAX_ITEMS-1 ); } 13
14
// IMPLEMENTATION FILE continued (Stack.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; } 14
15
// IMPLEMENTATION FILE continued (Stack.cpp) //------------------------------------------------------------- void StackType::Pop ( ItemType& item ) //------------------------------------------------------ // PRE: Stack has been initialized and is not empty. // POST: Top element has been removed from stack. // item is a copy of removed element. //------------------------------------------------------ { item = items[top]; top--; } 15
16
16 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code Private data: top [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ] letter ‘V’
17
17 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘V’ Private data: top -1 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ]
18
18 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘V’ Private data: top 0 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ] ‘V’
19
19 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘V’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] ‘C’ items [ 0 ] ‘V’
20
20 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘V’ Private data: top 2 [ 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( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’
22
22 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘S’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’
23
23 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘S’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’
24
24 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘S’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’
25
25 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘K’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’
26
26 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘K’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’
27
27 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘C’ Private data: top 0 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’
28
28 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘C’ Private data: top 0 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’
29
29 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); Tracing Client Code letter ‘V’ Private data: top -1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’
30
30 charletter = 'V'; StackType charStack; charStack.Push(letter); charStack.Push('C'); charStack.Push('S'); if ( !charStack.IsEmpty( )) charStack.Pop(letter); charStack.Push('K'); while (!charStack.IsEmpty( )) charStack.Pop(letter); End of Trace letter ‘V’ Private data: top -1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’
31
31 What is a Class Template? l A class template allows the compiler to generate multiple versions of a class type by using type parameters. l The formal parameter appears in the class template definition, and the actual parameter appears in the client code. Both are enclosed in pointed brackets,.
32
StackType numStack; top 3 [MAX_ITEMS-1]. [ 3 ] 789 [ 2 ] -56 [ 1 ] 132 items [ 0 ] 5670 32 ACTUAL PARAMETER
33
StackType myStack; top 3 [MAX_ITEMS-1]. [ 3 ] 3456.8 [ 2 ] -90.98 [ 1 ] 98.6 items [ 0 ] 167.87 33 ACTUAL PARAMETER
34
StackType nameStack; top 3 [MAX_ITEMS-1]. [ 3 ] Bradley [ 2 ] Asad [ 1 ] Rodrigo items [ 0 ] Max 34 ACTUAL PARAMETER
35
//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS //-------------------------------------------------------- #include "bool.h" #include "ItemType.h" // for MAX_ITEMS and ItemType template // formal parameter list class StackType { public: StackType( ); void MakeEmpty( ); bool IsEmpty( ) const; bool IsFull( ) const; void Push( ItemType item ); void Pop( ItemType& item ); private: int top; ItemType items[MAX_ITEMS]; }; 35
36
//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS cont’d //-------------------------------------------------------- // member function definitions for class StackType template // formal parameter list StackType ::StackType( ) { top = -1; }. template // formal parameter list void StackType ::Push ( ItemType newItem ) { top++; items[top] = newItem;// STATIC ARRAY IMPLEMENTATION } 36
37
37 StackType IntStack; StackType FloatStack; StackType StrStack; IntStack.Push(35); FloatStack(3.1415927); The definition of StackType begins with the Template ItemType is called the formal parameter to the template In the definition or instantiation the data type name enclosed in the angle brackets is called the actual parameter So StackType IntStack gets transformed by the compiler to StckType_int IntStack Using class templates
38
l The actual parameter to the template is a data type. Any type can be used, either built-in or user-defined. l When using class templates, both the specification and implementation should be located in the same file, instead of in separate.h and.cpp files. 38
39
39 Pointers: Recall that... char msg [ 8 ]; msg is the base address of the array. We say msg is a pointer because its value is an address. It is a pointer constant because the value of msg itself cannot be changed by assignment. It “points” to the memory location of a char. msg [0] [1] [2] [3] [4] [5] [6] [7] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ 6000
40
40 Addresses in Memory l When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is the address of the variable. int x; float number; char ch; 2000 2002 2006 x number ch
41
41 Obtaining Memory Addresses l The address of a non-array variable can be obtained by using the address-of operator &. int x; float number; char ch; cout << "Address of x is " << &x << endl; cout << "Address of number is " << &number << endl; cout << "Address of ch is " << &ch << endl;
42
42 What is a pointer variable? l A pointer variable is a variable whose value is the address of a location in memory. l To declare a pointer variable, you must specify the type of value that the pointer will point to. For example, int* ptr; // ptr will hold the address of an int char* q; // q will hold the address of a char
43
43 What is a pointer variable? How do you get ptr something to point to? We use the "address-of-operator" & How do we recover what ptr is pointing to? And we use the "dereference operator" * int alpha = 10; int* intPointer; intPointer = α y = *intPointer;
44
44 Left and Right Value All variables are made up of two components: A l-value which is the location in memory where the variable is stored. A r-value which is what is stored in that location So the statement alpha = y; Says that we take the r-value of y and store it in the l-value of alpha. The address of operator returns the l-value of a variable. The dereference operator returns the r-value of a variable's l- value l A pointer is a l-value variable - it's r-value is the l-value of another data object
45
45 Left and Right Value The address of operator returns the l-value of a variable. int* ptr; int alpha = 4; If the compiler assigns the variable alpha a location of 3500. What is it's l-value? r-value? The compiler assigns ptr the location 5680. ptr = α What is ptr 's l-value? r-value?
46
46 Another Example The dereference operator returns the r- value of a variable's l-value. The compiler assigns ptr location 7500 *ptr = 35; l What has happened?
47
47 Using a pointer variable int x; x = 12; int* ptr; ptr = &x; NOTE: Because ptr holds the address of x, we say that ptr “points to” x 2000 12 x 3000 2000 ptr
48
48 2000 12 x 3000 2000 ptr int x; x = 12; int* ptr; ptr = &x; cout << *ptr; NOTE: The value pointed to by ptr is denoted by *ptr Unary operator * is the dereference (indirection) operator
49
49 int x; x = 12; int* ptr; ptr = &x; *ptr = 5; // changes the value // at address ptr to 5 Using the dereference operator 2000 12 5 x 3000 2000 ptr
50
50 char ch; ch = 'A'; char* q; q = &ch; *q = 'Z'; char* p; p = q; // the right side has value 4000 // now p and q both point to ch Another Example 4000 A Z ch 5000 6000 4000 4000 q p
51
51 Addressing The statement *ptr = 25; is indirect addressing. The statement alpha = 10; is direct addressing.
52
52 C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double
53
53 The NULL Pointer There is a pointer constant 0 called the “null pointer” denoted by NULL in stddef.h But NULL is not memory address 0. NOTE: It is an error to dereference a pointer whose value is NULL. Such an error may cause your program to crash, or behave erratically. It is the programmer’s job to check for this. while (ptr != NULL) {... // ok to use *ptr here }
54
54 Allocation of memory STATIC ALLOCATION Static allocation is the allocation of memory space at compile time. DYNAMIC ALLOCATION Dynamic allocation is the allocation of memory space at run time by using operator new.
55
55 3 Kinds of Program Data l STATIC DATA: memory allocation exists throughout execution of program. static long SeedValue; l AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function. DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using unary operators new and delete
56
56 Life time of a variable l The lifetime of a global variable is the entire execution time of the program The lifetime of a local variable is the execution time of the block in which it was declared The lifetime of a dynamically allocated varaible is the time it is allocated using new to the time it is deallocated using delete A local variable can retain its value if it declared using the attribute “static”
57
Using operator new If memory is available in an area called the free store (or heap), operator new allocates the requested object or array, and returns a pointer to (address of ) the memory allocated. Otherwise, the null pointer 0 is returned. The dynamically allocated object exists until the delete operator destroys it. 57
58
58 2000 ptr Dynamically Allocated Data char* ptr; ptr = new char; *ptr = 'B'; cout << *ptr;
59
59 Dynamically Allocated Data char* ptr; ptr = new char; *ptr = 'B'; cout << *ptr; NOTE: Dynamic data has no variable name 2000 ptr
60
60 Dynamically Allocated Data char* ptr; ptr = new char; *ptr = 'B'; cout << *ptr; NOTE: Dynamic data has no variable name 2000 ptr ‘B’
61
61 Dynamically Allocated Data char* ptr; ptr = new char; *ptr = 'B'; cout << *ptr; delete ptr; 2000 ptr NOTE: Delete deallocates the memory pointed to by ptr. ?
62
The object or array currently pointed to by the pointer is deallocated, and the pointer is considered unassigned. The memory is returned to the free store. Square brackets are used with delete to deallocate a dynamically allocated array of classes. Using operator delete 62
63
63 Operator -> The arrow operator “->” (for lack of a better term) is used to dereference a pointer and access a member of a class struct MoneyType { int dollars; int cents; } MoneyType* moneyPtr = new MoneyType; moneyPtr->dollars = 33; moneyPtr->cents = 3300;
64
Some C++ pointer operations Precedence Higher -> Select member of class pointed to Unary: ++ -- ! * new delete Increment, Decrement, NOT, Dereference, Allocate, Deallocate + - Add Subtract >= Relational operators == != Tests for equality, inequality Lower = Assignment
65
65 Dynamic Array Allocation char *ptr; // ptr is a pointer variable that // can hold the address of a char ptr = new char[ 5 ]; // dynamically, during run time, allocates // memory for 5 characters and places into // the contents of ptr their beginning address ptr 6000
66
66 Dynamic Array Allocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, "Bye" ); ptr[ 1 ] = 'u'; // a pointer can be subscripted cout << ptr[ 2] ; ptr 6000 ‘B’ ‘y’ ‘e’ ‘\0’ ‘u’
67
67 Dynamic Array Deallocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, "Bye" ); ptr[ 1 ] = 'u'; delete ptr; // deallocates array pointed to by ptr // ptr itself is not deallocated, but // the value of ptr is considered unassigned ptr ?
68
68 int* ptr = new int; *ptr = 3; ptr = new int; // changes value of ptr *ptr = 4; What happens here? 3 ptr 3 ptr 4
69
69 Memory Leak A memory leak occurs when dynamic memory (that was created using operator new ) has been left without a pointer to it by the programmer, and so is inaccessible. int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; How else can an object become inaccessible? 8 ptr -5 ptr2
70
70 Causing a Memory Leak int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2; // here the 8 becomes inaccessible 8 ptr -5 ptr2 8 ptr -5 ptr2
71
71 occurs when two pointers point to the same object and delete is applied to one of them. int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2; A Dangling Pointer 8 ptr -5 ptr2 FOR EXAMPLE,
72
72 int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2; delete ptr2; // ptr is left dangling ptr2 = NULL; Leaving a Dangling Pointer 8 ptr -5 ptr2 8 ptr NULL ptr2
73
73 Dynamically Allocated Arrays l Use the following syntax int* arrayInt; arrayInt = new int[100]; x = arrayInt [4]; y = arrayInt [k]; delete [] arrayInt;
74
74 DYNAMIC ARRAY IMPLEMENTATION StackType ~StackType Push Pop. class StackType Private Data: top 2 maxStack 5 items 50 43 80 items [0] items [1] items [2] items [3] items [4]
75
//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS #include "ItemType.h" // for ItemType template class StackType { public: StackType( ); StackType( int max );// PARAMETERIZED CONSTRUCTOR ~StackType( ) ;// DESTRUCTOR... bool IsFull( ) const; void Push( ItemType item ); void Pop( ItemType& item ); private: int top; int maxStack; ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION }; 75
76
//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS cont’d //-------------------------------------------------------- template StackType ::StackType( ) //DEFAULT CONSTRUCTOR { maxStack = 500; top = -1; items = new ItemType[500]; // dynamically allocates array } template StackType ::StackType( int max ) // PARAMETERIZED { maxStack = max; top = -1; items = new ItemType[max]; // dynamically allocates array } 76
77
//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS cont’d //-------------------------------------------------------- template StackType ::~StackType( ) { delete [ ] items; // deallocates array }. template bool StackType ::IsFull( ) { return (top == maxStack - 1) } 77
78
78 What is a Queue? l Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). l A queue is a FIFO “first in, first out” structure.
79
Queue ADT Operations l MakeEmpty -- Sets queue to an empty state. l IsEmpty -- Determines whether the queue is currently empty. l IsFull -- Determines whether the queue is currently full. l Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue. l Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item. 79
80
ADT Queue Operations Transformers n MakeEmpty n Enqueue n Dequeue Observers n IsEmpty n IsFull change state observe state 80
81
81 DYNAMIC ARRAY IMPLEMENTATION QueType ~QueType Enqueue Dequeue. class QueType Private Data: front 1 rear 4 maxQue 5 items ‘C’ ‘X’ ‘J’ items [0] [1] [2] [3] [4] RESERVED
82
//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR QUEUE #include "ItemType.h" // for ItemType template class QueType { public: QueType( ); QueType( int max );// PARAMETERIZED CONSTRUCTOR ~QueType( ) ;// DESTRUCTOR... bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); private: int front; int rear; int maxQue; ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION }; 82
83
83 Implementation of Queues There are two basic approaches: A linear list and a variation on a circular list
84
84 LISTS The only method that would be affected is the dequeue method A BCDEF After a dequeue method is applied B CDEF
85
85 Using Linear List Using a linear list the deque method is template void QueType::Dequeue(ItemType& item) { item = item[0]; QueCount--; for (int k=0; k < QueCount; k++) items[k] = items[k+1]; } This is what complexity?
86
86 Circular List The variation on a circular list uses two variables - front and rear front points to the front of the queue rear points to the back of the queue
87
87 Circular Lists Enqueue('A'); front = 0,rear = 0 A.Enqueue('B'); front = 0,rear = 1 AB.Enqueue('C'); front = 0,rear = 2 ABC.Enqueue('D'); front = 0,rear = 3 ABCD.Dequeue(item); front = 1,rear = 3 BCD What is the complexity of this approach?
88
88 Circular Lists We want queue elements to wrap around We will use the modulo operation to do this –rear = (rear + 1) % maxQue; –front = (front + 1) % maxQue;
89
89 Circular Lists Queue Constructors template QueType ::QueType() // Default { maxQue = 501; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; // dynamically allocates }
90
90 Circular Lists Queue Constructors template QueType ::QueType( int max ) // PARAMETERIZED { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; // dynamically allocates }
91
91 Circular Lists Destructor template QueType ::~QueType( ) { delete [ ] items; // deallocates array }
92
92 Circular Lists template bool QueType ::IsEmpty( ) { return (rear == front) }
93
93 Circular Lists template bool QueType ::IsFull( ) {// WRAP AROUND return ( (rear + 1) % maxQue == front ) }
94
94 Circular Lists Enqueue and Dequeue operations template void QueType ::Enqueue( ItemType item ) { rear = (rear + 1) % maxQue; items[rear] = newItem; }
95
95 Circular Lists Enqueue and Dequeue operations template void QueType ::Dequeue( ItemType& item ) { front = (front + 1) % maxQue; item = items[front]; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.