Javatpoint Logo
Javatpoint Logo

Dynamic Data Structure

Dynamic Data Structure is that kind of data structure that changes its size during runtime. The values store in the data structure can be changed easily either it be static or dynamic data structure. But the dynamic data are designed in such a way that both the data and the size of the data structure can be easily changed at the runtime.

The main use case for which the Dynamic Data Structures are defined is to easily facilitate the change in the size of the data structure at the runtime without hindering the other operations that are associated with that data structure before increasing or decreasing the size of the data structure.

In comparison to the static data structures where we can only alter the data present in the data structure at runtime, in the dynamic data structures, both the data present in the data structure and the size of the data structure can be easily changed accordingly. In static data structures, we need to pre-define or calculate the size of the data structure. Because the size of the data structure is already fixed so we can store only a particular amount of data in the static data structures. This problem is solved by the dynamic data structure where we can easily store any amount that will get generated at the run time as in the dynamic data structure, i.e., we can easily increase the size of the data structure at the runtime of the program execution.

Apart from these benefits, there are some disadvantages of the dynamic data structures also. One of the major drawbacks of the dynamic data structure is the issue with memory consumption. As we know the amount of memory that needs to be allocated to the data structure is not fixed in the case of dynamic data structure there is always a potential chance of memory overflow by the data structure. Or on the other hand, there is also a chance of memory underflow if the data structure becomes empty during some operation at the program execution time. These issues can be prevented by having a strict check on the amount of memory which are getting consumed by the dynamic data structure in case of the memory overflow and a check needs to be added on the data stored in the dynamic data structure in case of the memory underflow.

Some of the major examples of dynamic data structures are:

  • Singly Linked List
  • Doubly Linked List
  • Vector
  • Stack
  • Queue
  • Tree

Now let us see Doubly Linked List in detail for a better understanding of the dynamic data structure and how it works. Now let us write a sample code of Doubly Linked List.

Code:

Output:

Please Choose one of the Operations::
1. To Insert Data at the End of the Doubly Linked List.
2. To Insert Data in the Front of the Doubly Linked List.
3. To Insert Data after the head node in the Doubly Linked List.
4. To Display Data in the Doubly Linked List.

1
Enter the data that you want to add to the Doubly Linked List::
12
Data Entered Successfully at the End of the Doubly Linked List.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data at the End of the Doubly Linked List.
2. To Insert Data in the Front of the Doubly Linked List.
3. To Insert Data after the head node in the Doubly Linked List.
4. To Display Data in the Doubly Linked List.

4
Data in the Doubly Linked List is::
12<==>NULL
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data at the End of the Doubly Linked List.
2. To Insert Data in the Front of the Doubly Linked List.
3. To Insert Data after the head node in the Doubly Linked List.
4. To Display Data in the Doubly Linked List.

2
Enter the data that you want to add to the Doubly Linked List::
20
Data Entered Successfully at the Front of the Doubly Linked List.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data at the End of the Doubly Linked List.
2. To Insert Data in the Front of the Doubly Linked List.
3. To Insert Data after the head node in the Doubly Linked List.
4. To Display Data in the Doubly Linked List.

4
Data in the Doubly Linked List is::
20<==>12<==>NULL
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data at the End of the Doubly Linked List.
2. To Insert Data in the Front of the Doubly Linked List.
3. To Insert Data after the head node in the Doubly Linked List.
4. To Display Data in the Doubly Linked List.

3 
Enter the data that you want to add after the head node of the Doubly Linked List::
15
Data Entered Successfully after the head node of the Doubly Linked List.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data at the End of the Doubly Linked List.
2. To Insert Data in the Front of the Doubly Linked List.
3. To Insert Data after the head node in the Doubly Linked List.
4. To Display Data in the Doubly Linked List.

4
Data in the Doubly Linked List is::
20<==>15<==>12<==>NULL
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data at the End of the Doubly Linked List.
2. To Insert Data in the Front of the Doubly Linked List.
3. To Insert Data after the head node in the Doubly Linked List.
4. To Display Data in the Doubly Linked List.

1
Enter the data that you want to add to the Doubly Linked List::
67
Data Entered Successfully at the End of the Doubly Linked List.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data at the End of the Doubly Linked List.
2. To Insert Data in the Front of the Doubly Linked List.
3. To Insert Data after the head node in the Doubly Linked List.
4. To Display Data in the Doubly Linked List.

4
Data in the Doubly Linked List is::
20<==>15<==>12<==>67<==>NULL
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
n

In the code written above first, we inserted value at the end of the Doubly-linked List. Then we added data at the beginning of the Doubly-linked List. Then one data is added after the head node of the Doubly-linked List. And there are different functions that are written like insert_end(), insert_front(), and insert_After() to add data at the end of the Doubly-linked List, to add data at the beginning of the Doubly-linked List, and to add after the head node of the Doubly-linked List respectively.

And the contents of the Doubly-linked List are printed after each insertion operation.

Here in this Doubly-linked List, we can store any amount of data by calling any one of the insert functions written and the size of the Doubly-linked List gets increased automatically at the time of the execution of the program. So, we can say that a Doubly-linked List satisfies all the conditions of a dynamic data structure.

Thus, this article gives us a clear idea about what a dynamic data structure is and what are its advantages over the static data structure. And we have also seen the implementation of one of the dynamic data structures i.e., Doubly-linked List in the C++ programming language.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA