Don't become Obsolete & get a Pink Slip
Follow DataFlair on Google News & Stay ahead of the game
Some of the advantages of using data structures are as follows:
- They provide an easy way to manage large datasets.
- They simplify the processing of data on a computer.
- These are essential for designing efficient program algorithms.
- A linear data structure traverses its elements sequentially. We can reach only one data item directly. To access other elements, you need the help of that base element.
- Unlike linear data structures, non-linear data structures don’t traverse in a sequence. Every data item connects with numerous other items, reflecting specific relationships.
- Dynamic data structures differ from static data structures in the way that we can modify the memory size allocated to it. These include queue, stack and linked lists.
The following table states the difference between static and dynamic data structures very clearly.
|Static Data Structures|
Dynamic Data Structures
|Memory is either wasted (underflow) or is insufficient (overflow).|
The amount of memory allocated varies, preventing memory wastage.
|Fast access to the data elements since the memory location is decided while writing the program.|
Slower access to each element since memory allocation happens at run time.
|Structures have fixed size, making them predictable and easier to work with.|
Structures vary in size, thus complex algorithms are necessary to deal with them.
|The relationship between different elements remains constant.|
The relationship between the elements changes as the program executes.
1. Linked List
A linked list maintains a list in the memory. The script has the base address of the first element, that contains the link to the next element in the list. You can only access an element (except the base element) using the previous element.
Linked lists store these elements in what we call a node. A node comprises of a data value and the link (address) to the next element. The first node is the head while the last node is the tail of the list. The tail, as shown in the diagram, contains the value NULL in place of a link. The code below creates a node:
Let’s add another node in our code with the following statement at the end of the script:
head.next = new Node(45);
The output in your console window will look something like this:
Note that now the next of the first element contains the link of the subsequent element.
We can implement a stack using a static data structure (array) or a linked list. Both approaches have their own advantages but as a beginner, we will implement stack using an array. The code below creates a new stack and pushes two elements in the stack using the predefined push() function:
<html> <body> <script> let stack = ; //create empty stack using array //push stack.push(65); stack.push(10); </script> </body> </html>
I removed the top element from my stack directly from the console using the pop() function. You can add the statement in your source code as well. The output will look something like this:
A queue is another type of ordered list which follows FIFO (first in first out) algorithm. It has two ends, front (elements added) and rear (elements removed). A common example you may see is a queue of cars in a one-way lane. The first car that enters the lane is the first one to exit. Also, the car in the middle cannot exit the queue until all the cars before it.
The process of inserting an element in the queue is enqueueing while removing an element is dequeuing. Similar to stacks, we can implement queues with the help of both an array and a linked list. Both approaches have their pros and cons, as explained in the difference between static and dynamic data structures. The array implementation of a queue is clear in the example below. Note that I performed dequeue directly on the Browser Console Window.
<html> <body> <script> let queue = ; //create an empty queue //enqueue queue.push(65); queue.push(10); </script> </body> </html>
A graph is a group of a finite number of vertices and edges that connect these vertices. The edges can be ‘directed’ (directed graph) and ‘undirected’ (undirected graph). Unlike trees, who maintain a parent-child relationship between their nodes (vertices), the nodes of the graph maintain a complex relationship among them.
6. Hash table
Hope the information provided was fruitful for you.
Feel free to drop your queries and feedback in the comment section below.