CSI 1340 Introduction to Computer Science II Chapter






















![ACTUAL PARAMETER Stack. Type<int> num. Stack; top 3 [MAX_ITEMS-1] . . . items [3] ACTUAL PARAMETER Stack. Type<int> num. Stack; top 3 [MAX_ITEMS-1] . . . items [3]](https://slidetodoc.com/presentation_image_h2/a2c52269c5fa578b695e0252f348625b/image-23.jpg)
![ACTUAL PARAMETER Stack. Type<float> my. Stack; top 3 [MAX_ITEMS-1] . . . items [3] ACTUAL PARAMETER Stack. Type<float> my. Stack; top 3 [MAX_ITEMS-1] . . . items [3]](https://slidetodoc.com/presentation_image_h2/a2c52269c5fa578b695e0252f348625b/image-24.jpg)





















![Dynamic Array Allocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, Dynamic Array Allocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr,](https://slidetodoc.com/presentation_image_h2/a2c52269c5fa578b695e0252f348625b/image-46.jpg)

![Dynamic Array Deallocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, Dynamic Array Deallocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr,](https://slidetodoc.com/presentation_image_h2/a2c52269c5fa578b695e0252f348625b/image-48.jpg)





















- Slides: 69
CSI 1340 Introduction to Computer Science II Chapter 4 ADTs Stack and Queue
Stacks of Plates in Cafeteria New plates are “pushed” on top The next plate is “popped” from the top
What is a Stack? ü 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. ü A stack is a LIFO “last in, first out” structure. ü Entries are taken out of the stack in the reverse order of their insertion.
Are Stacks Useful? Web Surfing Lost in hyperlink space 1. CNN article on wild goats 2. Yahoo search for wild goat cheese recipe 3. Ebay bid on goat hair purse 4. How did I get here?
Stack ADT Operations ü Make. Empty -- Sets stack to an empty state. ü Is. Empty -- Determines whether the stack is currently empty. ü Is. Full -- Determines whether the stack is currently full. ü Push (Item. Type new. Item) -- Adds new. Item to the top of the stack. ü Pop (Item. Type& item) -- Removes the item at the top of the stack and returns it in item.
letter ‘V’ Tracing Client Code Private data: char letter = ‘V’; Stack. Type char. Stack; top char. Stack. Push(letter); [MAX_ITEMS-1]. . . [2] [1] items [ 0 ] char. Stack. Push(‘C’); char. Stack. Push(‘S’); if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘V’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top -1 [MAX_ITEMS-1]. . . [2] [1] items [ 0 ] char. Stack. Push(letter); char. Stack. Push(‘C’); char. Stack. Push(‘S’); if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘V’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 0 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); [2] char. Stack. Push(‘K’); [1] items [ 0 ] ‘V’ while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘V’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 1 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); [2] [1] ‘C’ items [ 0 ] ‘V’ char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘V’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 2 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘S’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘V’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 2 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘S’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘S’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 1 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘S’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘S’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 2 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘K’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘S’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 2 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘K’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘K’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 1 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘K’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘K’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 1 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘K’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘C’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 0 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘K’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘C’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top 0 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘K’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘V’ Tracing Client Code char letter = ‘V’; Stack. Type char. Stack; Private data: top -1 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘K’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
letter ‘V’ End of Trace char letter = ‘V’; Stack. Type char. Stack; Private data: top -1 char. Stack. Push(letter); char. Stack. Push(‘C’); [MAX_ITEMS-1] char. Stack. Push(‘S’); . . . [2] ‘K’ [1] ‘C’ items [ 0 ] ‘V’ if ( !char. Stack. Is. Empty( )) char. Stack. Pop(letter); char. Stack. Push(‘K’); while (!char. Stack. Is. Empty( )) char. Stack. Pop(letter);
Just How Generic Are We? typedef string Gen. Type; class Stack. Type { public: Stack. Type(); bool push(const Gen. Type& url); bool pop(Gen. Type& url); private: enum {MAXSTACK = 100}; int top; typedef int Gen. Type; class Stack. Type { public: Stack. Type(); bool push(const Gen. Type& url); bool pop(Gen. Type& url); private: enum {MAXSTACK = 100}; int top;
What is a Class Template? ü A class template allows the compiler to generate multiple versions of a class type by using type parameters. ü The formal parameter appears in the class template definition, and the actual parameter appears in the client code. Both are enclosed in pointed brackets, < >.
ACTUAL PARAMETER Stack. Type<int> num. Stack; top 3 [MAX_ITEMS-1] . . . items [3] 789 [2] -56 [1] 132 [0] 5670
ACTUAL PARAMETER Stack. Type<float> my. Stack; top 3 [MAX_ITEMS-1] . . . items [3] 3456. 8 [2] -90. 98 [1] 98. 6 [0] 167. 87
Using class templates ü The actual parameter to the template is a data type. Any type can be used, either built-in or user-defined. ü When using class templates, both the specification and implementation should be located in the same file (the header file), instead of in separate. h and. cpp files.
Recall that… ü Memory is like a shelf of slots content 2 slot # 522 523 524 525 üEach slot has a number and some content va
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; // 2 bytes float number; // 4 bytes char ch; // 1 byte 2000 x 2002 number 2006 ch
Obtaining Memory Addresses ü We can get the value of a slot. How about the slot # itself? ü 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;
Results 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; Output: Address of x is 0012 FF 7 C Address of number is 0012 FF 78 Address of ch is 0012 FF 74
What is a pointer variable? 1. A pointer variable is a variable whose value is the address of a location in memory. 2. 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 2000 int x; x = 12; int* ptr; ptr = &x; 12 x 3000 2000 ptr NOTE: Because ptr holds the address of x, we say that ptr “points to” x
* - dereference operator 2000 int x = 12; int* ptr = &x; 12 x 3000 2000 ptr cout<<“Addr of x: “<< ptr << endl; cout<<“Value of x: “<< *ptr << endl; NOTE: The value pointed to by ptr is denoted
Results int x = 12; int* ptr = &x; cout<<“Addr of x: “<< ptr << endl; cout<<“Value of x: “<< *ptr << endl; Output: Addr of x: 0012 FF 7 C Value of x: 12
Using the dereference operator 2000 int x; x = 12; 12 5 x int* ptr; ptr = &x; *ptr = 5; 3000 2000 ptr // changes the value // at address ptr to 5
Another Example char ch = ch; ‘A’; 4000 A Z ch char* q; q = &ch; 5000 4000 6000 4000 q p *q = ‘Z’; char* p; p = q; // the right side has value 4000 // now p and q both point to ch
The NULL Pointer ü There is a special pointer value called the “null pointer” denoted by NULL to represent a pointer variable that is not pointing to anything ü NULL is not a valid memory address ü It is an error to dereference (e. g. , *x) 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
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.
Using operator new If memory is available, new allocates the requested object or array and returns a pointer to (address of ) the memory allocated. Otherwise, the null pointer is returned. The dynamically allocated object exists until the delete operator destroys it.
New Operator ü Creates a new dynamic variable of the specified type and returns a pointer to this new dynamic variable. ü Example double *dptr; dptr = new double; *dptr = 3. 14259; dptr 3. 14259
Dynamically Allocated Data char* ptr; 2000 ptr = new char; *ptr = ‘B’; cout << *ptr;
Dynamically Allocated Data char* ptr; 2000 ptr = new char; *ptr = ‘B’; cout << *ptr; NOTE: Dynamic data has no variable name
Dynamically Allocated Data char* ptr; 2000 ptr = new char; *ptr = ‘B’; cout << ‘B’ *ptr; NOTE: Dynamic data has no variable name
Dynamically Allocated Data char* ptr; 2000 ? ptr = new char; *ptr = ‘B’; cout delete << *ptr; NOTE: Delete deallocates the memory pointed to by ptr.
Using operator delete 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.
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 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 ptr ‘B’ ‘u’ ‘y’ ‘e’ ‘ ’
Declaring a Dynamic Array ü Example int *arrayptr; // (1) Declare a pointer arrayptr = new int[10]; // (2) Create an array pointed // to by the pointer arrayptr[0] = 65; // (3) Assign a value to the first // memory location in the array NOTE: With dynamic arrays, omit the * when assigning values or otherwise accessing the array elements. After allocation, access is identical to statically allocated arrays.
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
Declaring a Dynamic Record ü Example struct Rec. Type { int i. Num; float f. Num; }; Rec. Type *rptr;
What is a Queue? ü 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). ü A queue is a FIFO “first in, first out” structure.
Queue ADT Operations ü Make. Empty -- Sets queue to an empty state. ü Is. Empty -- Determines whether the queue is currently empty. ü Is. Full -- Determines whether the queue is currently full. ü Enqueue (Item. Type new. Item) -- 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.
Accessing a Dynamic Record ü Example rptr = new Rec. Type; // Creates a record pointed to by // the pointer (*rptr). i. Num = 65; // Assigns a value to the first // field in the record rptr -> i. Num = 65; // Alternative syntax
Dereferencing Operator w/ Records ü Asterisk and the pointer name are enclosed in parentheses. ü Example cout << (*rptr). i. Num;
Arrow Operator w/Records ü -> (a hyphen followed by a greater-than symbol) ü For records and classes, it is equivalent to the use of the dereferencing operator and parentheses. ü Example cout << rptr -> i. Num;
Pointers as Value Parameters ü Example void my. Func (int* intptr); // Prototype for my. Func
What happens here? int* ptr = new int; *ptr = 3; 3 ptr = new int; *ptr = 4; // changes value of ptr 3 ptr 4
Memory Leak A memory leak occurs when dynamic memory (that was created using operator new) is left without a pointer to it by the 8 program ptr int* ptr = new int; *ptr = 8; int* ptr 2 = new int; ptr 2 *ptr 2 = -5; -5
Causing a Memory Leak int* ptr = new int; *ptr = 8; int* ptr 2 = new int; *ptr 2 = -5; 8 ptr -5 ptr 2 ptr = ptr 2; // here the 8 becomes inaccessible 8 ptr -5 ptr 2
A Dangling Pointer ü Occurs when two pointers point to the same object and delete is applied to one of them. int* ptr = new int; ptr *ptr = 8; int* ptr 2 = new int; ptr 2 *ptr 2 = -5; ptr = ptr 2; FOR EXAMPLE, 8 -5
Leaving a Dangling Pointer int* ptr = new int; *ptr = 8; int* ptr 2 = new int; *ptr 2 = -5; ptr = ptr 2; delete ptr 2; ptr 2 = NULL; 8 ptr -5 ptr 2 // ptr is left dangling 8 ptr NULL ptr 2
Classes and Dynamic Memory. Allocation
class Counter { public: Counter( ); Counter(int init. Count); void increment. Counter( ); void decrement. Counter( ); void set. Counter(int init. Count); int get. Counter( ); private: int *countptr; }; countptr
#include "counter. h” Counter: : Counter( ) constructor { countptr = new int; *countptr = 0; } // default countptr 0 *countptr
Counter: : Counter(int init. Count) // parameterized constructor { countptr = new int; *countptr = init. Count; } countptr init. Count *countptr
void Counter: : set. Counter(int init. Count) { *countptr = init. Count; } countptr init. Count *countptr
void Counter: : increment. Counter( ) { *countptr = *countptr + 1; } countptr 0 1 *countptr
void Counter: : decrement. Counter( ) { *countptr = *countptr - 1; } countptr 1 0 *countptr
int Counter: : get. Counter( ) const { return *countptr; } countptr 0 *countptr
Destructor Syntax ü Example Counter: : ~Counter( ) // Implementation for destructor { delete countptr; // memory location pointed to by } // countptr is returned to heap NOTE: the pointer is NOT returned to the heap