Data Structures are different ways how we organize collection of data in comupter memory for faster/efficient/easier access to a perticular data in that collection. Data structure goes hand in hand with algorithm, at first we organize collection of data in a specific way, which we call a Data Structure and then we write some instructions/steps to how we can access/modify/insert/delete individual elements in that Data Structure, which we call an algorithm for that Data Structure.
The most basic data structure is a fixed size array. An array stores all of its elements next to each other, meaning if an array holds all of its data in memory range of 0-15 and it stores int type values then each element will occupy 4 bytes. So, when data is inserted it will put the first int in memory 0-3, next will be put in 4-7, next is 8-11, next is 12-15. So, basically to in an int array the data at i index will be at the memory position of i*4
and it will occupy the range i*4
-(i*4)+3
. And there are many algorthims to search, insert, remove elements in side this data structure (i. e. array).
In other words, A data structure is a guideline for storing collection of data and an algorithm is for searching, inserting, modifying, removing a specific data within that data structure.
DS | Description |
---|---|
Array | Simple structure that stores data linearly in memory |
Linked List | Each element or node occupies memory on demand while keeping a pointer to its previous or next element/node |
Stack | Restricts insertion and deletion of data, as only the topmost index can be removed or be inserted at (i. e. LIFO) |
Queue | Restricts insertion to be only at the end of list and deletion to be only at the beginning of the list (i. e. FIFO) |
B-Tree | Each node stores 2 related child nodes, left stores the smaller value and right stores the bigger value, no child node points back to its parent |
M-Tree | Each node can store M amount of child nodes, no child node points back to its parent |
Graph | Each node can keep pointers to any other nodes |