Today we will go over two of the most basic data structures, which are stack and queue!
What is Stack?
- Stack is a linear data structure and has a characteristic of LIFO (Last in Last Out). LIFO means what was put in at the end comes out first. Think of stack of dishes: The dish you put on the top at the end is the first one you take.
- Push is used to put in data, pop is used to take out data, and peek is used to take a look at the data that was put in at the end.
- Stack is often used to access previous work or data.
- pop(): Remove the top/last element of stack.
- push(): Add an element.
- peek(): Return the most recently added element to the stack.
- empty(): Check whether a stack is empty. Return a boolean value.
- swap(): Swap elements in stack.
Pseudocode for Stack
- Create a new class
- Make an empty array with Constructor (set this)
- Make a push function to push an element to an empty array.
- Make a pop function to return the last element in the array.
- Make a peek function that returns the element at -1 index of the array.
What is Queue?
- Queue is a linear data structure and has a characteristic of FIFO (First in First Out). FIFO means the data put in first comes out first. Think of a line at a restaruant or an amusement park.
- Enqueue is putting data in and Dequeue is taking data out.
- Queue is often used as a buffer that temporarily saves work that has to be done in an order.
- enqueue(): Add an element
- dequeue(): Remove an element
- peek(): Return the first element of queue
- isEmpty(): Check if a queue is empty. Return a boolean value.
- printQueue(): Return all elements of queue.
Pseudocode for Queue
- Create a new class
- Make an empty array with Constructor
- Make a enqueue function that pushes an element to an empty array.
- Make a dequeue function to shift element from an array.
- Make a peek function to return the element at 0 index.
- Make a isEmpty function that will check the length of an array and return true if 0 and false otherwise.
- Make a printQueue function that returns all the elements in the array.
Stack and Queue are often used and two of my favorite data structures. Know them well enough to use them whenever!