Download PPT for com sci component 1 and more Schemes and Mind Maps Computer Science in PDF only on Docsity!
The properties of stacks and queues In computer science, stacks can be used to manage the program counter when a subroutine is called. It is important to record the value of the program counter so when a routine is finished, the program knows where to get the next instruction. For example, consider this pseudocode: 01 function CheckValidAddress (EmailAddress) 02 Valid = true 03 if EmailAddress does not contain “@” then Valid = false 04 if EmailAddress does not contain “.” then Valid = false 05 return Valid 06 endfunction 07 function Main() 08 do 09 EmailAddress = input (“Email address:”) 10 Valid = CheckValidAddress (EmailAddress) 11 until Valid = true 12 endfunction Program execution starts at line 07. The program counter has the values 7, then 8, then 9. These values are not stored on the stack. Instruction 10 contains a call to the routine starting at line 01. At this point, the return address (11) is put on the stack, and the program counter changes to 01. When line 06 is reached, the stack is popped, and the number is put in the program counter to resume fetching the next instruction.
The properties of stacks and queues
- Using the program below, and the abstractions of the stack on the following slide, show the status of the program counter stack at each stage. The program starts execution at line 09. 01 function SubtractDeductions(Pay, Percent) 02 Pay = Pay - (Pay * Percent) / 100 03 return Pay 04 endfunction 05 function CalculatePay(Pay) 06 Pay = SubtractDeductions(Pay, 22) 07 Pay = SubtractDeductions(Pay, 7); 08 endfunction 09 function Main() 10 Pay = CalculatePay(2000) 11 print(Pay) 12 endfunction
The properties of stacks and queues The arithmetic logic unit (ALU) uses a stack to perform calculations. With Reverse Polish notation (RPN) the operator is placed after the two digits, not between them. For example, 6 + 3 can also be written as 6 3 + The rules for the ALU to perform a calculation are:
- (^) If the item is a number, place it on the stack.
- (^) If the item is an operator: + - * / pop the last two items off the stack, perform the arithmetic and put the answer back on the stack.
- (^) If there are no items left, the answer is the item on the stack. On the following slides, show the status of the stack after each stage of a calculation. Indicate the value of the stack pointer by moving the position of the black arrow. You do not need to remember RPN; it is just a good use of a stack.
The properties of stacks and queues
- Calculating 3*(2 + 6) using reverse Polish notation: 3 6 2 + * Stage 1 Stack pointer Data 4 3 2 1 0
Empty stack Stage 2 Stack pointer Data 4 3 2 1 0
Empty stack Stage 3 Stack pointer Data 4 3 2 1 0
Empty stack After initialising stack Data 4 3 2 1 0 -^ Empty stack Stage 4 Stack pointer Data 4 3 2 1 0
Empty stack Stage 5 Stack pointer Data 4 3 2 1 0
Empty stack
The properties of stacks and queues return False return Item return Item if self.StackPointer < self.Max - 1: self.StackPointer = self.StackPointer
- 1 self.Contents[self.StackPointer] = Item return True else: if self.StackPointer >= 0: Item = self.Contents[self.StackPointer] self.StackPointer = self.StackPointer
- 1 else: return None if self.StackPointer >= 0: Item = self.Contents[self.StackPointer] else: return None
The properties of stacks and queues Queues are used by an operating system to store process blocks. A process block contains all the information required to process the instructions of a program. This includes the values of the registers and the stack for each process. In a first come first served scheduling algorithm, new processes join the back of the queue, and the executing process is at the front of the queue. For simplicity, in the following illustration the process blocks are named A, B and C. On the following slides, show the status of the scheduler queue and indicate the value of the pointers.
- (^) Process A is loaded and is enqueued by the scheduler.
- (^) Process B is loaded and is enqueued by the scheduler.
- (^) Process C is loaded and is enqueued by the scheduler.
- (^) The running process completes its execution and is dequeued by the scheduler.
- (^) Process D is loaded and is enqueued by the scheduler.
- (^) The running process completes its execution and is dequeued by the scheduler.
- (^) The running process completes its execution and is dequeued by the scheduler.
The properties of stacks and queues Process dequeued Front Back Dat a 4 3 2 1 0
- Process D is enqueued Front Back Dat a 4 3 2 1 0
Process dequeued Front Back Dat a 4 3 2 1 0
Process dequeued Front Back Dat a 4 3 2 1 0
- An operating system queue managing five processes.
The properties of stacks and queues
ONLY USE THIS VERSION OF THE ACTIVITY IF YOU HAVE BEEN TAUGHT PYTHON.
- Copy the skeleton code from the file below into a new Python program. 03. Queues Activity 2 Python.txt
- Using the statements on the following slide, complete the program to perform the functions of a queue : enqueue, dequeue and peek. Where you see “# ENTER YOUR CODE HERE” you should paste the correct statements into that part of the program.
- The Enqueue method should return true if an item is enqueued and false if an overflow occurs.
- The Dequeue method should return the item removed or null if the queue is empty.
- The Peek method should return the next item in the queue or null if the queue is empty. You will need to add tabulation where it is needed for selection statements too. Run your program to check it works.
The properties of stacks and queues
- Show the status of the circular queue after the following operations: Dequeue Item Count (^3) Front Back Data 3 2 1 0
Dequeue Item Count (^2) Front Back Data 3 2 1 0
“Andy” is enqueued Item Count (^3) Front Back Data 3 2 1 0
“Sam” is enqueued Item Count (^4) Front Back Data 3 2 1 0
The properties of stacks and queues
- Show the status of the circular queue after the following operations: Dequeue Item Count (^3) Front Back Data
Dequeue Item Count (^2) Front Back Data
Dequeue Item Count (^1) Front Back Data
“Carol” is enqueued Item Count (^4) Front Back Data 3 2 1 0
The properties of stacks and queues
- Modify the queue program from a queue to a circular queue. Remember the back and front pointers can move from the top to the bottom of the structure. This introduces a complication in determining if the queue is empty or full. Once data starts being entered into the structure the condition for empty and full become the same: the front pointer is immediately behind the back pointer. The easiest solution is to introduce another variable/attribute to store how many items are currently in the queue. If the number of items equals the maximum number of items, then the queue is full. If the number of items equals zero, the queue is empty.
The properties of stacks and queues
- A shunting yard algorithm uses a stack to change infix expressions to post-fix expressions. Investigate the algorithm and produce a step-by-step illustration of how it works with the data shown. OPERATOR STACK
A + B * C - D
OUTPUT QUEUE INPUT QUEUE
You do not need to remember the shunting yard algorithm; it is just a good use of a stack and queue.