410 likes | 521 Views
Data Structures LAB 5. TA: Nouf Al-Harbi nouf200@hotmail.com. Data structures lab 5. Queue. objectives. By t he end of this lab you will be able to : Use pointers and dynamic memory allocation Implement a queue class with all its operations. Part 1 :.
E N D
Data StructuresLAB 5 TA: Nouf Al-Harbi nouf200@hotmail.com
Data structures lab 5 Queue
objectives • By the end of this lab you will be able to : • Use pointers and dynamic memory allocation • Implement a queue class with all its operations Queue
Part 1 : Pointers & Dynamic Memory Allocation Queue
x number ch Addresses in Memory • 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
Obtaining Memory Addresses • 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;
What is a pointer variable? • A pointer variable is a variable whose value is the address of a location in memory. • 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
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
dereference (indirection) operator * int x; x = 12; int* ptr; ptr = &x; cout << *ptr; • NOTE: The value pointed to by ptr is denoted by *ptr 2000 12 x 3000 2000 ptr
Using the dereference operator int x; x = 12; int* ptr; ptr = &x; *ptr = 5; // changes the value at address ptr to 5 2000 12 5 x 3000 2000 ptr
The NULL Pointer • Sometimes we want a pointer to point to nothing • That’s called the null pointer • NOTE: • It is an error to dereference a pointer whose value is NULL. • Such an error may cause your program to crash • It is the programmer’s job to check for this. while (ptr != NULL) { . . .// ok to use *ptr here }
3000 Dynamic Memory Allocation • To achieve dynamic allocation of a variable, we use the C++ operator new, followed by the name of a data type: int* intPointer= new int; *intPointer=10; 3000 10 Queue
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 6000 6000 ptr
Dynamic Array Allocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, “Bye” ); ptr[ 1 ] = ‘u’; // a pointer can be subscripted cout << ptr[ 2] ; 6000 ‘u’ 6000 ‘B’ ‘y’ ‘e’ ‘\0’ ptr
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
Part 2 : Queue Queue
Queue • It is an ordered group of homogeneous items of elements. • Queues have two ends: • Elements are added at one end. • Elements are removed from the other end. • The element added first is also removed first (FIFO: First In, First Out). Queue
Queue Operations .. • MakeEmpty • Sets queue to an empty state. • IsEmpty • Determines whether the queue is currently empty. • IsFull • Determines whether the queue is currently full. • Enqueue (ItemTypenewItem) • Adds newItem to the rear of the queue. • Dequeue (ItemType& item) • Removes the item at the front of the queue and returns it in item. Queue
Queue Example .. Implement a Queue of user-defined integer elements Queue
Private data: maxQue Items [ 0 ] [ 1 ] [ 2 ] [MaxQue] front rear QueType class (data & methods members) • 4 data members • front • index of the front element • rear • index immediately past the rear element • MaxQue • Number of elements in queue • Items • Dynamic integer array implementation Queue
Private data: maxQue Items [ 0 ] [ 1 ] [ 2 ] [MaxQue] front rear QueType class (data & methods members) QueType ~QueType MakeEmpty Enqueue Dequeue IsEmpty IsFull LengthIs DisplayQueue Queue
Queue Example 0 0 1 1 2 2 3 3 4 4 f = 0 f = 0 Queue () x x.enqueue(5) 5 Queue Queue r = 0 r = 1 f f r r 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 f = 1 f = 0 f = 1 f = 0 x.enqueue(9) x.enqueue(2) x.enqueue (8) x.dequeue(y) 5 8 Queue Queue Queue Queue 5 8 8 8 2 2 2 9 r = 3 r = 4 r = 2 r = 3 f f f f r r r r
Queue Example 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 f = 3 f = 2 f = 3 f = 2 f = 2 x.dequeue(y) x.enqueue(1) x.enqueue(4) x.enqueue(10) 10 10 10 9 1 2 9 1 2 9 1 Queue Queue Queue Queue Queue 2 9 9 1 4 x.dequeue(y) r = 2 r = 1 r = 0 r = 1 r = 4 f f f r r r r f r f
Implementing QueType Class • the class declaration is placed in one file (header file) QueType.h • the implementation of all the methods is put in another fileQueType.cpp • the Main function is put in another file Queue.cpp • Foe each QueType.cpp & Queue.cpp we should inclusdeQueType.h file • #include “QueType. h” Queue
Implementing QueType Class 1- class declaration QueType.h Queue
QueType.h Queue
Implementing QueType Class 2- implementation of all the methods QueType.cpp Queue
QueType Counstructor Queue
~QueType Counstructor Queue
MakeEmpty Method Queue
IsFull Method • Full True • Not full false Inside constant functions we can’t change data values Queue
IsEmpty Method • Full True • Not full false Inside constant functions we can’t change data values Queue
LengthIs Method • Length of the queue Queue
Enqueue Method • The item that will be inserted Queue
Enqueue Method Queue
Dequeue Method • The item that will be deleted Queue
Dequeue Method Queue
Display Method Queue
Implementing QueType Class 3- Main function Queue.cpp Queue
In Main function .. Queue