what is a queue
a linear data structur that follows the First-In-First-Out (FIFO) principle, where the first element added is the first one to be removed
how do you add and remove from a queue
items are enqueued at the back of the queue (added)
dequeued from the front (this removes and returns that item)
How are stacks implemented in code?(3)
Write pseudocode to declare an array of size 10 and a stack pointer initially of 0.
Write a push procedure
array stackItems[10]
stackPointer = 0
procedure push(item)
if stackPointer == 10 then
print (“Stack is full!”)
else
stackItems[stackPointer] = item
stackPointer++
end if
end procedure
Write pseudocode to declare an array of size 10 and a stack pointer initially of 0
Write a pop function
array stackItems[10]
stackPointer = 0
function pop()
if stackPointer == 0 then
print (“Stack is empty!”)
return null
else
stackPointer- -
return stackItems[stackPointer]
end if
end function
what are 5 features of stacks
Using pseudocode, write an algorithm that allows the user to enter a name which is then pushed onto a stack data structure, checking first that the data structure is not full
use the variable wNames and 2 others
new = input(“enter a name : “)
if top <=5 then
top = top + 1
wNames[top] = new
end if
A user enters whole numbers into a computer program. Each number entered is placed onto a
stack. The stack is created using an array with a maximum of 20 elements.
The function addItem is written but is incomplete.
Complete the function, addItem.
function addItem (number)
if top == …………………………………… then
return false
else
numStack[……………………………………] = ………………………………
top = …………………………………… + 1
…………………………………………………………………………
endif
endfunction
function addItem (number)
if top = ‘20’ then
return false
else
numStack[‘top’] = ‘number’
top = ‘top’ + 1
‘return true’
endif
endfunction
A stack, in shared memory, is being used to pass a single variable length ASCII string
between two sub-systems. The string is placed in the stack one character at a time in reverse
order with the last byte holding the number of characters pushed i.e the text “SILVER” would
be held in the stack as:
6 Top
S
I
L
V
E
R
Use pseudocode to write a procedure that will take a text string passed to it and push it to the
stack in the format defined above. You may assume any given input will fit in the stack.
procedure passToStack(passString)
stringLen = passString.Length()
if stringLen == 0 then
stack[0]=0
else
stackPtr = 0
stringPtr = stringLen - 1
for i = 1 TO stringLen
stack[stackPtr] =
passString[stringPtr]
stackPtr = stackPtr + 1
stringPtr = stringPtr -1
next i
stack[stackPtr] = stringLen
endif
endprocedure
The enqueue method:
* takes as a parameter the item to insert in the queue
* checks if the queue is full
* reports an error and returns false if the queue is full
* does the following if the queue is not full:
o adds the item to the array at the tail position and adjusts the pointer(s)
o returns true
The attribute numItems stores the number of items currently in the queue.
Write an algorithm, using pseudocode or program code, for the enqueue method.
public function enqueue(newItem : items) : boolean
if numItems = 10 then
print(“Error: The queue is full”)
return false
else
theItems[tail] = newItem
if tail = 9 then
tail = 0
else
tail += 1
endif
numItems += 1
return true
endif
endfunction
Write a programming statement to declare an instance of itemQueue called myItems
myItems = (new) itemQueue()
Write a procedure, insertItems(), to ask the user to input the data for an item. The
item is then added to the queue myItems. The user is continually asked to input data
items until the queue is full.
procedure insertItems()
newItem : Items
itemCount = myItems.getnumItems()
while itemCount < 10
newItem.itemName = input(“Enter the item name”)
newItem.cost = input(“Enter the item cost”)
newItem.dateArrival = input(“Enter the date of arrival”)
newItem.transferred = input(“Has it been transferred?”)
myItems.enqueue(newItem)
itemCount = itemCount + 1
endwhile
myItems.setnumItems(itemCount)
endprocedure
When the main program ends, the items and the queue no longer exist.
Describe how Kamran could amend the program to make sure the items and queue still
exist and are used the next time the program is run.
go to question 7c using this link
https://drive.google.com/file/d/16ne5XXRiSE2PsB07MlbFH9y5UY8Qvyoh/view
how did u do
answers:
https://drive.google.com/file/d/1BdwZeoZdx9yhCFE8o8EeYy_0UDSwFSh8/view