In my previous article, 102(a):Deep dive into data structures, I expounded more on the various types of data structures that are commonly used and theoretically explained how they are implemented.
In this article, we will get our hands a little bit dirty by practically implementing them.
Note:
I’ve used python for implementation since it is human friendly thus can be understood by a vast number of people.
Link to the GitHub repository that contains the sample codes can be found in their respective sections
1.Stack data structure:
As we stated earlier, stack is a data structure that follows the LIFO principle, that is, elements that get in last come out first. Any time an element is added it goes onto the top of the stack and the only element that can be removed is the element on top of the stack. It is an example of a linear data structure
Types of stacks:
There are two types of stacks:
- Register stack: It is a memory element present in the memory unit and can handle a small amount of data only. It is small compared to memory stack.
- Memory stack: It handles a large amount of memory data. It’s height is quite flexible as compared to register stack.
Applications of stack:
- Reverse a word: you can push a given word to stack letter by letter then pop letters from the stack.
- Redo and undo features in places such as editors
- Backtracking algorithms, as it helps you come back from the previous state. This can be seen in games such as chess, finding your way through a maze e.t.c
- Forward and backward features in browsers
Implementation of stack:
Stacks can be implemented in two ways:
- Arrays
- Linked lists
a)Implementing stacks using arrays:
output
b)Implementing stacks using linked lists:
Output:
2.Queue data structure:
This is also a type of linear data structure in which addition of an element takes place from one end called the rear(tail) and removal of an element takes place from the other end also called front(head).
It follows the FIFO (first in first out) principle, meaning the first element to be inserted will be the first element to be removed.
Applications of queue:
- Serving requests on a single shared resource e.g printer
- In call center stations, queues are used to put callers on hold until a service representative is free
Implementation of queue:
Queue can be implemented using:
- Arrays
- Linked lists
a)Implementing using arrays:
Output:
Github link
b)Implementing using Queue from queue library:
3.HashMap data structure:
As I stated in the previous article, a HashMap is a type of data structure that maps certain keys to certain values.
Implementation of HashMap:
HashMaps are implemented in python using dictionaries
4.Graph data structure:
Graphs are a type of non-linear data structure that arranges data as multiple nodes. They are connected together using edges. Nodes represent entities where data is stored and the relationship between nodes are expressed by edges.
Types of graphs:
- Undirected: a type of graph where all the edges are bi-directional, the edges do not point at a specific direction.
- Directed graph: a type of graph where all edges are uni-directional, the edges point at a specific direction.
- Weighted graph: a graph that has a value associated with every edge.
- Unweighted graph: a type of graph that has no value or weight associated with the edge. Applications of graph:
- Used in social networks such as Facebook and LinkedIn
- Used in blockchain technology, where the nodes are blocks that store many transactions while edges connect many subsequent blocks
- Google maps also uses graphs
- It also helps define the flow of computational software programs. Implementation of graphs:
5.Set data structure:
This is a type of data structure that can store any unique values. It does not allow repeating of values
Applications of set data structures:
- It is used in eliminating duplicates
- It is used to sort, because it is ordered
- It is also used for searching, checking whether a given element is present in the in the set.
Implementations of set data structures:
They can be implemented using the set() function in python
Output:
Thank you for reading this far!
Top comments (0)