Table of contents
1.
What is a Static Data structure?
2.
What is Dynamic Data Structure?
3.
Difference Between Static Data Structure and Dynamic Data Structure
4.
Features of Static Data Structures
5.
Features of Dynamic Data Structures
6.
Advantages and Disadvantages of Static Data Structures
6.1.
Advantages
6.2.
Disadvantages
7.
Advantages and Disadvantages of Dynamic Data Structures
7.1.
Advantages 
7.2.
Disadvantages
8.
Examples of Static Data Structure
9.
Examples of Dynamic Data Structure
10.
Frequently Asked Questions
10.1.
What are dynamic data types?
10.2.
How many data structures are there?
10.3.
Which is better static or dynamic?
11.
Conclusion
Last Updated: Jul 10, 2024
Medium

Static Data Structure vs Dynamic Data Structure

Author Muskan Sharma
0 upvote

A data structure is a way to store data in a particular organized way. We can design different types of data structures to meet specific requirements. Using data structures, we can reduce the time complexity of various operations like insert/search/update/delete, etc.

  • For example, the array data structure can store data in contiguous memory locations. 
  • We can use linked lists to perform insert/delete operations at the ends efficiently. 
  • Similarly, if we want to keep data sorted while inserting/deleting new data, we can use set/multiset to organize our data.

There are two types of data structures:

  1. Static Data structure
  2. Dynamic Data structure
Static Data Structure vs Dynamic Data Structure

What is a Static Data structure?

In static data structure, as the name suggests, the size of the data structure is fixed. The size of the structure has to be specified at the time of initialization, and it cannot be changed later. However, we can modify the content of the data structure without modifying the memory allocated to it.

What is Dynamic Data Structure?

The size of the dynamic data structure is not fixed and can be changed during the execution of operations. Which means we can add/delete elements from the data structure. Dynamic data structures are designed to facilitate change of data structures in the run time. Dynamic data structures are memory efficient and especially useful when we don't know the data size beforehand.

Difference Between Static Data Structure and Dynamic Data Structure

Basis Static Data Structure Dynamic Data Structure
Size  Static Data Structure has a fixed size. Dynamic Data Structure have a dynamic size, which means it can be increased and decreased.
Memory Allocation Memory allocated at compile time Memory allocated at runtime
Memory Management No dynamic memory allocation or deallocation Dynamic memory allocation and deallocation
Insertion and Deletion Not efficient for frequent insertions/deletions Efficient for frequent insertions/deletions
Examples Arrays, Stacks, Queues, etc. Linked Lists, Trees, Hash Tables, etc.

Features of Static Data Structures

Here are some features of static data structures:

  • The size of a static data structure is fixed and determined at compile-time.
  • Memory allocation for static data structures is done at compile-time and cannot be changed during runtime.
  • Static data structures can be accessed randomly or sequentially, and access time is usually faster than dynamic data structures.
  • Static data structures are best suited for applications that have a known maximum size and a fixed number of elements.
  • Insertion and deletion operations can be slower in static data structures, especially if the structure needs to be resized or elements need to be shifted.

Features of Dynamic Data Structures

Here are some features of dynamic data structures:

  • The size of a dynamic data structure is not fixed and can change during runtime.
  • Memory allocation for dynamic data structures is done during runtime using techniques such as heap memory allocation or pointer-based data structures.
  • Dynamic data structures can grow or shrink as needed and are best suited for applications that have varying sizes or a changing number of elements.
  • Insertion and deletion operations are generally faster in dynamic data structures since elements can be added or removed without the need to shift other elements.
  • Accessing elements in a dynamic data structure may be slower than in a static data structure since memory may be spread out in different locations.

Advantages and Disadvantages of Static Data Structures

Let's look into the advantages and disadvantages of Static Data Structures:

Advantages

Here are some advantages of static data structures:

  • Accessing elements is faster than dynamic data structures.
  • Memory is allocated at compile-time, eliminating the need for additional memory management during runtime.
  • They can be more efficient for smaller datasets with fixed sizes.
  • They are easier to implement and require less overhead compared to dynamic data structures.

Disadvantages

Here are some disadvantages of static data structures:

  • They have a fixed size, which can lead to memory wastage for larger or variable-sized data sets.
  • They are not flexible, and adding or removing elements can be inefficient and require resizing the entire structure.
  • They can lead to memory fragmentation and may not be suitable for long-term use.
  • They can be challenging to implement for complex or dynamic applications.

Also see, Duck Number in Java

Advantages and Disadvantages of Dynamic Data Structures

Let's look into the advantages and disadvantages of Dynamic Data Structures:

Advantages 

Here are some advantages of dynamic data structures:

  • They can change in size and shape during runtime, making them flexible and adaptable to different applications.
  • Memory usage is optimized, as it can grow or shrink based on actual data requirements.
  • Insertion and deletion of elements are faster and more efficient than static data structures.
  • They are well-suited for large and complex data sets.

Disadvantages

Here are some disadvantages of dynamic data structures:

  • Memory allocation and deallocation can lead to performance issues, such as fragmentation, overhead, or memory leaks.
  • They may require more memory than static data structures for bookkeeping and pointer storage.
  • Accessing elements can be slower than static data structures due to the need for pointer traversal or indirection.
  • They can be more challenging to implement and debug than static data structures.
    Also read - Data Structure MCQ

Examples of Static Data Structure

Examples of static data structures and their implementation in C programming language are:

1. Arrays:

int arr[10]; // an integer array of size 10

 

2. Structs:

struct Employee {
  char name[50];
  int id;
  float salary;
};
struct Employee emp; // an instance of the Employee struct

 

3. Stack with a fixed size:

#define MAX_SIZE 10 // maximum size of the stack
int stack[MAX_SIZE];
int top = -1; // top of the stack
void push(int value) {
   if (top >= MAX_SIZE - 1) {
       // stack is full
   } else {
       top++;
       stack[top] = value;
   }
}
int pop() {
   if (top < 0) {
       // stack is empty
       return -1;
   } else {
       int value = stack[top];
       top--;
       return value;
   }
}

 

4. Queue with a fixed size:

#define MAX_SIZE 10 // maximum size of the queue
int queue[MAX_SIZE];
int front = 0, rear = -1; // front and rear of the queue
void enqueue(int value) {
   if (rear >= MAX_SIZE - 1) {
       // queue is full
   } else {
       rear++;
       queue[rear] = value;
   }
}
int dequeue() {
   if (front > rear) {
       // queue is empty
       return -1;
   } else {
       int value = queue[front];
       front++;
       return value;
   }
}



 

5. Linked lists with a fixed size:

struct Node {
   int data;
   struct Node* next;
};
struct Node* head = NULL;
struct Node* temp = NULL;
struct Node* newNode = NULL;
void addNode(int value) {
   if (head == NULL) {
       head = (struct Node*)malloc(sizeof(struct Node));
       head->data = value;
       head->next = NULL;
   } else {
       temp = head;
       while (temp->next != NULL) {
           temp = temp->next;
       }
       newNode = (struct Node*)malloc(sizeof(struct Node));
       newNode->data = value;
       newNode->next = NULL;
       temp->next = newNode;
   }
}


 

Check out this problem - Queue Implementation

Examples of Dynamic Data Structure

Here are some examples of dynamic data structures along with their implementation in C:

1. Linked List - A collection of nodes, each with a data field and a pointer to the next node.

struct Node {
   int data;
   struct Node* next;
};
void add(struct Node** head, int data) {
   struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
   new_node->data = data;
   new_node->next = (*head);
   (*head) = new_node;
}


2. Stack - A data structure that uses a "last-in, first-out" (LIFO) approach.

struct Stack {
   int top;
   unsigned capacity;
   int* array;
};
struct Stack* create_stack(unsigned capacity) {
   struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
   stack->capacity = capacity;
   stack->top = -1;
   stack->array = (int*) malloc(stack->capacity * sizeof(int));
   return stack;
}
void push(struct Stack* stack, int item) {
   if (stack->top == stack->capacity - 1) {
       printf("Stack overflow\n");
       return;
   }
   stack->array[++stack->top] = item;
}
int pop(struct Stack* stack) {
   if (stack->top == -1) {
       printf("Stack underflow\n");
       return INT_MIN;
   }
   return stack->array[stack->top--];
}

 

3. Queue - A data structure that uses a "first-in, first-out" (FIFO) approach.

struct Queue {
   int front, rear, size;
   unsigned capacity;
   int* array;
};
struct Queue* create_queue(unsigned capacity) {
   struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));
   queue->capacity = capacity;
   queue->front = queue->size = 0;
   queue->rear = capacity - 1;
   queue->array = (int*) malloc(queue->capacity * sizeof(int));
   return queue;
}
void enqueue(struct Queue* queue, int item) {
   if (queue->size == queue->capacity) {
       printf("Queue overflow\n");
       return;
   }
   queue->rear = (queue->rear + 1) % queue->capacity;
   queue->array[queue->rear] = item;
   queue->size++;
}
int dequeue(struct Queue* queue) {
   if (queue->size == 0) {
       printf("Queue underflow\n");
       return INT_MIN;
   }
   int item = queue->array[queue->front];
   queue->front = (queue->front + 1) % queue->capacity;
   queue->size--;
   return item;
}

 

Recommended Article: Difference Between Array List and Linked List , hash function in data structure.

Read about Application of Queue in Data Structure here.

Frequently Asked Questions

What are dynamic data types?

Dynamic data types refer to variables whose data type can change during execution, allowing flexibility in programming. In languages like Python or JavaScript, variables can store different types of data without explicit declaration.

How many data structures are there?

There are various data structures in computer science, including arrays, linked lists, stacks, queues, trees, graphs, hash tables, and more, each serving different purposes in organizing and manipulating data.

Which is better static or dynamic?

The choice between static and dynamic data structures depends on the use case and the specific requirements of the program. Static data structures are faster and more memory-efficient, while dynamic data structures are more flexible and can handle varying data sizes and shapes.

Conclusion

In this blog, we learned about different types of data structures. We also discussed specific examples to illustrate each type of data structure. Data structures are extensively asked in technical interviews. We saw the limitation of each type of data structure to understand to keep the scope for further improvement. It depends on the developers to decide which data structure will suit their requirements provided the time complexity of various operations in different data structures.

Hence learning never stops, and there is a lot more to learn.

Recommended Articles

Static and Instance methods in Java

Application of Linked List

Has-A Relationship in Java

Java OOPs Interview Questions

Java

Do check out some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Code360

You can also consider our Online Coding Courses such as the DSA in PythonC++ DSA CourseDSA in Java Course to give your career an edge over others.

Live masterclass