Data Structures Stacks and Queues 17122021 1 Learning

















![Exam Question 1. (a) Explain what is meant by a LIFO data structure. [2] Exam Question 1. (a) Explain what is meant by a LIFO data structure. [2]](https://slidetodoc.com/presentation_image_h2/13d43ab62745b07c1bac48821d7c5812/image-18.jpg)






- Slides: 24
Data Structures Stacks and Queues 17/12/2021 1
Learning Objectives Explain: n n The structures of stacks and queues. The concepts of: First In First Out (FIFO) Last In First Out (LIFO) Stack pointers 17/12/2021 2
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. 17/12/2021 3
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. 17/12/2021 4
Queues Jobs are put in at one end and taken out of the other. Queues need 2 pointers: n Start Pointer (SP) Showing it which one is next to be done. n 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. 17/12/2021 5
Problems with Queues 17/12/2021 6
The array may be full So no new value can be entered. n The computer can recognise this problem by examining the end pointer to check if it is pointing outside the array. EP SP Cristi Nojdar Vlad S Flavius Aimee Vlad U Key: SP = Start Pointer EP = End Pointer 17/12/2021 Note: do NOT use these abbreviations in exam questions. 7
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. n 1. SP EP Key: SP = Start Pointer EP = End Pointer Note: do NOT use these abbreviations in exam questions. The computer can recognise if 2. the queue is empty by examining the start and end pointers to check if they are both pointing at the beginning of the queue. SP EP 17/12/2021 8
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 17/12/2021 9
Stacks 1. Zaid and Iram are in the stack. Notice that the pointer is pointing to the next space above. 17/12/2021 10
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. 17/12/2021 11
Stacks 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. 17/12/2021 12
Problems with Stacks 17/12/2021 13
The array may be full So no new value can be entered. n The computer can recognise this problem by examining the stack pointer to check if it is pointing outside the array. 17/12/2021 Pointer Cristi Nojdar Vlad S Flavius Aimee Vlad U 14
The array may be empty So there is no value to be read. n 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 17/12/2021 15
LILO / FIFO & LIFO / FILO In a queue, the last one to come in is the last one to come out: n n LILO (Last In Last Out) FIFO (First In First Out). In a stack, the last one in is the first one out: n n LIFO (Last In First Out) FILO (First In Last Out). 17/12/2021 16
Stacks are more sensibly stored in linked lists than arrays Why? n n 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. n 17/12/2021 Note that the top of the stack is the beginning of the linked list. 17
Exam Question 1. (a) Explain what is meant by a LIFO data structure. [2] (b) Draw a simple diagram to show a stack can be stored in an array. [2] 17/12/2021 18
Answer (a) - Data enters at one end (of a stack) - Leaves at the same end - Hence 'last in, first out' (1 per -, max 2) (b) [2] Key: SP = Start Pointer Note: do NOT use this abbreviation in exam questions. 17/12/2021 19
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) 17/12/2021 20
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. 17/12/2021 21
Plenary Explain the concepts of: First In First Out (FIFO) Last In First Out (LIFO) Stack pointers 17/12/2021 22
LILO / FIFO & LIFO / FILO In a queue, the last one to come in is the last one to come out: n n LILO (Last In Last Out) FIFO (First In First Out). In a stack, the last one in is the first one out: n n LIFO (Last In First Out) FILO (First In Last Out). 17/12/2021 23
Pointers Queues require 2 pointers: n Start Pointer (SP) Showing it which one is next to be done. n End Pointer (EP) Showing where the next job to come along will be put. Stacks need only 1 pointer: n Showing the next space above where the next job to come along will be put. 17/12/2021 24