8 basic data structures plus a guide to algorithms
If you want to become a programmer, you should master the basic data structures. It will help you stand out from the crowd and make your resume shine with one in-demand skill.
Learning about algorithms is just as valuable, as they are used to solve problems by using the data you structure.
In this guide, I’ll share the eight most common data structures along with a simple guide to writing algorithms. But before that, let’s start from the basics!
Editor’s note: GoDaddy now has a hosting centre located in India, enabling faster load times and better security for customer websites. You can read more here.
What is a data structure?
A data structure is a specific method to store and organize data in a computer to be easily used to efficiently perform operations on it.
When data is unstructured, it is not organized or doesn’t have a defined data model.
Then, it is not suitable for the analysis or operations. Unstructured data is a very common problem. It is estimated that 80% of the world’s data is unstructured.
Most organizations collect data and store it in an unorganized way that is not effective in making data easy to use.
There are different types (forms) of data structure, and each type could be effective for some operations but not for others.
So it is the programmer’s job to understand which data structure is suitable to analyze the data efficiently so it can be used to solve the problems or achieve the goal.
Data structures are the fundamentals of software engineering and computer science and are being used in most software systems.
Editor’s note: One of the best ways to impress clients is with a web professional certificate from GoDaddy Academy. Taught online by industry experts, the video-based certifications end with a live project-based assessment.
Most common types of data structure
Now let us discuss eight of the most common data structures.
1. Arrays
The first in our list of basic data structures is one of the simplest data structures. An array is a fixed-size structure that stores multiple items of the same kind of data sequentially.
An array contains the same but fixed-size data type elements (also known as variables). That’s why you can’t change the array’s size. In an array index, each item starts with “0.”
An array has elements inside each container and lined up small containers in a sequence.
As arrays are fixed in size, it is not possible to insert or delete an element to an array.
To add an element, you need to create a new array with increased size (+1), then copy the existing elements and add a new element to it.
They are mostly used as a structure to build more complicated data structures and for sorting algorithms.
2. Linked lists
A linked list is a linear data structure where items are arranged in linear order and linked (connected) to each other. That’s why you cannot access random data; you need to access data only in order (sequentially).
In a linked list, the first element in the list is known as “Head,” and the last item is known as “Tail.”
Each item is called a “node,” and each contains a pointer and a key in the linked list. The Pointer takes you to the next Node known as “next.”
You can traverse each item from head to tail (in forward direction) by creating a single linked list. In the same way, a double-linked list can traverse in both directions; from head to tail (forward direction) and from tail to head (backward direction).
They are used in switching between programs for symbol table management.
Related: How to grow your design business into an agency
3. Stacks
Next on our list of basic data structures is Stack. Stack is also a linear order structure, but it works in a LIFO (Last in First Out) order. That’s why it is also known as LIFO structure. It means the last-placed element can be accessed first.
You can push (add a new) or pop (delete) an element on/from the top of the elements, like a stack of plates in the real world.
Stacks are mostly used in recursion programming to implement function calls and mathematical expressions for parsing and evaluations.
4. Queues
Queues are the same as Stack structure, but they don’t follow the LIFO model. A queue follows the FIFO (First in First Out) model. This means the first element can be accessed first.
A line of people entering the building is a great example. The first person (starting the line) will enter the building before anyone else, and the person standing in the last of the line will enter in the last.
In the same way, you can add a new element (enqueue) at the end of the structure and dequeue (delete) an element from the starting of the structure.
They are mostly used in multithreading to manage threads as well as to execute priority queuing systems.
5. Hash tables
The hash table data structure connects each value with a key and stores them.
From a group of similar objects, you can easily find a specific object.
To understand the Hash table, you can take it as a student ID (Key) that a college assigns to students. All the details related to a student can be easily found by using the student ID.
To map any size of data set to one fixed size, the hash table uses the hash function. The values returned by a hash function are known as hash values.
They are mostly used to create associate arrays, database indexes, and a “set.”
6. Trees
Another basic data structure is a Tree. In the tree structure, data is linked together as in the linked list but organized hierarchically, just like the visual representation of a person’s family tree.
There are various types of trees like:
- Binary search tree (BST)
- Red-black tree
- B tree, treap
- Splay tree
- N-ary tree
- AVL tree
And each type is suited for certain applications.
For example, the BST data structure stores values (data) in sorted order. In a BST, every node comprises the following attributes:
- Key is the stored value in the node
- Left is the Pointer to the left child node
- Right is the point to the right child node
- P is the Pointer to the parent node
BST structure is widely used in different types of search operations, and other types of tree structures are used to create expression solvers and in wireless networking.
7. Heaps
A heap is a specific type of binary tree where the parent nodes are compared to their child nodes, and values are arranged in the nodes accordingly.
A heap can be represented as an array or a binary tree as you can see in the images below:
Binary Tree Representation
Array Representation
There are two types of heaps:
- Min heap, where the parent’s key is equal or less than the keys of its children.
- Max heap, where the parent’s key is greater than the keys of its children.
Heaps are widely used to find the largest and smallest values in an array and to create priority queues in algorithms.
8. Graphs
A graph is a non-linear and abstract data structure that consists of a fixed (finite) set of nodes or vertices and is connected by a set of edges. Edges are the arcs or lines that simply connect nodes in the graph.
Graphs are great for solving real-world problems, as well as representations of digital networks. They’re also used for the representations of the networks like circuit networks.
Related: Top 5 things to know before applying for web developer jobs
What is an algorithm?
An algorithm is a finite set of step-by-step (written in order) instructions to solve a specific problem or to get the desired output.
Algorithms are generally developed independently of the underlying language. It means you can use an algorithm in more than one programming language.
As I mentioned, data structures are used to store and organize data. Algorithms are used to solve problems by using that data.
Here are some qualities of a good algorithm:
- The input and output of an algorithm must be defined precisely.
- In the algorithm, each step must be clear and univocal.
- An algorithm must be the most efficient method to solve a problem over other ways.
- An algorithm should be written in such a way that it can be used for other languages.
An algorithm is not a complete program or code, it is just the solution or core logic of a problem that can be expressed using a flowchart or as pseudocode (plain language description with steps).
Characteristics of an algorithm
We can’t count each and every procedure (solution) as an algorithm. An algorithm must have these characteristics to follow:
- Unambiguous: An algorithm must be univocal and clear. Each and every step (phase), input, and output must be clear and have only one meaning.
- Input: There should be 0 or more inputs to the algorithm.
- Output: There should be at least one or more outputs that should be the same as the expected output.
- Finiteness: There should be a finite number of steps for the algorithm.
- Feasibility: The algorithm should be achievable (feasible) with available resources.
- Independent: The algorithm must have well-defined instructions and should be independent of any code or program.
We can say an algorithm is efficient if it has less execution time and takes less memory space.
That’s why the performance of an algorithm is measured on the basis of these properties:
Time complexity
This is the time required by an algorithm during the execution.
Space complexity
This is the memory space required by an algorithm during the execution.
Types of algorithms
Here are some important categories of algorithms from the viewpoint of data structure.
- Search: The algorithm to find an item in the data structure.
- Sort: To sort items in a specific (desired) order.
- Insert: To add/insert items in the data structure.
- Update: The algorithm to update existing items in the data structure.
- Delete: To delete items from the data structure.
How to write algorithms
There is no standard rule to write an algorithm.
An algorithm is never written to support a particular code. There are some basic code constructs that are used in every programming language like flow control, loops, and others.
You can use these basic code constructs to write an algorithm.
Most of the time, programmers use a step-by-step approach to write algorithms, but it might not be possible each and every time.
Each algorithm must be planned and written according to the problem domain. You must have a clear understanding of the problem domain to write an algorithm to solve that problem.
Example of an algorithm
Here is an example of an algorithm for subtracting two numbers and showing the result.
Step 1 − START
Step 2 − proclaim the three integers x, y & z
Step 3 − define values of x & y
Step 4 − subtract values of x & y
Step 5 − save the output of step 4 to z
Step 6 − print z
Step 7 − STOP
The same algorithm can also be written as:
Step 1 − START SUBTRACT
Step 2 − obtain values of x & y
Step 3 − z ← x – y
Step 4 − print z
Step 5 − STOP
In the same way, there are many other ways to get the same output (subtraction).
The question is, which method or algorithm will be most efficient to use?
In the above examples, the second method is more efficient and useful than the first one. Here is why:
- There are no unrequired definitions or steps in the algorithm so it will be easier for a programmer to analyze it
- Also, it will take less time to execute
Algorithms give a roadmap to programmers regarding how to build (code) a program.
Final words about basic data structures
We have explored the eight most common data structures in this guide that every programmer should know, along with an explanation of how to write algorithms. And once you have a basic understanding of both, you can even practice creating new ones.
Frequently Asked Questions
What are basic data structures?
Data structure is a method to store and organize data so it can be easily used to perform operations to get desired results. Arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs are the basic data structures.
What is the most basic data structure?
Arrays are the most basic data structures, and many others are derived from them. Stacks and queues are examples of them.
Why use data structures?
Data structures organize data in such a way that makes it easier to perform operations on the data.
Programmers can save lots of time and can work more efficiently by using the right kind of data structures.
Also, it is impossible to design the most efficient algorithms without the help of data structures.
How are data structures classified?
Data structures have two categories: primitive data structure and non-primitive data structure.
Primitive data structures are basic data structures and can be used directly according to the machine’s instructions.
Non-primitive data structures are more complex or advanced data structures derived from primitive data structures.
How data structures are used in the real world
There are unlimited real-life applications to use data structures. Here are some examples to use data structures in the real world.
- In a chess game to store the possible moves
- On a social networking website (like Facebook) to store the friendship information (i.e., who is connected with whom)
- In an internet browser to implement the back step (backward) functionality
And so on…
Should I learn data structures and algorithms before programming?
You need to learn programming before learning data structures and algorithms. You must have a basic knowledge of programming to start learning DSA (data structures and algorithms).
What is a binary tree?
It is a type of data structure in which each record (parent node) could have a maximum of two children (also known as a left node and a right node).
________________________________
Got a few minutes? (Probably not.)
Fumbling for login credentials, running endless updates, explaining product purchases… No thanks. We built the Hub from GoDaddy Pro to save you an average three hours per month for every client site you maintain.