11
Stacks and Queues Stacks and Queues Introduction to Computing Introduction to Computing Science and Programming I Science and Programming I

Stacks and Queues Introduction to Computing Science and Programming I

Embed Size (px)

Citation preview

Page 1: Stacks and Queues Introduction to Computing Science and Programming I

Stacks and QueuesStacks and Queues

Introduction to Computing Introduction to Computing Science and Programming IScience and Programming I

Page 2: Stacks and Queues Introduction to Computing Science and Programming I

Data StructuresData Structures

Data structures are different Data structures are different structures that can be used to store structures that can be used to store data. Different structures will data. Different structures will organize it differently causing organize it differently causing differences in how the data is differences in how the data is accessed and changed.accessed and changed.

The only data structure we’ve used The only data structure we’ve used in Python is the list.in Python is the list.

Page 3: Stacks and Queues Introduction to Computing Science and Programming I

Abstract Data TypeAbstract Data Type

We’re going to talk about a couple We’re going to talk about a couple abstract data types. We’re only going to abstract data types. We’re only going to look at them in a very general, abstract look at them in a very general, abstract way. We won’t worry about details of how way. We won’t worry about details of how to implement the structures in Python. We to implement the structures in Python. We will only discuss how the data is organized will only discuss how the data is organized and what operations can be performed on and what operations can be performed on it.it.

The structures can be used in any The structures can be used in any programming language.programming language.

Page 4: Stacks and Queues Introduction to Computing Science and Programming I

QueuesQueues

A queue is a simple data structure that A queue is a simple data structure that is works similar to a line of people (a is works similar to a line of people (a queue) waiting to be helped by a queue) waiting to be helped by a cashier.cashier.

You can only add or remove items one You can only add or remove items one at a time.at a time.

It is a First In First Out, FIFO data It is a First In First Out, FIFO data structure. The first item you put in will structure. The first item you put in will be the first item that leaves the queue.be the first item that leaves the queue.

Page 5: Stacks and Queues Introduction to Computing Science and Programming I

QueuesQueues

\ ---------------------------- /\ ---------------------------- /Entrance ->Entrance -> -> Exit -> Exit

/ --------------------------- \/ --------------------------- \

Every queue has to basic operations.Every queue has to basic operations. queue(Item) adds the item to the queuequeue(Item) adds the item to the queue dequeue() removes an item from the queuedequeue() removes an item from the queue

Some operations that many queues have, but Some operations that many queues have, but aren’t required.aren’t required. size() number of elements in the queuesize() number of elements in the queue isEmpty() check whether there are any itemsisEmpty() check whether there are any items peek() Look at the next item to be removed without peek() Look at the next item to be removed without

removing itremoving it

Page 6: Stacks and Queues Introduction to Computing Science and Programming I

QueuesQueues

\ ---------------------------- /\ ---------------------------- /Entrance ->Entrance -> 5 10 8 -> Exit 5 10 8 -> Exit

/ --------------------------- \/ --------------------------- \

This is what the queue would look like after the This is what the queue would look like after the following operations.following operations. queue(8)queue(8) queue(10)queue(10) queue(5)queue(5)

If the items we’re removed using the dequeue If the items we’re removed using the dequeue operation, they would be returned in the order operation, they would be returned in the order they were put it, 8, then 10, and finally 5.they were put it, 8, then 10, and finally 5.

Page 7: Stacks and Queues Introduction to Computing Science and Programming I

QueuesQueues

Remember that these queues and any Remember that these queues and any other structure can store numbers, other structure can store numbers, strings, or objects of any kind.strings, or objects of any kind.

Applications of the queue have to do Applications of the queue have to do with resource management and with resource management and requests. For example if you send requests. For example if you send several documents to be printed, they several documents to be printed, they will be put in a queue before being will be put in a queue before being printed one at a time in the order printed one at a time in the order they were added.they were added.

Page 8: Stacks and Queues Introduction to Computing Science and Programming I

StacksStacks

A stack is a simple data structure A stack is a simple data structure that works similarly to a stack of that works similarly to a stack of papers. You can only add elements papers. You can only add elements to the top and also only remove to the top and also only remove them from the top.them from the top.

This makes it a Last In First Out, This makes it a Last In First Out, LIFO, structure because the last item LIFO, structure because the last item added will be the first one removed.added will be the first one removed.

Page 9: Stacks and Queues Introduction to Computing Science and Programming I

StacksStacks

Enter -> -> ExitEnter -> -> Exit| || || || || || |--------------

Every stack has to basic operations.Every stack has to basic operations. push(Item) adds the item to the top of the stack, “pushing” the push(Item) adds the item to the top of the stack, “pushing” the

others downothers down pop() removes an item from the top of the stackpop() removes an item from the top of the stack

Some operations that many stacks have, but aren’t Some operations that many stacks have, but aren’t required.required. size() number of elements in the stacksize() number of elements in the stack isEmpty() check whether there are any itemsisEmpty() check whether there are any items peek() Look at the next item to be removed without removing peek() Look at the next item to be removed without removing

itit

Page 10: Stacks and Queues Introduction to Computing Science and Programming I

StacksStacks

Enter -> -> ExitEnter -> -> Exit| 5 | 5 ||| 10| 10 ||| 8| 8 ||--------------------

This is what the stack would look like after the following This is what the stack would look like after the following operations.operations. push(8)push(8) push(10)push(10) push(5)push(5)

If the items we’re removed using the pop operation, they If the items we’re removed using the pop operation, they would be returned in the reverse of the order they were put would be returned in the reverse of the order they were put it, 5, then 10, and finally 8.it, 5, then 10, and finally 8.

Page 11: Stacks and Queues Introduction to Computing Science and Programming I

StacksStacks

Stacks are used in a wide array of Stacks are used in a wide array of applications. They can be used to applications. They can be used to evaluate equations as well as keep evaluate equations as well as keep track of function calls in a program track of function calls in a program that is running.that is running.