Static and Dynamic Data Structures (original) (raw)

Last Updated : 12 Jul, 2025

Data structures are the fundamental building blocks of computer programming. They determine how data is organized, stored, and manipulated within a software application. There are two main categories of data structures: **static and **dynamic. Static data structures have a fixed size and are allocated in memory during compile-time, while dynamic data structures can grow and shrink in size during runtime. This article will provide an overview of the key differences between **static and **dynamic data structures, their use cases, and the trade-offs to consider when choosing between them.

Table of Content

What is Static Data Structure?

Static data structures are characterized by their fixed size and predefined memory allocation. This means that the amount of memory allocated to the structure is determined at compile time and remains constant throughout the program's execution. Examples of static data structures include arrays .

Features of Static Data Structures:

There are many features of static data structures. Let's discuss the most popular of them:

Advantages of Static Data Structures:

Disadvantages of Static Data Structures:

Examples of Static Data Structures:

Array : An array is a collection of elements identified by index or key values. The size of the array is specified at the time of creation and remains constant. For example, an array of integers can be declared as int arr[8]; in C++, which creates an array of size **8.

Memory-Representation-of-Array-(1)

Note: Problem with above Array implementation: Once we create the Array we cannot alter the size of the array. So the size of the array is unalterable

Implementation:

C++ `

#include using namespace std;

int main() { // Declaration of an array of size 6 int array[6];

// initialize and printing values of array elements
for (int i = 0; i < 6; i++) {
    array[i] = i;
    cout << array[i] << " ";
}
cout << endl;

return 0;

}

Java

public class Main { public static void main(String[] args) { // Declaration of an array of size 6 int[] array = new int[6];

    // initialize and printing values of array elements
    for (int i = 0; i < 6; i++) {
        array[i] = i;
        System.out.print(array[i] + " ");
    }
    System.out.println();
}

}

Python

def main(): # Declaration of a list of size 6 array = [0] * 6

# initialize and printing values of array elements
for i in range(6):
    array[i] = i
    print(array[i], end=" ")
print()

if name == "main": main()

JavaScript

function main() { // Declaration of an array of size 6 let array = new Array(6);

// Initialize and print values of array elements
for (let i = 0; i < 6; i++) {
    array[i] = i;
    process.stdout.write(array[i] + " ");
}
console.log();

}

// Call the main function to execute the code main();

`

**In the above code: we declared an array of the fixed size. After that, we initialize some value using a loop and printed them.

What is Dynamic Data Structure?

Dynamic data structures are flexible in size and can grow or shrink as needed during program execution. This adaptability makes them suitable for handling data of varying sizes or when the size is unknown beforehand. Examples of dynamic data structures include linked lists, stacks, queues, and trees.

Features of Dynamic Data Structures:

There are many features of dynamic data structures. Let's discuss the most popular of them:

Advantages of Dynamic Data Structures:

Disadvantages of Dynamic Data Structures:

Examples of Dynamic Data Structures:

Linked List : Linked Lists are linear data structures where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays.

Singly-Linked-List-(1)

**Implementation:

C++ `

#include using namespace std;

// defining a linked list class LinkedList { public: int data; LinkedList* next; };

int main() { // creating a linked list class LinkedList* node1 = new LinkedList(); node1->data = 1;

class LinkedList* node2 = new LinkedList();
node2->data = 2;

class LinkedList* node3 = new LinkedList();
node3->data = 3;

// linking all the nodes

node1->next = node2;
node2->next = node3;
node3->next = NULL;

// printing value of linked list
cout << node1->data << " " << node2->data << " "
     << node3->data << endl;

return 0;

}

Java

public class LinkedList { int data; LinkedList next;

public static void main(String[] args)
{
    // creating a linked list
    LinkedList node1 = new LinkedList();
    node1.data = 1;

    LinkedList node2 = new LinkedList();
    node2.data = 2;

    LinkedList node3 = new LinkedList();
    node3.data = 3;

    // linking all the nodes
    node1.next = node2;
    node2.next = node3;
    node3.next = null;

    // printing value of linked list
    System.out.println(node1.data + " " + node2.data
                       + " " + node3.data);
}

}

// This code is contributed by Shivam

Python

class LinkedList: def init(self): self.data = None self.next = None

creating a linked list

node1 = LinkedList() node1.data = 1

node2 = LinkedList() node2.data = 2

node3 = LinkedList() node3.data = 3

linking all the nodes

node1.next = node2 node2.next = node3 node3.next = None

printing value of linked list

print(node1.data, node2.data, node3.data)

This code is contributed by shivam

JavaScript

class LinkedList { constructor(data) { this.data = data; this.next = null; } }

function main() { // creating a linked list let node1 = new LinkedList(1); let node2 = new LinkedList(2); let node3 = new LinkedList(3);

// linking all the nodes
node1.next = node2;
node2.next = node3;
node3.next = null;

// printing value of linked list
let currentNode = node1;
let output = "";
while (currentNode !== null) {
    output += currentNode.data + " ";
    currentNode = currentNode.next;
}
console.log(output.trim());

}

main();

`

**In the above code: A class LinkedList is defined with two members: data (an integer) and next (a pointer to the next node) and three nodes (node1, node2, node3) are created with data values of 1, 2, and 3 respectively. These nodes are then linked together to form a linked list: node1 points to node2, node2 points to node3, and node3 points to NULL, indicating the end of the list. Finally, the data values of the nodes are printed out, resulting in the output 1 2 3.

Difference between Static and Dynamic Data Structure:

Below are the comparison of static and dynamic data structure:

Feature Static Data Structure Dynamic Data Structure
Memory Allocation Memory allocation size is fixed at compile time Memory allocation size can be adjusted at runtime
Flexibility Not flexible, size cannot be changed once allocated Flexible, size can be adjusted as needed
Efficiency Typically more memory efficient May be less memory efficient due to dynamic resizing
Performance Generally faster access and manipulation May have slower access and manipulation due to dynamic resizing
Examples Arrays, Linked Lists (with fixed size) Linked Lists (with dynamic size), Stacks, Queues
Implementation Typically implemented using arrays Implemented using pointers or dynamic memory allocation
Usage Suitable for applications with fixed-size requirements Suitable for applications with dynamic size requirements, or where size is unknown upfront