1 / 41

Data Structures LAB 5

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 :.

Download Presentation

Data Structures LAB 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data StructuresLAB 5 TA: Nouf Al-Harbi nouf200@hotmail.com

  2. Data structures lab 5 Queue

  3. 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

  4. Part 1 : Pointers & Dynamic Memory Allocation Queue

  5. 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

  6. 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;

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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 }

  12. Allocation of memory

  13. 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

  14. 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

  15. 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

  16. 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

  17. Part 2 : Queue Queue

  18. 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

  19. 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

  20. Queue Example .. Implement a Queue of user-defined integer elements Queue

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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 fileQueType.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

  26. Implementing QueType Class 1- class declaration QueType.h Queue

  27. QueType.h Queue

  28. Implementing QueType Class 2- implementation of all the methods QueType.cpp Queue

  29. QueType Counstructor Queue

  30. ~QueType Counstructor Queue

  31. MakeEmpty Method Queue

  32. IsFull Method • Full  True • Not full  false Inside constant functions we can’t change data values Queue

  33. IsEmpty Method • Full  True • Not full  false Inside constant functions we can’t change data values Queue

  34. LengthIs Method • Length of the queue Queue

  35. Enqueue Method • The item that will be inserted Queue

  36. Enqueue Method Queue

  37. Dequeue Method • The item that will be deleted Queue

  38. Dequeue Method Queue

  39. Display Method Queue

  40. Implementing QueType Class 3- Main function Queue.cpp Queue

  41. In Main function .. Queue

More Related