Principle of Computation and Application: Stack, Queue

nuttchai
2 min readDec 13, 2019

--

Part 3: Stack, Queue

Stack and Queue are the essential part of programming that we also need to understand. In fact, these methods are based on Array class.

Stack is an Abstract Data Type (ADT) which is is an abstraction of a data structure. It is Last-In, First-Out (LIFO) data structure. In the other words, the first element which is put on the top of the stack will be the last removed element in stack.

In Stack Operation:

  • push() − add element on the top of the stack
  • pop() − remove the uppermost element in stack
  • peek() − return the last element that was put into stack without removing it
  • is_empty() − check whether stack is empty or not
// Stack Examples = list()       // set s to be an empty stack
s.push(3) // s is now [3]
s.push(6) // s is now [3, 6]
s.pop() // s is now [3]
s.is_empty() // return False
s.pop() // s is now []

Queue is a Linear Data Structure which is quite similar to stack (however, it doesn’t). It is First-In, First-Out (FIFO) data structure. In the other words, the first element that is added into the queue will also be the first element that will be removed out of the queue too.

In Queue Operation:

  • enqueue() − add the element into queue
  • dequeue() − remove the first element of queue
  • peek() − check the first element of the queue
  • is_empty() − check whether queue is empty or not
// Queue Exampleq = list()          // set s to be an empty stack
q.enqueue(3) // s is now [3]
q.enqueue(6) // s is now [3, 6]
q.dequeue() // s is now [6]
q.dequeue() // s is now []
q.is_empty() // return True

Conclusion

Both methods are based on array class which stack is LIFO that first added element will be the last removed element, and queue is FIFO that first added element will also be the first removed element.

Thank You for Visiting My Blog!

--

--