Stack & Queues COP 3502.

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Building Java Programs Chapter 14
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Data Structures and Algorithms (60-254)
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks, Queues & Deques CSC212.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
TCSS 342, Winter 2005 Lecture Notes
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Stacks and Queues Introduction to Computing Science and Programming I.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
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’
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
COSC160: Data Structures: Lists and Queues
MEMORY REPRESENTATION OF STACKS
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stack and Queue.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Lesson Objectives Aims
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Data Structures and Algorithms for Information Processing
Stacks CS-240 Dick Steflik.
Stacks and Queues CSE 373 Data Structures.
Lecture 9: Stack and Queue
Presentation transcript:

Stack & Queues COP 3502

Stacks A stack is a data structure that stores information arranged like a stack. We have seen stacks before when we used a stack to trace through recursive programs. The essential idea is that the last item placed into the stack is the first item removed from the stack Or LIFO(Last In, First Out) for short. 2 TOP 23 7 15

Stacks Stacks: Stack behavior: Stacks are an Abstract Data Type They are NOT built into C So we must define them and their behaviors Stack behavior: A data structure that stores information in the form of a stack. Contains any number of elements of the same type. Access policy: The last item placed on the stack is the first item removed from the stack.

Stacks Stacks: PUSH 19 TOP 19 TOP 2 2 23 23 7 7 15 15 Basic Operations: PUSH and POP PUSH – PUSH an item on the top of the stack. PUSH 19 19 TOP TOP 2 2 23 23 7 7 15 15 (Stack before Push Operation) (Stack after Push Operation)

Stacks Stacks: POP 19 19 TOP TOP 2 2 23 23 7 7 15 15 Basic Operations: PUSH and POP POP – POP off the item in the stack and return it. POP 19 Element removed: 19 TOP TOP 2 2 23 23 7 7 15 15 (Stack before Pop Operation) (Stack after Pop Operation)

Stacks Stacks: Other useful operations: empty – typically implemented as a boolean function that returns true if no items are in the stack. full – returns true if no more items can be added to the stack. In theory a stack should never be full, but any actual implementation has a limit on the # of elements it can store. top – simply returns the value stored at the top of the stack without popping it off the stack.

Stacks Simple Example: PUSH(7) PUSH(3) PUSH(2) POP PUSH(5) 2 5 3 7

Stacks On Monday we used stacks to: Convert infix expressions to postfix expressions Evaluate postfix expressions Infix: 2 * 3 + 5 + 3 * 4 Postfix: 2 3 * 5 + 3 4 * + The operator follows all of its operands Reduces computer memory access and utilizes the stack to evaluate expressions. No parentheses are necessary Still used by some calculators.

Stack Implementation struct stack { int items[SIZE]; int top; }; Last class we talked about 2 types of stack implementations – Array and Linked List Array Implementation: When we initialize the array, There are no items in the stack, so what is top set to in the initialize function? -1 What if we push(10), then what is top? What if we pop(), then what is top? For all of these operations we either access top + 1, or top so all operations are O(1) (we don’t have to traverse the array.)

Stack Implementation Linked List Implementation struct stack { int data; struct stack *next; }; Linked List Implementation Notice that we don’t have a ‘top’ Why? The top will ALWAYS be the first node And we don’t need to worry about the size since it’s a linked list that can expand while there’s heap memory available. So we only need to either add an element to the front or take an element off of the front The runtime for either operation is O(1)

Stack – Linked List Implementation Why do we use double pointers? If we want to be able to return a 0 or a 1 if a Push is successful, We can’t return the address of the new front of the list (which is what we would usually do with a linked list) So we’ll pass a pointer to the front of the list (stack **front) and then if we modify (*front) in the Push function we are changing not the local value of the pointer, but we’re changing the contents of the pointer in the same memory address that was passed from main so those changes will be reflected in main.

Stack Application We did 2 examples last time: Reading in a list of numbers from a user and printing it in backwards order. We also talked about reading in each character of a string and printing it out in backwards order. Checking if we have matching parentheses

Queues If we wanted to simulate customers waiting in a line to be served, We wouldn’t use a stack… LIFO is only going to make the person that got in line first mad.

Queues We would want to use FIFO First In First Out, or 1st in line 1st one to get served. Instead of push and pop, we have the operations Enqueue and Dequeue that add/remove elements from the list.

Queue Basic Operations Enqueue: Inserts an element at the back of the queue Returns 1 if successful, 0 otherwise. Dequeue: Removes the element at the front of the queue. Returns the removed element. Peek Looks at the element at the front of the queue without removing it. Returns the front element. isEmpty Checks to see if the queue is empty. Returns true or false. isFull Checks to see if the queue is full.

Queue Example Starting Queue: 3 5 7 9 11 TIME OPERATION 1 Enqueue(13) 2 Dequeue() 3 Enqueue(15) 4 5 front back Time 1: 3 5 7 9 11 13 Time 2: 5 7 9 11 13 Time 3: 5 7 9 11 13 15 Time 4: 7 9 11 13 15 Time 5: 9 11 13 15

Queues - Array Implementation What would we need for an array implementation? We need an array obviously And we need to keep track of the front and the back.

BAD Queue Implementation Example Starting Queue: TIME OPERATION 1 Enqueue(13) 2 Dequeue() 3 Enqueue(15) 4 5 3 5 7 9 11 front back Time 1: 3 5 7 9 11 13 front back Time 2: 5 7 9 11 13 front back Time 3: Notice that you have to Shift the contents of the Array over each time front changes 5 7 9 11 13 15 front back Time 4: 7 9 11 13 15 front back Time 5: 9 11 13 15 front back

Queues: Array Implementation struct queue { int *elements; int front; int numElements; }; We will use the following revamped idea to store our queue structure: Keep track of the array, the front, and the current number of elements.

Queues: Array Implementation struct queue { int *elements; int front; int numElements; }; Enqueue: We’ll simply add the given element to the index “back” in the array. BUT we’re not storing “back”!!!!! What must we do instead? Add it to the index: front + numElements But what if this goes outside the bounds of our array? numElements = 4 9 11 13 15 front

Queues: Array Implementation struct queue { int *elements; int front; int numElements; int queueSize; }; Enqueue(17): Add it to the index: front + numElements But what if this goes outside the bounds of our array? Front = 2, plus numElements = 4, gives us 6 We can mod by the queueSize (front + numElements ) % queueSize = 0 numElements = 4 numElements = 5 17 9 11 13 15 front

Queues: Array Implementation struct queue { int *elements; int front; int numElements; int queueSize; }; So we’re allowing our array to essentially wrap around. This way we don’t have to copy the contents of our array over if front or back moves numElements = 5 17 9 11 13 15 front

Queues: Array Implementation struct queue { int *elements; int front; int numElements; int queueSize; }; Dequeue If the numElements > 0 numElements--; front = (front + 1) % queueSize numElements = 5 numElements = 4 17 9 11 13 15 front front

Queues - Dynamically Allocated Array Implementation struct queue { int *elements; int front; int numElements; int queueSize; }; What if our numElements == queueSize? We can realloc more memory for our array and update queueSize! But we also need to make sure we copy over the wraparound values correctly.

Queues - Linked List Implementation struct node { int data; struct node *next; }; We are going to need a linked list So we’ll use the same node implementation as before. But we’ll need to keep track of the front and the back. Otherwise either enqueue or dequeue would require an O(n) traversal each time. So we’ll keep a front and back pointer inside of a structure called queue. struct queue { struct node *front; struct node *back; };