Advanced Higher Computing Science Stacks Queues and linked lists.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Character Arrays 4 Integer Arrays 4 Floating Point Number Arrays 4.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
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.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Data Structures AH Computing. Description and exemplification of the following variable types/data structures: 2-D arrays, records, queues, stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
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.”
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Stack and Queue.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CHP-4 QUEUE.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Stacks And Queues Chapter 18.
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
CHP-3 STACKS.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Advanced Higher Computing Science Stacks Queues and linked lists
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Objectives In this lesson, you will learn to: Define stacks
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Data Structures Interview / VIVA Questions and Answers
STACKS.
Stacks Chapter 4.
Stack.
CMSC 341 Lecture 5 Stacks, Queues
Data structures.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Stacks, Queues, and Deques
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
DATA STRUCTURES IN PYTHON
Presentation transcript:

Advanced Higher Computing Science Stacks Queues and linked lists

Why do we need data structures? How we represent data when solving a problem should mirror the real world as closely as possible Data structures should be what you think about when first approaching a problem

Are all complex data structures arrays? An array is a set of contiguous blocks of memory. Resizing an array is a complex and memory intensive operation Storing data in non contiguous blocks makes the data structure more flexible but more complex

Real world examples: Stacks Browser history Interrupts Blog posts

Real world examples: Stacks Undo function To do lists

The Stack A stack is a 1-dimensional array or list where data elements can be added or retrieved only from the top of the stack. The terms push and pop refer to adding and removing items respectively. In a stack, a pointer is required to identify the location of the top of the stack A stack is often referred to as a "Last in First Out" structure

The stack

The interrupt stack: Run programA Interrupt event (interrupt1) Store programA register contents in memory. Push programA memory location on to stack Run interrupt1 routine Interrupt event (Interrupt2) Store interrupt1 register contents in memory. Push interrupt 1 memory location on to stack Run interrupt2 routine Interrupt2 complete Pop interrupt1 memory location from stack Retrieve interrupt1 status and register contents Continue interrupt1 routine interrupt1 complete Pop programA memory location from stack Retrieve programA status and register contents Continue programA

Implementing a stack stackPointer is used to keep track of the top of the stack max is the maximum number of items which the stack can hold stack[] is an array to hold the data

Implementing a stack CLASS Stack IS {INTEGER stackPointer, INTEGER max, INTEGER stack[]} METHODS CONSTRUCTOR Stack(INTEGER size) DECLARE THIS.stack INITIALLY []*size DECLARE THIS.stackPointer INITIALLY 0 DECLARE THIS.size INITIALLY size END CONSTRUCTOR PROCEDURE Push(INTEGER value) IF THIS.stackPointer=THIS.size THEN ELSE SET THIS.stack[THIS.stackPointer] TO value SET THIS.stackPointer TO THIS.stackPointer+1 END IF END PROCEDURE

Implementing a stack FUNCTION pop() RETURNS INTEGER IF THIS.stackPointer=0 THEN ELSE SET THIS.stackPointer TO THIS.stackPointer -1 RETURN THIS.stack[THIS.stackPointer] END IF END FUNCTION END CLASS

Real world examples: Queues Tech Support helplines

Real world examples: Queues Scheduled tasks Printer queues

The Queue A queue is a 1-dimensional array or list, but data elements are inserted and retrieved at different ends. In a queue two pointers are required: one to point to the head of the queue and one to point to the end. A queue is a "First in First Out" structure.

The Circular queue

When the last space in the array is reached the rear pointer is moved to the start of the array. What would this queue look like after the following operations? Leave 6 Join 77

The Circular queue What would this queue look like after the following operations? Leave 6 Join 77

Implementing a Circular queue start is used to keep track of the front of the queue rear is used to keep track of the back of the queue maxSize is the maximum number of items which the queue can hold currentSize is the current number of items which the queue is holding queue[] is an array to hold the data

Implementing a circular queue CLASS Queue IS {INTEGER start, INTEGER rear, INTEGER currentSize, INTEGER maxSize, ARRAY OF INTEGER queue} METHODS CONSTRUCTOR Queue(INTEGER size) DECLARE THIS.start INITIALLY 0; DECLARE THIS.rear INITIALLY 0; DECLARE THIS.currentSize INITIALLY 0 DECLARE THIS.maxSize INITIALLY size; DECLARE THIS.queue INITIALLY INTEGER[maxSize] END CONSTRUCTOR

Implementing a circular queue PROCEDURE join(INTEGER data) IF THIS.currentSize = THIS.MaxSize THEN SEND "Queue Overflow" TO DISPLAY ELSE SET THIS.queue[THIS.rear] TO data SET THIS.rear TO THIS.rear + 1 SET THIS.currentSize TO THIS.currentSize + 1 END IF IF THIS.rear > THIS.maxSize THEN SET THIS.rear TO 1 END IF END PROCEDURE

Implementing a circular queue FUNCTION leave() RETURNS INTEGER IF THIS.currentSize = 0 THEN SEND "Queue Underflow" TO DISPLAY RETURN 0 ELSE RETURN THIS.queue[THIS.start] SET THIS.queue[THIS.start] TO 0 SET THIS.currentSize TO THIS.currentSize - 1 SET THIS.start TO THIS.start + 1 END IF IF THIS.start > THIS.maxSize THEN SET THIS.start TO 1 END IF END FUNCTION END CLASS

A queue with one pointer Join 56

A queue with one pointer queuePointer keeps track of the front of the queue size is the maximum number of items which the queue can hold queue[] is an array to hold the data

A queue with one pointer CLASS Queue IS {ARRAY OF INTEGER queue, INTEGER queuePointer, INTEGER size} METHODS CONSTRUCTOR Queue(INTEGER queueSize) DECLARE THIS. size INITIALLY queueSize DECLARE THIS.queue INITIALLY [size] DECLARE THIS.queuePointer INITIALLY 0 END CONSTRUCTOR

A queue with one pointer PROCEDURE join(INTEGER value) IF THIS.queuePointer=THIS.size THEN ELSE SET THIS.queue[THIS.queuePointer] TO value SET THIS.queuePointer TO THIS.queuePointer+1 END IF END PROCEDURE

A queue with one pointer FUNCTION leave() RETURNS INTEGER DECLARE result INITIALLY IF THIS.queuePointer=0 THEN ELSE SET result TO THIS.queue[0] FOR i FROM 0 TO THIS.queuePointer-2 DO SET THIS.queue[i] TO THIS.queue[i+1] END FOR SET THIS.queuePointer TO THIS.queuePointer-1 END IF RETURN result END FUNCTION END CLASS

Real world examples: Linked lists Road train File blocks on disk

Linked Lists A linked list is a dynamic data structure, which means that its size is not fixed Each item in a linked list consists of the data and a link to the memory location of the next item. A linked list is very flexible as the order of items in a linked list can be changed without actually moving any data around, just the links between them.

File blocks on disk Why does this data need to be stored as a dynamic structure? What causes disk fragmentation and why is it a problem? What is done during disk defragmentation?

Linked Lists it is not possible to identify a specific item directly using its index in the way that you can when storing data in an array. To identify the position of an item in a linked list you have to walk through the list from beginning to end. Linked lists can be used to implement queues and stacks as well as arrays, with the advantage that they do not need to have a fixed size, avoiding overflow errors.

Implementing a linked list PROCEDURE setupList() DECLARE data INTEGER INITIALLY 0 DECLARE head POINTER INITIALLY NUL # POINTER is a link to a memory location# END PROCEDURE PROCEDURE newNode(INTEGER data, POINTER next) IF head = NUL THEN next = NUL ELSE next = NUL END IF END PROCEDURE

Implementing a linked list PROCEDURE deleteNode(INTEGER data) IF head = NUL THEN SEND "List is empty" TO DISPLAY ELSE IF THEN END IF DECLARE found INITIALLY FALSE REPEAT IF THEN SET found TO true END IF UNTIL current item pointer = NUL IF found = false THEN SEND "not found" TO DISPLAY END IF END PROCEDURE

Using a linked list to implement a stack or a queue Since a linked list is a dynamic structure, a stack can grow and shrink as items are pushed and popped. The stack pointer effectively becomes the last item in the list If a linked list is used to implement a queue, since it is a dynamic structure, the front and back of the queue are the last and first items in the list. Both structures will require a variable to store the maximum number of items allowed in the linked list.

Specimen Paper Q 3a A computerised version of a card game, based on various animals native to Scotland, is being developed for a website. During game play, players can take a card from or place a card on a pile of cards. A stack data structure will represent this pile of cards. The stack is held in a 1-D array and the last item placed in the stack was the Golden Eagle. The 1-D array in which the stack is held is shown below:

Specimen Paper Q 3a (i) An item is added to the stack by “pushing” and removed by “popping”. Draw the final state of the stack after the following five operations: 1. Pop 2. Push Loch Ness Monster 3. Pop 4. Pop 5. Push Grouse IndexCharacter 0Ptarmigan 1Otter 2Golden Eagle 3 4

Specimen Paper Q 3a (i) An item is added to the stack by “pushing” and removed by “popping”. Draw the final state of the stack after the following five operations: 1. Pop 2. Push Loch Ness Monster 3. Pop 4. Pop 5. Push Grouse IndexCharacter 0Ptarmigan 1Grouse 2 3 4

Specimen Paper Q 3 (ii & iii) (ii) Apart from the 1-D array, describe another item of data required to implement a stack. (iii) When a stack is implemented using a 1-D array, adding a valid item can cause an execution error. Explain why an execution error can occur in this situation.

Specimen Paper Q 3 (ii & iii) (ii) Apart from the 1-D array, describe another item of data required to implement a stack. A stack pointer (INTEGER) is required to store the index position of the top of the stack (iii) When a stack is implemented using a 1-D array, adding a valid item can cause an execution error. Explain why an execution error can occur in this situation.

Specimen Paper Q 3 (ii & iii) (ii) Apart from the 1-D array, describe another item of data required to implement a stack. A stack pointer (INTEGER) is required to store the index position of the top of the stack (iii) When a stack is implemented using a 1-D array, adding a valid item can cause an execution error. Explain why an execution error can occur in this situation. Stack Overflow occurs when the array is full and an attempt is made to access an index position which does not exist.

Specimen Paper Q 3b (i) A linked list could have been used rather than a stack to represent the pile of cards. (i) The animals Ptarmigan, Otter and Golden Eagle are entered, in the order given, into a linked list. Draw this linked list.

Specimen Paper Q 3b (i) A linked list could have been used rather than a stack to represent the pile of cards. (i) The animals Ptarmigan, Otter and Golden Eagle are entered, in the order given, into a linked list. Draw this linked list.

Specimen Paper Q 3b (ii) (ii) Explain why the execution error in part (a) (iii) would not occur if a linked list is used rather than a stack.

Specimen Paper Q 3b (ii) (ii) Explain why the execution error in part (a) (iii) would not occur if a linked list is used rather than a stack. A linked list is a dynamic structure (items are not stored contiguously in memory) so the number of items is not fixed and items can be easily added

2014 Q4 In the early days of hand-held calculators, ‘Postfix’ notation was used to reduce memory access during a calculation and made use of the stack to evaluate expressions. Postfix notation avoids the use of brackets. The arithmetical expression 6 * (4 + 3) is written in Postfix notation as * and is then processed, left to right, using a stack.

2014 Q4

Stack pointer At the beginning of the process the stack is empty. The first element input is a 6 and this is pushed onto the stack. After the number “4” has been pushed on to the stack, the state of the stack is: The arithmetical expression 6 * (4 + 3) is written in Postfix notation as * (a) Explain why the “stack pointer” is needed during stack operations.

2014 Q4 (b) (i) Following the algorithm above, show the state of the stack after the 3 has been pushed on to the stack. (ii) Show the contents of the stack, and the value of the stack pointer, after the algorithm has run to completion.

2014 Q4 (i) Following the algorithm above, show the state of the stack after the 3 has been pushed on to the stack. ii) Show the contents of the stack, and the value of the stack pointer, after the algorithm has run to completion.

2014 Q4 (i) Following the algorithm above, show the state of the stack after the 3 has been pushed on to the stack Stack pointer 2 ii) Show the contents of the stack, and the value of the stack pointer, after the algorithm has run to completion.

2014 Q4 (i) Following the algorithm above, show the state of the stack after the 3 has been pushed on to the stack Stack pointer 2 ii) Show the contents of the stack, and the value of the stack pointer, after the algorithm has run to completion Stack pointer 1

2014 Q4 (i) Following the algorithm above, show the state of the stack after the 3 has been pushed on to the stack Stack pointer 2 ii) Show the contents of the stack, and the value of the stack pointer, after the algorithm has run to completion Stack pointer Stack pointer 0

2014 Q4 (c) The algorithm above makes use of the “pop” operation. Use pseudocode to describe the operation to pop a value from the stack. (d) Describe one problem that should be checked for when pushing a value onto a stack.

2014 Q4 IF THEN SEND "Stack underflow" TO DISPLAY ELSE SET outputValue TO stack[stackPointer] SET stackPointer TO stackPointer -1 END IF Stack c) d) Stack overflow

2012 4b A 1-D array with six elements is used to hold the contents of a queue and the variables front and rear are used to hold the positions of the item at the front and rear of the queue. The following lines of code are executed: MyQ.addtoback(15) MyQ.addtoback(29) MyQ.remove MyQ.addtoback(8) The following diagram shows that 29 and 8 are in the queue. The number 15 is still present in the 1-D array but is not in the queue. Index front 1 rear 2

2012 4b (i)State what happens to the variables front and rear when a number is removed from the queue. ii) The state of MyQ shown above is changed by executing these additional lines of code: MyQ.addtoback(11) MyQ.remove MyQ.addtoback(9) Draw a diagram that shows the new state of MyQ

2012 4b (i)State what happens to the variables front and rear when a number is removed from the queue. SET front TO front + 1 ii) The state of MyQ shown above is changed by executing these additional lines of code: MyQ.addtoback(11) MyQ.remove MyQ.addtoback(9) Draw a diagram that shows the new state of MyQ

2012 4b (i)State what happens to the variables front and rear when a number is removed from the queue. SET front TO front + 1 ii) The state of MyQ shown above is changed by executing these additional lines of code: MyQ.addtoback(11) MyQ.remove MyQ.addtoback(9) Draw a diagram that shows the new state of MyQ

2012c (i) Describe the problem that will arise as items continue to be added and removed from MyQ. (ii) Describe how the problem in (c)(i) could be solved.

2012c (i) Describe the problem that will arise as items continue to be added and removed from MyQ. The end of the array will be reached but there is still room in the array for items to be placed in the queue (ii) Describe how the problem in (c)(i) could be solved.

2012c (i) Describe the problem that will arise as items continue to be added and removed from MyQ. The end of the array will be reached but there is still room in the array for items to be placed in the queue (ii) Describe how the problem in (c)(i) could be solved. Either use a circular queue where items joining the queue will be added to the start of the array and the pointer rear will become 1 Or Reset the array every time an item is removed by shuffling items one place up

2010 Q2

(a)Explain why the characters L, E and D have not been moved to the locations identified by indices 0, 1 and 2 after characters C and A have been processed. (b) State the values stored in the variables front and rear after the characters O and N have been added and a further three characters removed from the queue.

2010 Q2 (a)Explain why the characters L, E and D have not been moved to the locations identified by indices 0, 1 and 2 after characters C and A have been processed. It would be very inefficient to move every character one place up every time a key is pressed (b) State the values stored in the variables front and rear after the characters O and N have been added and a further three characters removed from the queue.

2010 Q2 (a)Explain why the characters L, E and D have not been moved to the locations identified by indices 0, 1 and 2 after characters C and A have been processed. It would be very inefficient to move every character one place up every time a key is pressed (b) State the values stored in the variables front and rear after the characters O and N have been added and a further three characters removed from the queue. Front = 5 Back = 6

2010 Q2 c (c) Characters will continue to be added and removed from the queue held in the 1-D array. (i)State the problem encountered as characters continue to be added and removed. (ii) Describe how to solve the problem encountered in (i).

2010 Q2c (c) Characters will continue to be added and removed from the queue held in the 1-D array. (i)State the problem encountered as characters continue to be added and removed. Rear will reach 9 but the queue is not full as there are spaces at the front of the array. (ii) Describe how to solve the problem encountered in (i).

2010 Q2c (c) Characters will continue to be added and removed from the queue held in the 1-D array. (i)State the problem encountered as characters continue to be added and removed. Rear will reach 9 but the queue is not full as there are spaces at the front of the array. (ii) Describe how to solve the problem encountered in (i). The solution would be to wraparound where items are added at the start of the array.

2010 Q2d Another data structure used in programming is a stack. (i)Explain how the operation of a stack data structure differs from a queue. (ii) Explain why a keyboard buffer does not use a stack data structure.

2010 Q2d Another data structure used in programming is a stack. (i)Explain how the operation of a stack data structure differs from a queue. In a stack items are popped and pushed at the same end. In a queue items leave at one end and join at the other (ii) Explain why a keyboard buffer does not use a stack data structure.

2010 Q2d Another data structure used in programming is a stack. (i)Explain how the operation of a stack data structure differs from a queue. In a stack items are popped and pushed at the same end. In a queue items leave at one end and join at the other (ii) Explain why a keyboard buffer does not use a stack data structure. Characters would be processed in the wrong order ( last key pressed processed before earlier characters )