Basic data structures for temporary data holding

Jyothi
3 min readSep 22, 2020

As I understand many engineers are familiar in basic data structures, there are two basic data structures to start with. These data structures are stacks and queues. These data structures are used for temporary holding of data or passing data from one entity to another entity, etc.

Stack is a data structure called Last In First Out (LIFO). This is basically used in situations where we store a pile of data records or elements where the first inserted or added elements will be at the bottom of the list and the newly added element is at the top of the list. So, to remove the first element in the list we should remove all other elements which are on top of the list.

Stack data structure has three operations:

· Push or insert an element into stack

· Pop or remove an element from the stack

· Peek or top to get the element info which is on top of the stack

I tried to list down some requirements where stack data structure can be useful:

· If the requirement is to hold the elements in the order such that the removal of those elements return in reverse order as the first element is popped in the last order.

· If there is a requirement of dependencies. If A is depending on B, B is depending on C. A is inserted before B, B is inserted before C. So, when C is completed its task, C is popped from the stack, then B can do its task, then after B task, B can be popped, then A can do its task.

· If there is a requirement of giving priority to the newer element.

· If there are conditions such that based on the next received data, some part of previously saved data should be popped out.

· Where the elements are inserted and deleted from single position

There are lots and lots of use cases, I tried to list some:

· Syntax format checking: when a program is written in high level language (C/C++/Python,..), it should be converted to machine readable code. In this process, compiler has to check for syntax errors. Syntax errors can be non-matching brackets, non-matching start and end comments, etc. Stacks help in checking this type of errors. Pushing the open brackets or beginning of comments, etc. Pop if the corresponding matching end brackets or end of comments are seen. If there is no matching, returning error. Simplifying the code by making use of stack data structure.

· Undo/redo operations where the latest operation is saved on top of the stack.

· Call stack: Saving the set of function calls where as an example func1() calling func2(), func2() invoking func3().

· Reversing a string or reversing a set of elements

· Back button implementation on web browser

· Parsing of HTML or XML document where there can tag inside tag inside tag, etc.

· Mathematical expression evaluation: Changing an infix format to postfix format.

· There are mathematical problems where we keep pushing and popping data based on conditions related to the problems.

Lots of data on internet on data structures, final challenge is to find a feasible data structure for a problem for further implementation.

--

--