Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 15 1.

Similar presentations


Presentation on theme: "Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 15 1."— Presentation transcript:

1 Spring 2008 Mark Fontenot mfonten@engr.smu.edu CSE 2341 - Honors Principles of Computer Science I Note Set 15 1

2 Overview 2 Stacks Queues

3 The Stack 3 STACK data structure that stores and retrieves items in a last-in-first-out manner only have access to element “on top” of the stack How is this different from a basic array?

4 Stack 4 Think of a stack of plates in a cafeteria 1 2 3 4 5 first in last infirst out last out

5 Applications of Stacks 5 Used during program execution Can use a stack to perform mathematical operations general: useful data structure for any algorithms that work with the last saved element of a series.

6 2 Types 6 Static Stacks Fixed size Implemented as Arrays Dynamic Stacks ability to grow and shrink as needed. Implemented as Linked lists

7 Basic Stack Operations 7 push(x) causes a value to be stored or pushed onto the stack Empty Stackpush(5) 5 push(10) 5 10 push(7) 5 10 7

8 Basic Stack Operations 8 pop(x) retrieves (and hence, removes) a value from the stack. 5 5 10 pop(x) 7 105 pop(x)

9 Basic Stack Operations 9 isFull() boolean function needed with fixed-size stacks prevents stack overflow from trying to push a value on when there is no more room isEmpty() boolean function needed for both fixed and dynamic stacks prevents any errors from the pop operation on an empty stack.

10 The Queue 10 queue data structure that stores and retrieves items in a first-in-first-out manner Elements are added to the rear and removed from the front

11 Queue 11 Think of a line of people waiting to check out front rear

12 Applications of Queue 12 Operating systems use queues to keep track of processes that are executing and in which order they are allowed to use the processor Printing queues Communications

13 2 Types 13 Static queues Fixed size Implemented as Arrays Dynamic queues ability to grow and shrink as needed. Implemented as Linked lists

14 Basic Queue Operations 14 enqueue(x) causes a value to be stored in the queue Empty Queue enqueue(5) 5 enqueue(10) 510 enqueue(7) 5 10 7 FrontRear Front Rear

15 Basic Queue Operations 15 dequeue() causes a value to be removed from the queue dequeue() 5 10 7 dequeue() 10 7

16 Problem 16 With static queue Every dequeue operation requires the elements behind it to move “forward” – very expensive Can overcome by allowing both front and rear indices to move but possible to run out of room in the array Over come by using a circular array OR dynamic queue.

17 DynamicQueue 17 class DynIntQueue { private: struct queueNode { int val; queueNode* next; }; queueNode* front; queueNode* rear; public: //See handout! }; 534 Null Front Rear

18 Fini 18 ?


Download ppt "Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 15 1."

Similar presentations


Ads by Google