1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
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.
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.”
Queue Overview Queue ADT Basic operations of queue
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Chapter 12: Data Structures
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Lists CSE1303 Part A Data Structures and Algorithms.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Chapter 16 Stack and Queues part2
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
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.
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’
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linear Data Structures
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Click to edit Master text styles Stacks Data Structure.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Review Array Array Elements Accessing array elements
Data Abstraction & Problem Solving with C++
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
CC 215 Data Structures Queue ADT
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Stack and Queue Author : Srinadh Gunnam.
Pointers and Linked Lists
Queues Definition of a Queue Examples of Queues
Stacks, Queues, and Deques
Presentation transcript:

1 Data Structures - Part II CS215 Lecture #8

Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used in the reverse order  A stack has two operations: push (places a new item at the top of the stack) pop (retrieves the top item from the stack)

Stack Implementation  A stack can be implemented in several ways, e.g., by an array or by a linked list.  A stack pointer is a variable that identifies the beginning of the part of the array that is currently unused  A stack pointer variable, therefore, holds an address.

Declaring a Stack stack:.word 0:maxstacksize sp:.word stack or stack:.word 0:maxstacksize sp:.word... la sp, stack sp uses the address bound to the label stack as the initial value (static initialization) dynamic initialization

Some variations to the implementation  In some implementations, a stack pointer points to the top of the stack  In our sample implementation, the stack pointer points to the next available space we can push an element into  A stack can be grown upward or downward, i.e., we can either add a value to the stack pointer or subtract a value from it when pushing an element

Pushing an element unto the stack This operation is called push. moveM[sp],x addsp,sp,4

Example 16.1 sp initially sp move M[sp],20 add sp,sp,4 20

Example move M[sp],5 add sp,sp,4 sp move M[sp],10 add sp,sp,

Taking an item off the stack This operation is called pop. addsp,sp,-4 move x,M[sp]

Example 16.3 sp initially sp add sp,sp,-4 move x,M[sp] Note: popping the stack means moving back the pointer to the next available space. There is no need to explicitly delete the data. It will get overwritten in the next push.

Full and Empty Stack  SAL does not provide any form of boundary check, i.e., there is no automatic feature that detects whether an element is “pushed” beyond the stack’s capacity  A robust program must check before a push whether the stack is full.  It should also check whether the stack is empty before a pop.

Example 16.4 P.187 textbook.data stack:.byte 0:50 sp:.word stack #static initialization bottom:.word stack bias:.word 48 top:.word number:.word digit:.word push_error:.asciiz “Full stack failure “ pop_error:.asciiz “Empty stack failure “

Example 16.4 P.187 textbook addtop,bottom,50 loop_top: remdigit,number,10 adddigit,digit,bias bgesp,top,bad_push #check if full movem[sp],digit addsp,sp,1 divnumber,number,10 bgtznumber,loop_top bad_push:puts push_error done

Example 16.5 P.187 textbook print_it:bltsp,bottom,bad_pop #empty check addsp,sp,-1 putcm[sp] bgtsp,bottom,print_it done bad_pop:puts pop_error done

Queues  A queue is a data structure that maintains a “first-in first-out” (FIFO) ordering.  In contrast, a stack maintains a “last-in first-out” (LIFO) ordering.  A queue adds new elements at the end. An element can only be removed at the front.  This is an abstraction of the “first- come first-served” practice.

Queue operations  A queue has two operations: enqueue dequeue  An enqueue operation adds new elements at the end of the queue or its tail. This is similar to the stack operation push; only that push now is done at the end of the array instead of at the front (or top).  A dequeue operation removes an element from the front of the array or its head.

Implementation  A queue can be implemented using an array.  A naïve implementation will allow the enqueued data to “walk” through the array. tailhead

A circular queue  An array can be reused by allowing the enqueued data to “walk around” the array. This type of implementation is called a circular queue. tail head

 The particular implementation that will be illustrated here uses an empty element. This will simplify the check for a full or empty queue.  If the queue is empty, the dequeue operation must not return an invalid element.  If the queue is full, the enqueue operation must not destroy an element already in the queue.

Adding elements (Enqueue)  To add one element, we simply add 1 to the the current value of the tail, then execute: add address,base,tail move m[address],new_element #copy element into array tail head B B = blank cell

Removing Elements (Dequeue)  To delete one element, add 1 to the current value of head then we execute add address,base,head move element,m[address] #copy it to element tail head 012 B 012 B

Detecting an Empty Queue  During a dequeue, the first thing that is done is to check whether ( head == tail ). If this is true, then the queue is empty. tail head 012 B 012 B not empty empty

Detecting a Full Queue  Before an enqueue is done, the tail is incremented by 1. If after the increment (head == tail) then the queue is full. 012 B 012 B tail head before increment after increment full queue detected

Making the address circular  Suppose we have the array  To make the address 4 equal to the address 0 we simply use modulo arithmetic, i.e., 4 modulo 4 =

 Using modulo arithmetic maps any address to an address within the allocated space, thus preventing access to out-of-the-range addresses  Also, the modulo conveniently gives us the offset from the base address.  We need the offset from the base address to access specific elements in the queue.

Example 17.1.data queue:.byte 0:4 queueaddr:.word head:.word tail:.word linenumber:.byte nextline:.byte addr:.word newline:.byte string1:.asciiz "Which line is ringing ?" string2:.asciiz "The next line to be answered is " string3:.asciiz "Enqueueing line " empty:.asciiz "No calls waiting " full:.asciiz "ERROR: Queue is full. Exiting program. "

Example 17.2.text __start: la queueaddr,queue loop: puts string1 get linenumber beq linenumber,'\n',dequeue get newline #reads the second character enqueue: add tail,tail,1 rem tail,tail,4 #uses modulo beq tail,head,full_queue puts string3 put linenumber put '\n' add addr,queueaddr,tail move m[addr],linenumber b loop

Example 17.3 dequeue: beq head,tail,empty_queue add head,head,1 rem head,head,4 #uses modulo add addr,queueaddr,head move nextline,m[addr] puts string2 put nextline put '\n' b loop empty_queue: puts empty put '\n' b loop full_queue: puts full done