Nodes
Node
references the next Node
in the stack, but not the previouspeek
you will view the value of the top Node in the stack. When you attempt to peek
an empty stack an exception will be raised.Stack Visualization
top
push
something to the stack, it becomes the new top
pop
something off the stack, you remove the current top
and set the next top
as top.next
Nodes
are in the stack since you are always pushing to the top of the stackNode
, you push
it to the stack by assigning it as the new top
with its next
property equal to the old top
top
to the old top
by setting the top.next
property to equal the old top
top
value equal to the value of the new Node
Code example
ALOGORITHM push(value)
// INPUT <-- value to add, wrapped in Node internally
// OUTPUT <-- none
node = new Node(value)
node.next <-- Top
top <-- Node
pop
a node off the stack since you are always removing one off the top of the stackpop
a Node
off the stack, first you check isEmpty
to ensure no exception is raised, then you set a temp
variable to hold the old top
, then you assign the value of top
to the value that the next
property is referencing, then you clear the next
reference in the temp
variable, then return temp
to the usertemp
variable and assign the current top
to it
top
value to the temp.next
property
next
property in the temp
variable
temp
to the userCode example
ALGORITHM pop()
// INPUT <-- No input
// OUTPUT <-- value of top Node in stack
// EXCEPTION if stack is empty
Node temp <-- top
top <-- top.next
temp.next <-- null
return temp.value
peek
only looks at the top
Node of the stacktop.value
if the stack is not emptyCode example
ALGORITHM peek()
// INPUT <-- none
// OUTPUT <-- value of top Node in stack
// EXCEPTION if stack is empty
return top.value
ALGORITHM isEmpty()
// INPUT <-- none
// OUTPUT <-- boolean
return top = NULL
peek
you will view the value of the front
Node in the queue. If called when the queue is empty an exception will be raised.next
property of the last Node in the queue to the value of the new Node
Code example
ALGORITHM enqueue(value)
// INPUT <-- value to add to queue (will be wrapped in Node internally)
// OUTPUT <-- none
node = new Node(value)
rear.next <-- node
rear <-- node
temp
variable and assign front Node to it
temp.next
attribute and return temp
to the user
Code Example
ALGORITHM dequeue()
// INPUT <-- none
// OUTPUT <-- value of the removed Node
// EXCEPTION if queue is empty
Node temp <-- front
front <-- front.next
temp.next <-- null
return temp.value
front
node and returns its valueCode example
ALGORITHM peek()
// INPUT <-- none
// OUTPUT <-- value of the front Node in Queue
// EXCEPTION if Queue is empty
return front.value
Code example
ALGORITHM isEmpty()
// INPUT <-- none
// OUTPUT <-- boolean
return front = NULL