# Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues.

## Presentation on theme: "Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues."— Presentation transcript:

Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues

Alford Academy Business Education and Computing 3 Stack - Last In First Out (LIFO) A stack can be considered a 1-D array with the following special features:- 1.Items are added to the top of the stack (PUSH) 2.Items are removed from the top of the stack (POP) 3.Items cannot be added to the stack if it is full (STACK OVERFLOW) 4.Items cannot be removed from the stack if it is empty (STACK UNDERFLOW) 5 1 2 3 stackPointer This stack illustrates the following stack operations: 1. PUSH(3) 2. PUSH(2) 3. PUSH(1) 4. PUSH(5)

Alford Academy Business Education and Computing 4 Stack Operations Show the state of an initially empty stack after the following sequence of stack operations. Your diagram should show the position of stackPointer:- PUSH(10) PUSH(12) POP PUSH(17) POP PUSH(100)

Alford Academy Business Education and Computing 5 Stack PUSH and POP algorithms The following algorithms apply to a stack data structure: PUSH a data item onto the stack If stackPointer > maximum then output ‘Stack Overflow’ Else stackPointer = stackPointer + 1 stack(stackPointer) = data item End if POP a data item from the stack If stackPointer < minimum then output ‘Stack Underflow’ Else data item = stack(stackPointer) stackPointer = stackPointer - 1 End if

Alford Academy Business Education and Computing 6 Practice in Using Stack PUSH and POP Do SCHOLAR p124 Q1 and Q2 Implement the program to PUSH and POP a stack in the handout

Alford Academy Business Education and Computing 7 Queue – First In First Out (FIFO) A queue can be considered a 1-D array with the following special features:- 1.Items are added to the rear of the queue 2.Items are removed from the start of the queue 3.Items cannot be added to the queue if it is full (QUEUE FULL) 4.Items cannot be removed from the queue if it is empty (QUEUE EMPTY) 5 1 2 3 rear This queue illustrates the following queue operations: ADDQUEUE(3) ADDQUEUE(2) ADDQUEUE(1) ADDQUEUE(5) start

Alford Academy Business Education and Computing 8 Queue ADD and REMOVE algorithms The following algorithms apply to a queue data structure: ADD a data item onto the queue If (rear = maximum) and (start = 1) Then output ‘Queue Full’ Else rear = rear + 1 queue(rear) = data item End if REMOVE a data item from the queue If (rear = 0) Or (start > rear) then output ‘Queue Empty’ Else data item = queue(start) start = start + 1 End if

Alford Academy Business Education and Computing 9 Practice in Using Queue ADD and REMOVE Implement the program to ADD and REMOVE using a queue in the handout Do SCHOLAR p130 Q5 and Q6 HOMEWORK – Read SCHOLAR Topic 6 (stack and queue). Don’t worry if their algorithms are slightly different from mine. Have all code working with screenshots and printouts of code + test cases