Stacks and Queues. DCS – SWC 2 Stacks A stack is an abstract data structure, with some special properties: –Insertion and deletion is only allowed at.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

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.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Data Structures & Algorithms
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
TCSS 342, Winter 2005 Lecture Notes
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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.
Stacks, Queues, and Deques
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
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.
A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000.
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,
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ Stack ◦ Queue & Priority Queue 2.
Reverse Polish Notation Written by J.J. Shepherd.
Computer Engineering Rabie A. Ramadan Lecture 6.
CISC220 Spring 2010 James Atlas Lecture 10: Queues.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Review Use of Stack Introduction Stack in our life Stack Operations
STACKS & QUEUES for CLASS XII ( C++).
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
G64ADS Advanced Data Structures
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Stacks Chapter 5.
Lecture 7 Queues Stacks Trees.
Chapter 15 Lists Objectives
MEMORY REPRESENTATION OF STACKS
Priority Queues and Heaps
Objectives In this lesson, you will learn to: Define stacks
Cinda Heeren / Geoffrey Tien
Stacks and Queues.
Introduction to Data Structure
Abstract Data Types Stack, Queue Amortized analysis
Building Java Programs
structures and their relationships." - Linus Torvalds
Principles of Computing – UFCFA3-30-1
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
structures and their relationships." - Linus Torvalds
Stacks and Queues.
Stacks, Queues, and Deques
Stacks and Queues.
ITEC 2620M Introduction to Data Structures
CSE 214 – Computer Science II Stacks
Stacks and Queues CLRS, Section 10.1.
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.
Abstract Data Types Stack, Queue Amortized analysis
Stack Implementations
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Abstract Data Type Abstract Data Type as a design tool
Based on slides by Alyssa Harding & Marty Stepp
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks, Queues, and Deques
BY PROF. IRSHAD AHMAD LONE.
structures and their relationships." - Linus Torvalds
Stacks and Queues.
Abstract Data Types Stacks CSCI 240
Lecture 9: Stack and Queue
Queues.
Abstract Data Types (ADTs)
Presentation transcript:

Stacks and Queues

DCS – SWC 2 Stacks A stack is an abstract data structure, with some special properties: –Insertion and deletion is only allowed at the end of the stack (usually called the top) –Due to this restriction, elements are removed from the stack in the opposite order in which they were added –This is called LIFO order (Last In, First Out)

DCS – SWC 3 Stacks public interface Stack { void push(T element); T pop(); T peek(); }

DCS – SWC 4 Stacks A B C D push(A) push(B) push(C) push(D) A = pop() B = pop() C = pop() D = pop()

DCS – SWC 5 Stacks Note that we have not stated exactly how a Stack is implemented Can be done in various ways (array, linked list, etc.) However, data structure should support efficient (O(1)) insertion and removal at the end of the collection

DCS – SWC 6 Stacks What are stacks used for…? –Expression evaluation –Memory management –…any situation where LIFO order is the proper order for processing data

DCS – SWC 7 Stacks

DCS – SWC 8 Stacks The (in)famous HP15-C pocket calculator used Reverse Polish Notation for entering and evaluating expressions This notation is very closely related to stacks Loved by many, hated by more…

DCS – SWC 9 Stacks 12 x 37 is not typed in as: but rather as: 12x37=12E37x E

DCS – SWC 10 Stacks X Push(12) Push(37) Push(X) 12 = pop() 37 = pop() X = pop() Push(12 x 37) 444

DCS – SWC 11 Queues A queue is (also) an abstract data structure, with some special properties: –Insertion is only allowed at the end of the queue (usually called the tail) –Deletion is only allowed at the start of the queue (usually called the head) –Elements are removed from the stack in the same order in which they were added –This is called FIFO order (First In, First Out)

DCS – SWC 12 Queues public interface Queue { void add(T element); T remove(); T peek(); }

DCS – SWC 13 Queues A B C D add(A) add(B) add(C) add(D) A = remove() B = remove() C = remove() D = remove()

DCS – SWC 14 Queue Like for a Stack, a Queue can be implemented in various ways Data structure should support efficient (O(1)) insertion and removal at the start and end of the collection Linked list likely choice

DCS – SWC 15 Queue What are queues used for…? –Event handling –All situations where we need to process data in the order it is produced, but cannot process data immediately

DCS – SWC 16 Stacks and Queues in Java Stack is part of Java library, and can be used as-is Queue is an interface! There are several implementations of the Queue interface availabe See the documentation for details