• Home
  • University of the People
  • Data Structures (proctored course) CS 3303
  • Understanding Stack and Queue Data Structures

Understanding Stack and Queue Data Structures

In your own words, describe the structure and function of both the stack and queue data structure and discuss how they are different from the perspective of how they are used. The stack is a LIFO (Last In First Out) data structure. This means that when items are added to a list which is implemented using a stack, the item added last will be the first item to be removed. Additionally, items are added and removed from one end. It is considered a good design decision to ensure that the top of the stack is represented by the position n-1 (where n is the number of elements in the stack). This is because this implementation has a desired cost of @(1), unlike using the position 0 since this will give the list an undesirable cost of O(n). Finally, adding an element to a queue is known as a push operation whilst removing an element from the list is known as a pop operation. A queue, on the other hand is a FIFO (First in First Out) data structure. This means that when items are added to a list which implements a queue, the first item added to the list is ,also the first item to be removed from the list. This is analogous to the real world queue, where the first person in a queue is also the first person to be served. Additionally, list items are added from one end, but removed from the other end. Adding an item to a queue has a cost of @(1) time when the rear end of the element is in position n-1 whist removing an element from the queue has a cost of O(n) if the rear element is in position n-1. The reverse is true when the rear end of the queue is chosen to be in position 0. It is also worth noting that adding an item to a queue is considered to be an enqueue operation whilst removing an element from the list is considered to be a dequeue operation