Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Static Data structure?
3.
What is Dynamic Data Structure?
4.
Difference between Static Data Structure and Dynamic Data Structure
5.
Features of Static Data Structures
6.
Features of Dynamic Data Structures
7.
Advantages and Disadvantages of Static Data Structures
7.1.
Advantages
7.2.
Disadvantages
8.
Advantages and Disadvantages of Dynamic Data Structures
8.1.
Advantages 
8.2.
Disadvantages
9.
Examples of Static Data Structure
10.
Examples of Dynamic Data Structure
11.
Frequently Asked Questions
11.1.
What are dynamic data types?
11.2.
How many data structures are there?
11.3.
Which is better static or dynamic?
12.
Conclusion
Last Updated: Jun 11, 2024
Medium

Static Data Structure vs Dynamic Data Structure

Author Muskan Sharma
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Let's first have a brief understanding of data structures. A data structure is basically 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, we can use the array data structure to store data in contiguous memory locations. 
  • We can use linked lists to efficiently perform insert/delete operations at the ends. 
  • Similarly, if we want to keep data sorted while inserting/deleting new data, we can use set/multiset to organize our data.
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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Dynamic Data Structure?

In dynamic data structures, the size of the data structure can be modified after initialization. It means we can add/delete elements from the data structure, thus modifying its size at runtime. 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

BasisStatic Data StructureDynamic Data Structure
Size Static Data Structure have fixed size.Dynamic Data Structure have dynamic size, means it can be increased and decreased.
Memory AllocationMemory allocated at compile timeMemory allocated at runtime
Memory ManagementNo dynamic memory allocation or deallocationDynamic memory allocation and deallocation
Insertion and DeletionNot efficient for frequent insertions/deletionsEfficient for frequent insertions/deletions
ExamplesArrays, 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 Coding Ninjas Studio

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.

 

Previous article
Has-A Relationship in Java
Next article
What is truncation in Java?
Live masterclass