2.3.2 Algorithms for the main data structures

Cards (33)

  • What do algorithms and data structures do together?

    They allow data to be manipulated effectively
  • What type of data structure is a stack?
    First in, last out (FILO)
  • How is a stack typically implemented?
    As an array with a single pointer
  • What is the purpose of the top pointer in a stack?
    It tracks the top element of the stack
  • Why is the top pointer initialized at -1?
    To indicate that the stack is empty
  • What does the size() function return in a stack?
    The number of elements on the stack
  • How is the size of the stack calculated in pseudocode?
    Return top + 1
  • What does the isEmpty() function check in a stack?
    If the top pointer is less than 0
  • What does the peek() function do in a stack?
    Returns the top element without removing it
  • What should be checked before using peek() on a stack?
    Whether the stack has data in it
  • How is an element added to a stack?
    By updating the top pointer and inserting the element
  • What happens during the pop() operation in a stack?
    The top element is removed and returned
  • What must be checked before performing a pop() operation?
    That the stack isn’t empty
  • What is the result of the peek() operation after pushing 1, 5, and 4 onto the stack?
    4
  • What does the isEmpty() function return after pushing elements onto the stack?
    False
  • What happens when trying to push onto a full stack?
    An error is returned
  • What is the final output of the stack operations performed?
    4, 4, False, 2, 5
  • What type of data structure is a queue?
    First in, first out (FIFO)
  • How is a queue typically represented?
    As an array with two pointers
  • What do the front and back pointers in a queue represent?
    Front holds the first element, back the next space
  • How is the size of a queue calculated?
    By subtracting front from back
  • What does the isEmpty() function check in a queue?
    If front and back point to the same position
  • What does the peek() function do in a queue?
    Returns the front element without removing it
  • How is an element added to a queue?
    By placing it at the back pointer's position
  • What happens during the dequeue() operation in a queue?
    The front element is removed and returned
  • What must be checked before performing a dequeue() operation?
    That the queue isn’t empty
  • What is the result of the dequeue() operation on the queue?
    Alex
  • What is the size of the queue after enqueuing Julia?
    5
  • What does the peek() operation return after enqueuing Julia?
    Rajiv
  • What is the final output of the queue operations performed?
    Alex, 5, Rajiv, 5, Rajiv, False
  • What are the main operations associated with stacks?
    • size()
    • isEmpty()
    • peek()
    • push(element)
    • pop()
  • What are the main operations associated with queues?
    • size()
    • isEmpty()
    • peek()
    • enqueue(element)
    • dequeue()
  • Compare the structure and operations of stacks and queues.
    Stacks:
    • FILO structure
    • Single pointer (top)
    • Operations: push, pop, peek

    Queues:
    • FIFO structure
    • Two pointers (front, back)
    • Operations: enqueue, dequeue, peek