Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’

Similar presentations


Presentation on theme: "Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’"— Presentation transcript:

1 Data Structures (part 2)

2 Stacks

3 An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’ item I gave you on hold; this is more important, so rush it out first.” We’ll end up doing the last item first (last in, first out).

4 The Stack Push Pop

5 Idea: a “Last In, First Out” (LIFO) data structure Behaviors: Push: Add to top of stack Pop: Remove from top of stack (and return that top value) Top: Return topmost item (but leave it on the stack) Is_Full: is it full? Is_Empty: is it empty? Initialize: empty stack Properties

6 The Stack as a Logical Data Structure The stack is an idea It implies a set of logical behaviors It can be implemented various ways –Using a linked list or a tree or an array In this example, we’ll focus on dynamic implementations using dynamic data...

7 Stacks: Dynamic Implementation A linked list with restricted set of operations to change its state: only modified from one end 4 17 42 top

8 public void Push (int value) // Purpose: push one value onto stack // Pre: top points to null-terminated list // Post: the list has one node added public int Pop (void) // Purpose: Pop a value off stack; // if empty, throw an exception // Pre: top points to a null-terminated list // Post: list has one fewer, return top data Application Programmer Interface

9 Push Create new node Add it to the front 17 42 top

10 Push Create new node Add it to the front 42 17 top 4 temp ?

11 Push Create new node Add it to the front 42 17 top 4 temp

12 Push Create new node Add it to the front 4 42 17 top

13 Pop Capture the first value (to return) Remove the first node (move top to next) 4 42 17 top

14 Pop Capture the first value (to return) Remove the first node (move top to next) 4 42 17 top value = 4

15 Pop Capture the first value (to return) Remove the first node (move top to next) 4 42 17 top value = 4

16 Pop Capture the first value (to return) Remove the first node (move top to next) 42 17 top value (4) is returned

17 Summary: Stack Allow us to model “last-in, first-out” behavior Can be implemented using different data types Behavior is important (and defines a Stack) –Push to the front –Pop from the front –(from the same end)

18 Queues

19 Some Examples Waiting in line –At the grocery store –At the movies Ordering items –Bills to pay –Making pizzas We can use a queue to model each of these.

20 The Queue Enqueue Dequeue

21 Idea: a “First In, First Out” (FIFO) data structure Behaviors: Enqueue: Add to end of queue Dequeue: Remove from front of queue (and return that front value) Top: Return front-most item (but leave it in the queue) Is_Full: is it full? Is_Empty: is it empty? Initialize: empty queue Properties of Queues

22 The Queue as a Logical Data Structure The queue is an idea It implies a set of logical behaviors It can be implemented various ways –Using a linked list or a tree or an array In this example, we’ll focus on dynamic implementations using dynamic data...

23 Queues: Dynamic Implementation A linked list with restricted set of operations to change its state: only modified by adding to one end and deleting from the other. 4 17 42 tail head

24 Application Programmer Interface public void Enqueue (int value) // Pre: Q is initialized // Purpose: Add a value to the tail // Post: Q has new element at tail public int Dequeue (void) // Pre: Q is initialized // Purpose: Remove first value from Q and // return it via ‘value’ OR indicate // that Q is empty via exception // Post: element that was at head has been // removed OR throw exception

25 Enqueue Create a new node with data Add it to the end –Update references as needed 4 17 tail head

26 Enqueue Create a new node with data Add it to the end –Update references as needed 4 17 tail head 42 temp

27 Enqueue Create a new node with data Add it to the end –Update references as needed 4 17 tail head 42 temp

28 Enqueue Create a new node with data Add it to the end –Update references as needed 4 17 tail head 42 temp

29 Dequeue Capture the first value (to return) Remove the first node (move head to next) 4 17 head 42 tail

30 Dequeue Capture the first value (to return) Remove the first node (move head to next) value = 4 4 17 head 42 tail

31 Dequeue Capture the first value (to return) Remove the first node (move head to next) value = 4 4 17 head 42 tail

32 Dequeue Capture the first value (to return) Remove the first node (move head to next) value (4) is returned 17 head 42 tail

33 An Example Program Reverse Polish Notation (RPN) Calculator Enter two operands Perform operation Maintain stack of numbers

34 Summary: Queues Allow us to model “first-in, first-out” behavior Can be implemented using different data types Behavior is important (and defines a Queue) –Enqueue to end –Dequeue from front –(or vice-versa – just do at opposite ends)

35 FIN


Download ppt "Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’"

Similar presentations


Ads by Google