DATA STRUCTURES LAB 5 TA Nouf AlHarbi nouf
DATA STRUCTURES LAB 5 TA: Nouf Al-Harbi nouf 200@hotmail. com
DATA STRUCTURES LAB 5 Queue
objectives 3 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
4 Part 1 : Pointers & Dynamic Memory Allocation Queue
Addresses in Memory 5 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 x 2002 number 2006 ch
Obtaining Memory Addresses 6 The address of a non-array variable can be obtained by using the address-of operator &. int float char x; number; 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? 7 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 8 2000 int x; x = 12; 12 x int* ptr; ptr = &x; 3000 2000 ptr NOTE: Because ptr holds the address of x, we say that ptr “points to” x
9 dereference (indirection) operator * 2000 int x; x = 12; 12 x int* ptr; ptr = &x; 3000 cout ptr << 2000 *ptr; NOTE: The value pointed to by ptr is denoted by *ptr
10 Using the dereference operator int x; x = 12; int* ptr; ptr = &x; 2000 12 x 3000 2000 *ptr = 5; ptr // changes the value at address ptr to 5 5
The NULL Pointer 11 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 }
Allocation of memory 12 STATIC ALLOCATION is the allocation of memory space at compile time. DYNAMIC ALLOCATION is the allocation of memory space at run time by using operator new.
Dynamic Memory Allocation 13 To achieve dynamic allocation of a variable, we use the C++ operator new, followed by the name of a data type: 3000 int* int. Pointer= new int; 10 *int. Pointer=10; 3000 Queue
Dynamic Array Allocation 14 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 ptr
Dynamic Array Allocation 15 char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, “Bye” ); ptr[ 1 ] = ‘u’; // a pointer can be subscripted cout << ptr[ 2] ; 6000 ptr ‘B’ ‘u’ ‘y’ ‘e’ ‘ ’
Dynamic Array Deallocation 16 char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, “Bye” ); ptr[ 1 ] = ‘u’; delete ptr; ? ptr // deallocates array pointed to by ptr // ptr itself is not deallocated, but // the value of ptr is considered unassigned
17 Part 2 : Queue
Queue 18 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. . 19 Make. Empty � Is. Empty � Determines whether the queue is currently full. Enqueue (Item. Type new. Item) � Determines whether the queue is currently empty. Is. Full � Sets queue to an empty state. Adds new. Item to the rear of the queue. Dequeue (Item. Type& item) � Removes the item at the front of the queue and returns it in item. Queue
20 Queue Example. . Implement a Queue of userdefined integer elements Queue
21 Que. Type class (data & methods members) 4 data members � front � � rear index immediately past the rear element Max. Que � index of the front element Number of elements in queue Private data: front max. Que Items rear [Max. Que] Items [0] [1] [2] Dynamic integer array implementation Queue
22 Que. Type class (data & methods members) Que. Type ~Que. Type Private data: Make. Empty front Enqueue Dequeue rear Is. Empty max. Que Items [0] [1] [2] [Max. Que] Is. Full Length. Is Display. Queue
Queue Example 23 0 Queue () x x. enqueue (8) x. enqueue (2) x. dequeue (y) x. enqueue (9) 2 3 4 Queue f r 0 x. enqueue (5) 1 Queue 1 2 3 4 f 0 2 4 8 1 f 4 f=1 r=3 r 3 2 f=0 r=3 2 2 8 4 r 3 2 f 1 Queue 3 2 8 f=0 r=2 r 2 1 Queue 0 3 8 5 f=0 r=1 r 1 5 f 0 Queue r=0 5 f 0 f=0 4 9 r f=1 r=4
Queue Example 24 0 x. dequeue (y) Queue 9 3 f 2 10 4 3 10 1 4 9 r 1 2 f 3 r f 1 4 9 4 r=4 1 9 2 r 1 f=2 r 4 3 f 2 10 0 x. enqueue (4) 1 4 9 2 0 x. dequeue (y) 1 f 2 Queue 3 2 r 0 x. enqueue (10) 2 Queue 0 x. enqueue (1) 1 1 f=2 r=0 f=2 r=1 f=3 r=2
Implementing Que. Type Class 25 � the class declaration is placed in one file (header file) Que. Type. h � the implementation of all the methods is put in another file Que. Type. cpp � the Main function is put in another file Queue. cpp Foe each Que. Type. cpp & Queue. cpp we should inclusde Que. Type. h file � #include “Que. Type. h” Queue
26 Implementing Que. Type Class 1 - class declaration Que. Type. h Queue
Que. Type. h 27 Queue
28 Implementing Que. Type Class 2 - implementation of all the methods Que. Type. cpp Queue
Que. Type Counstructor 29 Queue
~Que. Type Counstructor 30 Queue
Make. Empty Method 31 Queue
Is. Full Method 32 Is. Ful l • Full True • Not full false Inside constant functions we can’t change data values Queue
Is. Empty Method 33 Is. Empt y • Full True • Not full false Inside constant functions we can’t change data values Queue
Length. Is Method 34 Length. I s • Length of the queue Queue
Enqueue Method 35 • The item that will be inserted Enqueu e Queue
Enqueue Method 36 Queue
Dequeue Method 37 • The item that will be deleted Dequeu e Queue
Dequeue Method 38 Queue
Display Method 39 Queue
40 Implementing Que. Type Class 3 - Main function Queue. cpp Queue
In Main function. . 41 Queue
- Slides: 41