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.

Slides:



Advertisements
Similar presentations
Stacks and Queues. Not really data structures – More of an enforcement of policy – Can be implemented using an array or linked list – Can store just about.
Advertisements

Stacks, Queues, and Linked Lists
Data Structure HKOI training /4/2010 So Pak Yeung.
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
CS 106 Introduction to Computer Science I 12 / 13 / 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.
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.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 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.”
Objectives of these slides:
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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,
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.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
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.
Linear Data Structures
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
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.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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,
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Chapter 18: Stacks and Queues.
COSC160: Data Structures: Lists and Queues
Chapter 15 Lists Objectives
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 19: Stacks and Queues.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Queues: Implemented using Arrays
Stacks: Implemented using Linked Lists
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.
Stacks and Queues.
Circular Queues: Implemented using Arrays
Lecture 9: Stack and Queue
Presentation transcript:

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 List

Stack top max index = 5 start index = 0 Two operations: PUSH – puts an item on the top of the stack POP – takes an item from the top of the stack 3 is the top of the stack

Stack - pop top What value do we get if we pop from the stack? Answer: 1000

top Stack - pop and the top of the stack changes to ? top Let’s pop once more..

top 2000 is popped… top and top changes to 1 Stack - pop

 to what extent do we pop? top Let’s keep popping…

top and now top is 0 let’s pop more..

top and now top is -1 can we still pop? before popping we need to check the top

top pushing an item to stack causes top to move upward first then store the item Stack - push top 5000 To what extent do we push?

Pseudocode for push 1. increment top 2. put item on stack top What if top is the last index? Can we still push? NO !!! when top is max index, we need to signal stack is full

Pseudocode for push 1. increment top 2. put item on stack 3. if top = max index then isFULL=TRUE top Are we done? No, we need to check first if the stack is full Before we proceed to push an item

Pseudocode for push 1. If not isFULL then 2. increment top 3. put item on stack 4. if top = max index then 5. isFULL=TRUE 6. else 7. isFULL = FALSE 8. else 9. display “stack full,push not allowed”

Pseudocode for Pop 1. If not isEmpty then 2. getitem=Stack[top] 3. top=top if top < first index then 5. isEmpty = True 6. else 7. isEmpty=False 8. Else 9. display “stack empty, nothing to pop”

Applications of Stack  Checking of balanced parenthesis  Tracking memory addresses before and after invoking a function  Arithmetic operations evaluation  Program compilation

Queue  Data structure which stores data in First In – First Out (FIFO)

Queue front rear Two operations: Enqueue – item after the rear Dequeue – get item on the front Did you observe queues in your day to day life?

Queue front rear Enqueue 1.If queue is not full 2. add 1 to rear 3. put item on rear if rear is max index if rear is max index isFull = TRUE isFull = TRUE else else isFull=FALSE isFull=FALSE

front rear Dequeue 1.If queue is not empty 2.get item from front 3.Add 1 to front if front = rear if front = rear isEmpty = TRUE isEmpty = TRUE else else isEmpty=FALSE isEmpty=FALSE Queue

Applications of Queue  Printing queue  Manage job requests