30/09/20161 Data Structures Stacks and Queues. 230/09/2016 Learning Objectives Explain: The structures of stacks and queues. The concepts of: First In.

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

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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  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.
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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.
Introduction to Data Structures. Data Structures A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly.
Introduction to Data Structures. About the document. The document is prepared by Prof. Shannon Bradshaw at Drew University. The key concepts in data structure.
Stacks and Queues Introduction to Computing Science and Programming I.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Data Structures Types of Data Structure Data Structure Operations Examples Choosing Data Structures Data Structures in Alice.
Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data.
(c) , University of Washington19a-1 CSC 143 Stacks and Queues: Concepts and Implementations.
Advanced Higher Computing Science Stacks Queues and linked lists.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
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.
INTRODUCTION TO DATA STRUCTURES 1. DATA STRUCTURES A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
An Introduction to Programming Using Alice Data Structures.
STACKS & QUEUES for CLASS XII ( C++).
Stack ADT (Abstract Data Type) N …
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
Week 4 - Monday CS221.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
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.
Chapter 4 The easy stuff.
COSC160: Data Structures: Lists and Queues
Program based on queue & their operations for an application
Chapter 15 Lists Objectives
CSE 373: Data Structures and Algorithms
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Introduction to Data Structures
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Stacks A stack is a data structure that is similar in spirit to a pile of cafeteria trays. Think about the trays in the dining halls: when the dining staff.
Introduction to Data Structures
Stacks and Queues.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Stacks, Queues, and Deques
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Introduction to Data Structures
Stacks, Queues, and Deques
Popping Items Off a Stack Lesson xx
Lesson Objectives Aims
CSC 143 Queues [Chapter 7].
ITEC 2620M Introduction to Data Structures
Queues: Implemented using Arrays
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.
Data structures.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Visit for more Learning Resources
Stacks and Queues: Concepts and Implementations
CE 221 Data Structures and Algorithms
Stacks and Queues CSE 373 Data Structures.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Queues: Implemented using Linked Lists
Stacks, Queues, and Deques
Using a Queue Chapter 8 introduces the queue data type.
CSCS-200 Data Structure and Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
DATA STRUCTURES IN PYTHON
Chapter 4 Stacks and Queues
Presentation transcript:

30/09/20161 Data Structures Stacks and Queues

230/09/2016 Learning Objectives Explain: The structures of stacks and queues. The concepts of: First In First Out (FIFO) Last In First Out (LIFO) Stack pointers

330/09/2016Queues Imagine Zaid, Iram, Sahin, Rashid send jobs for printing, in that order. When these jobs arrive they are put in a queue awaiting their turn to be dealt with. Key: SP = Start Pointer EP = End Pointer Note: do NOT use these abbreviations in exam questions.

430/09/2016Queues It is only fair that when a job is called for by the printer that Zaid’s job is sent first because his has been waiting longest. These jobs are held in an array. Key: SP = Start Pointer EP = End Pointer Note: do NOT use these abbreviations in exam questions.

530/09/2016Queues Jobs are put in at one end and taken out of the other. Queues need 2 pointers: Start Pointer (SP) Start Pointer (SP) Showing it which one is next to be done. End Pointer (EP) End Pointer (EP) Showing where the next job to come along will be put. Key: SP = Start Pointer EP = End Pointer Note: do NOT use these abbreviations in exam questions.

630/09/2016 Problems with Queues

730/09/2016 The array may be full So no new value can be entered. The computer can recognise this problem by examining the end pointer to check if it is pointing outside the array. The computer can recognise this problem by examining the end pointer to check if it is pointing outside the array. Aimee Flavius Vlad S Nojdar Vlad U Cristi EP SP Key: SP = Start Pointer EP = End Pointer Note: do NOT use these abbreviations in exam questions.

830/09/2016 The array may be empty So there is no value to be read. When the end pointer and start pointer meet then the queue is empty and the start and end pointers are reset to point to the beginning of the queue. The computer can recognise if the queue is empty by examining the start and end pointers to check if they are both pointing at the beginning of the queue. The computer can recognise if the queue is empty by examining the start and end pointers to check if they are both pointing at the beginning of the queue. EPSP1. 2. EPSP Key: SP = Start Pointer EP = End Pointer Note: do NOT use these abbreviations in exam questions.

930/09/2016 Stacks Imagine a queue where the data was taken off the array at the same end that it was put on. This would be an unfair queue because the first one there would be the last one dealt with. This type of unfair queue is called a stack. A stack will only need one pointer because adding things to it and taking things off it are only done at one end

1030/09/2016 Stacks 1.Zaid and Iram are in the stack. Notice that the pointer is pointing to the next space above.

1130/09/2016 Stacks 2.When a job is taken off the stack it is found by the computer at the space under the pointer (Iram’s job), and the pointer moves down one.

1230/09/2016Stacks 3.A new job (Sahin’s) is placed on top of the stack and the pointer then moves up one. This may seem wrong, but you will find out why this might be appropriate in some circumstances later in the course.

1330/09/2016 Problems with Stacks

1430/09/2016 The array may be full So no new value can be entered. The computer can recognise this problem by examining the stack pointer to check if it is pointing outside the array. The computer can recognise this problem by examining the stack pointer to check if it is pointing outside the array. Aimee Flavius Vlad S Nojdar Vlad U Cristi Pointer

1530/09/2016 The array may be empty So there is no value to be read. The computer can recognise this problem by examining the stack pointer to check if it is pointing at the first location in the array. The computer can recognise this problem by examining the stack pointer to check if it is pointing at the first location in the array. Pointer

1630/09/2016 LILO / FIFO & LIFO / FILO In a queue, the last one to come in is the last one to come out: LILO (Last In Last Out) LILO (Last In Last Out) FIFO (First In First Out). FIFO (First In First Out). In a stack, the last one in is the first one out: LIFO (Last In First Out) LIFO (Last In First Out) FILO (First In Last Out). FILO (First In Last Out).

1730/09/2016 Stacks are more sensibly stored in linked lists than arrays Stacks are more sensibly stored in linked lists than arrays Why? It would mean that the stack has no maximum size (this is a linked list’s main advantage over arrays). It would mean that the stack has no maximum size (this is a linked list’s main advantage over arrays). A stack is only active (read from and wrote to) at one end and so a linked list is ideal as it is always accessed from the front first. A stack is only active (read from and wrote to) at one end and so a linked list is ideal as it is always accessed from the front first. So we are always at the front end of the list and don’t need to read through it as we just use the item at the front. Note that the top of the stack is the beginning of the linked list. Note that the top of the stack is the beginning of the linked list.

1830/09/2016 Exam Question 1.(a) Explain what is meant by a LIFO data structure. [2] (b) Draw a simple diagram to show how a stack can be stored in an array. [2]

1930/09/2016 Answer (a) - Data enters at one end (of a stack) - Leaves at the same end - Hence 'last in, first out' (1 per -, max 2) (2)(b)[2] Key: SP = Start Pointer Note: do NOT use this abbreviation in exam questions.

2030/09/2016 Question 2.A stack is being held in an array. Items may be read from the stack or added to the stack. a) State a problem that may arise when (i) adding a new value to the stack (ii) reading a value from the stack. (2) b) Explain how the stack pointer can be used by the computer to recognise when such problems may occur. (2)

2130/09/2016 Answer a) (i) The array may be full, consequently no new value can be entered. (ii) The array may be empty, there is no value to be read. b) (i) The stack pointer will be pointing outside the array. (ii) The stack pointer will be pointing at the first location in the array. Notes. When discussing stacks and queues it is important to have a picture in your mind of what it looks like. The simplest picture is to imagine them being held in an array.

2230/09/2016 Plenary Explain the concepts of: First In First Out (FIFO) Last In First Out (LIFO) Stack pointers

2330/09/2016 LILO / FIFO & LIFO / FILO In a queue, the last one to come in is the last one to come out: LILO (Last In Last Out) LILO (Last In Last Out) FIFO (First In First Out). FIFO (First In First Out). In a stack, the last one in is the first one out: LIFO (Last In First Out) LIFO (Last In First Out) FILO (First In Last Out). FILO (First In Last Out).

2430/09/2016 Pointers Queues require 2 pointers: Start Pointer (SP) Start Pointer (SP) Showing it which one is next to be done. End Pointer (EP) End Pointer (EP) Showing where the next job to come along will be put. Stacks need only 1 pointer: Showing the next space above where the next job to come along will be put. Showing the next space above where the next job to come along will be put.