Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 30/09/20161 Data Structures Stacks and Queues

2 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

3 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.

4 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.

5 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.

6 630/09/2016 Problems with Queues

7 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.

8 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.

9 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

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

11 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.

12 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.

13 1330/09/2016 Problems with Stacks

14 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

15 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

16 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).

17 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.

18 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]

19 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.

20 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)

21 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.

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

23 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).

24 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.


Download ppt "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."

Similar presentations


Ads by Google