In: Computer Science
Give the peek algorithm (code) for a Stack that can hold 10 nodes. Include error checking, and assume that the array theData and the integer top are data members of the class Stack.
Give the algorithm for the structure Queue that keeps the front and rear “pointers” from progressing through all of memory (assume the Queue array has n elements).
Give the peek algorithm
(code) for a Stack that can hold 10 nodes. Include
error
checking, and assume that the array
-theData- and the integer -top- are data members of
the class -Stack.
Algorithm peek(Stack
stk)
//Input: Stack stk which is an array
of 10 nodes, and the top pointing to top element of the
stk.
//Output: The top element of the
stack, if it exists. null otherwise.
//Given the Stack stk, will check if
there are elements in the stack, and if there are....
//Will return the top element of the
stack, and if there are not.... will return null.
//Note that this algorithm doesn't
modify the Stack by any means....
if(stk.top ==
-1) //This is how an empty stack in the array
representation is used.
return
NULL;
else
return
stk.theData[top]
=================================================================================
Give the algorithm for the
structure -Queue- that keeps the front and rear
"pointers"
from progressing through all of memory
(assume the Queue array has n elements).
Algorithm isFull(Queue
que)
//Input: Queue que which is an array
of n elements, and rear pointing to the recently
inserted
//
element in the array, and front pointing to the first element in
the Queue.
//Output: Returns true if que is full,
and false otherwise.
//Given the queue, if it is full,
returns true, and false otherwise. This helps in
//keeping the rear from going beyond
the allocation.
if(rear ==
n)
return
true;
else
return
false;
Algorithm isEmpty(Queue
que)
//Input: Queue que which is an array
of n elements, and rear pointing to the recently
inserted
//
element in the array, and front pointing to the first element in
the Queue.
//Output: Returns true if que is
empty, and false otherwise.
//Given the queue, if it is empty,
returns true, and false otherwise. This helps in
//keeping the front from going beyond
the allocation.
if(rear ==
front)
return
true;
else
return
false;
Thank you.