Nodes. Each Node references the next Node in the stack, but does not reference its previous.First In Last Out,this means that the first item added in the stack will be the last item popped out of the stack.Last In First Out,this means that the last item added to the stack will be the first item popped out of the stack.example of what a stack looks like:
top. When you push something to the stack, it becomes the new top. When you pop something from the stack, you pop the current top and set the next top as top.next.Pushing a Node onto a stack will always be an O(1) operation. This is because it takes the same amount of time no matter how many Nodes (n) you have in the stack.
When adding a Node, you push it into the stack by assigning it as the new top, with its next property equal to the original top.
next property of the added Node to reference the same Node that top is referencingHere is the pseudocode to push a value onto a stack:
ALOGORITHM push(value)
// INPUT <-- value to add, wrapped in Node internally
// OUTPUT <-- none
node = new Node(value)
node.next <-- Top
top <-- Node
Popping a Node off a stack is the action of removing a Node from the top. When conducting a pop, the top Node will be re-assigned to the Node that lives below and the top Node is returned to the user.
You would check isEmpty before conducting a pop.
Node from the stack is to create a reference named temp that points to the same Node that top points to.re-assign top to the value that the next property is referencing.Node safely without it affecting the rest of the stack.Before we do that though you may want to make sure that you clear out the next property in your current temp reference.temp Node that was just popped off.Here is the pseudocode for a pop
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
When conducting a peek, you will only be inspecting the top Node of the stac
You would check isEmpty before conducting a peek. This will ensure that an exception is not raised. Alternately, you can wrap the call in a try/catch block.
Here is the pseudocode for a peek
ALGORITHM peek()
// INPUT <-- none
// OUTPUT <-- value of top Node in stack
// EXCEPTION if stack is empty
return top.value
Here is the pseudocode for isEmpty
ALGORITHM isEmpty()
// INPUT <-- none
// OUTPUT <-- boolean
return top = NULL
First In Last Out,This means that the first item in the queue will be the first item out of the queue.Last In First Out,This means that the last item in the queue will be the last item out of the queue.example of what a stack looks like:
queue, you use the enqueue action. This is done with an O(1) operation in time because it does not matter how many other items live in the queue (n); it takes the same amount of time to perform the operation.1.First, we should change the next property of current Node to point to the new Node we are adding.
Following the rules of reference types, this means that we must change
rear.nexttonew Node
next property, we can re-assign the rear reference to point to new Node. By doing this, it allows us to keep a reference of where the rear is, and we can continue to enqueue Nodes into the queue as needed.Here is the pseudocode for the enqueue method
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
When you remove an item from a queue, you use the dequeue action. This is done with an O(1) operation in time because it doesn’t matter how many other items are in the queue, you are always just removing the front Node of the queue.
you would check isEmpty before conducting a dequeue. This will ensure that an exception is not raised. Alternately, you can wrap the call in a try/catch block..
temporary reference type named temp and have it point to the same Node that front is pointing too.re-assign front to the next value that the Node front is referencing.front to the second Node in line, we can next re-assign the next property on the temp Node to null.return the value of the temp Node that was just removed.Here is the pseudocode for the dequeue method
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`
When conducting a peek, you will only be inspecting the front Node of the queue.
You would check isEmpty before conducting a peek. This will ensure that an exception is not raised. Alternately, you can wrap the call in a try/catch block.
Here is the pseudocode for a peek
ALGORITHM peek()
// INPUT <-- none
// OUTPUT <-- value of the front Node in Queue
// EXCEPTION if Queue is empty
return front.value
We do not
re-assign the next propertywhen wepeekbecause we want tokeepthe reference to thenext Nodein the queue. This will allow thefrontto stay in the front until we decide to dequeue
Here is the pseudocode for isEmpty
ALGORITHM isEmpty()
// INPUT <-- none
// OUTPUT <-- boolean
return front = NULL
References:
@By Code Fellows/Stacks and Queues