Stacks and Queues Dr. Andrew Wallace PhD BEng(hons) EurIng

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.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
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.
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.
CHAPTER 7 Queues.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks.
10.Elementary data structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Stacks and queues Stacks and queues are dynamic set in which element.
Data Structures from Cormen, Leiserson, Rivest & Stein.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Dr. Andrew Wallace PhD BEng(hons) EurIng
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.
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.”
Testing Dr. Andrew Wallace PhD BEng(hons) EurIng
Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng
Stack and Queue.
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
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.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
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.
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,
Trees Dr. Andrew Wallace PhD BEng(hons) EurIng
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
Foundations of Data Structures Practical Session #4 Recurrence ADT 08/04/2013Amihai Savir & Ilya Mirsky
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
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.
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will.
Tables, hashtables, relations and dictionaries Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 10 Elementary data structures Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Linked Data Structures
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Elementary data structures
COSC160: Data Structures: Lists and Queues
Stacks and Queues Chapter 4.
CPSC 311 Section 502 Analysis of Algorithm
Stacks and Queues.
Stack and Queue APURBO DATTA.
What does that mean? In general there are two aspects:
COMPUTER 2430 Object Oriented Programming and Data Structures I
Basic Data Types Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Algorithms Part III. Data Structures
אחסון (אירגון) מידע DATA DATA DATA Link Link Link … …
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.
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.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Python: Stacks and Queues (as an Array)
Queues: Implemented using Linked Lists
Lecture 16 Stacks and Queues CSE /26/2018.
Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures
Presentation transcript:

Stacks and Queues Dr. Andrew Wallace PhD BEng(hons) EurIng

Overview In and out Stack operations Stack implementation Queue operations Queue implementation Complexity

In and Out Data Memory Address Data Memory Address Data Memory Address Data Memory Address Data Memory Address Data Memory Address Data Memory Address Data Memory Address Stack Queue

In and out LIFO Last in first out FIFO First in first out

Stack Operations S[1 … S.top] Is empty Push Pop

Stack Operations IsEmpty(S) if S.top == null return TRUE else return FALSE

Stack Operations Push(s.x) S.top = S.top + 1 S[S.top] = s.x data S.top s.x

Stack Operations Pop(S) ASSERT(!IsEmpty(S)) S.top = S.top -1 return S[S.top + 1] data S.top s.x

Stack Implementation Array Linked list

Stack Implementation s.tops.top s.tops.top

Queue Operations Q[Q.head … Q.tail] Enqueue(Q.tail) Dequeue(Q.head)

Queue Operations Enqueue(Q.x) Q.tail = Q.x if Q.tail == Q.length Q.tail = 1 else Q.tail = Q.tail + 1 data Q.head Q.tail Q.x Q.tail

Queue Operations Dequeue(Q) Q.x = Q[Q.head] if Q.head == Q.length Q.head = 1 else Q.head = Q.head + 1 return Q.x data Q.tail data Q.head Q.x

Queue Implementation Array Linked list

Queue Implementation Q.headQ.head Q.TailQ.Tail Q.TailQ.Tail

Queue Implementation Q.headQ.head 90 Q.TailQ.Tail Q.TailQ.Tail

Complexity Pop(S) ASSERT(!IsEmpty(S)) S.top = S.top -1 return S[S.top + 1] Dequeue(Q) Q.x = Q[Q.head] if Q.head == Q.length Q.head = 1 else Q.head = Q.head + 1 return Q.x Q(1)

Questions?