Stacks and Queues Introduction to Computing Science and

  • Slides: 11
Download presentation
Stacks and Queues Introduction to Computing Science and Programming I

Stacks and Queues Introduction to Computing Science and Programming I

Data Structures n n Data structures are different structures that can be used to

Data Structures n n Data structures are different structures that can be used to store data. Different structures will organize it differently causing differences in how the data is accessed and changed. The only data structure we’ve used in Python is the list.

Abstract Data Type n n We’re going to talk about a couple abstract data

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

Queues n n n A queue is a simple data structure that is works

Queues n n n A queue is a simple data structure that is works similar to a line of people (a queue) waiting to be helped by a cashier. You can only add or remove items one at a time. It is a First In First Out, FIFO data structure. The first item you put in will be the first item that leaves the queue.

Queues  -------------- / Entrance -> -> Exit / --------------  n Every queue

Queues -------------- / Entrance -> -> Exit / -------------- n Every queue has to basic operations. n n n queue(Item) adds the item to the queue dequeue() removes an item from the queue Some operations that many queues have, but aren’t required. n n n size() number of elements in the queue is. Empty() check whethere any items peek() Look at the next item to be removed without removing it

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

Queues -------------- / Entrance -> 5 10 8 -> Exit / -------------- n This is what the queue would look like after the following operations. n n queue(8) queue(10) queue(5) If the items we’re removed using the dequeue operation, they would be returned in the order they were put it, 8, then 10, and finally 5.

Queues n n Remember that these queues and any other structure can store numbers,

Queues n n Remember that these queues and any other structure can store numbers, strings, or objects of any kind. Applications of the queue have to do with resource management and requests. For example if you send several documents to be printed, they will be put in a queue before being printed one at a time in the order they were added.

Stacks n n A stack is a simple data structure that works similarly to

Stacks n n A stack is a simple data structure that works similarly to a stack of papers. You can only add elements to the top and also only remove them from the top. This makes it a Last In First Out, LIFO, structure because the last item added will be the first one removed.

Stacks Enter -> n Every stack has to basic operations. n n n |

Stacks Enter -> n Every stack has to basic operations. n n n | | | ------- -> Exit push(Item) adds the item to the top of the stack, “pushing” the others down pop() removes an item from the top of the stack Some operations that many stacks have, but aren’t required. n n n size() number of elements in the stack is. Empty() check whethere any items peek() Look at the next item to be removed without removing it

Stacks Enter -> n This is what the stack would look like after the

Stacks Enter -> n This is what the stack would look like after the following operations. n n -> Exit | 5 | | 10 | | 8 | ----- push(8) push(10) push(5) If the items we’re removed using the pop operation, they would be returned in the reverse of the order they were put it, 5, then 10, and finally 8.

Stacks n Stacks are used in a wide array of applications. They can be

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