240 likes | 447 Views
Data Structures. Stacks and Queues. Learning Objectives. Explain: The structures of stacks and queues. The concepts of: First In First Out (FIFO) Last In First Out (LIFO) Stack pointers. Queues. Imagine Zaid, Iram, Sahin, Rashid send jobs for printing, in that order.
E N D
Data Structures Stacks and Queues
Learning Objectives • Explain: • The structures of stacks and queues. • The concepts of: • First In First Out (FIFO) • Last In First Out (LIFO) • Stack pointers
Queues • 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.
Queues • 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.
Queues • Jobs are put in at one end and taken out of the other. • Queues need 2 pointers: • Start Pointer (SP) • Showing it which one is next to be done. • 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.
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. EP Cristi Nojdar Vlad S Flavius Aimee SP Vlad U Key: SP = Start Pointer EP = End Pointer Note: do NOT use these abbreviations in exam questions.
The array may be empty 1. SP EP • So there is no value to be read. • When the end pointer and start pointer meet then the queue is empty and the start pointer is reset to point to the beginning of the queue and the end pointer to the second location in 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. Key: SP = Start Pointer EP = End Pointer Note: do NOT use these abbreviations in exam questions. 2. SP EP
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
Stacks • Zaid and Iram are in the stack. • Notice that the pointer is pointing to the next space above.
Stacks • 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.
Stacks • 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.
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. Pointer Cristi Nojdar Vlad S Flavius Aimee Vlad U
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. Pointer
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) • FIFO (First In First Out). • In a stack, the last one in is the first one out: • LIFO (Last In First Out) • FILO (First In Last Out).
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). • 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.
Exam Question • (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]
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.
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)
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.
Plenary • Explain the concepts of: • First In First Out (FIFO) • Last In First Out (LIFO) • Stack pointers
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) • FIFO (First In First Out). • In a stack, the last one in is the first one out: • LIFO (Last In First Out) • FILO (First In Last Out).
Pointers • Queues require 2 pointers: • Start Pointer (SP) • Showing it which one is next to be done. • 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.