DATA STRUCTURES:
a technique used to represent data in an organized manner.
WHY USE DATA STRUCTURES:
Using data structure one can retrieve and process data easily.
TYPES OF DATA STRUCTURES:
Mainly data structures are divided into two broad categories called primitive (build-in data structures) and non-primitive data structures (user-defined data structures).
Later non-primitive data structures are divided as shown in the below chart.
Let’s discuss them one by one.
1. PRIMITIVE/ BUILT-IN DATA STRUCTURES:
- Primitive data structures are built-in for a programming language.
- Primitive data types always have value.
- They start from lowercase letters.
1.1 INTEGER: integer stores 4 bytes of memory.
int a=10;
Where a is an integer type.
1.2 FLOAT: float variable also stores 4 bytes in memory.
int b=1.5;
Where b is the name of a variable that stores decimal digits.
1.3 CHAR: character stores 2 bytes of memory.
Char c=’A’;
it stores character value in it.
1.4 BOOLEAN: it occupies 1 bit of memory.
boolean value=true;
It stores true or false value.
1.5 STRING :
String data type stores sequence of character.
String s = “hello”;
2. NON-PRIMITIVE/ USER DEFINED DATA STRUCTURES:
- Non-primitive data types are not built-in. Users define the data structure.
- They start from uppercase letters.
- Its value can be null.
2.1 LINEAR DATA STRUCTURES:
data stored next to each other.
2.1.1 STATIC:
memory is fixed while writing a code.
2.1.1.1 ARRAYS:
Data Structure which stores the same data type value is called an array.
No. of elements/size of array = n.
indexing = 0 to n-1;
Array data structures are of two types: 1D and 2D.
Syntax of 1D array in java :
int[] arr=new int[sizeOfArray]; //1D array of integer type.
E.g. – int [] arr=new int[5];
Indexing starts from 0 to 4.
Size of array = 5;
Array arr stores integer type of value in it.
Index : 0 1 2 3 4
3 | 40 | 42 | 9 | 22 |
Syntax of 2D array in java :
int[][] arr2=new int[row][col]; //2D array of integer type.
All elements of an array share the same variable name but different index values.
E.g – int[][] arr2=new int[2][2];
Col 1 = 0 col 2= 1 col 3= 2
row=1 43 | 5 | 32 |
row=2 51 | 41 | 3 |
row=3 4 | 6 | 18 |
An array is static which means the size cannot be increased and decreased during runtime.
Disadvantage of arrays:
the size of the array is fixed.
2.1.2 DYNAMIC:
memory allocation occurs at the time of runtime.
2.1.2.1 LINKED LIST:
- it’s smallest unit is a node.
- A linked list is a collection of nodes.
- Each node connects to the other and forms a chain.
- Each node has data and a pointer that stores the address of the next node.
Syntax of linked list :
LinkedList<Integer> list = new LinkedList<>();
Diagramatic representation of a node :
Data value | pointer |
Disadvantages of linked list :
- A reverse linked list is really difficult.
- If we want to access an element at index n, then we need to traverse all the elements in a linked list.
- Memory usage is more as compared to an array as it needs pointer memory as well.
2.1.2.2 STACK :
- Stack data structure is similar to a stack of plates.
- It follows the LIFO principle which means the last element added in the stack will be first to remove.
Diagramatic representation of stack :
Fig 2: stack diagrammatic representation.
Functions of stack :
1) push() : use to add elements from stack.
2) pop() : this function is used to remove elements from the stack.
3) isEmpty() : function return true if stack is empty else it will return false.
2.1.2.3 QUEUE:
This data structure is like a real queue in front of the billing counter. A queue is based on the FIFO principle. The first entered element will operate first.
Diagrammatic Representation Of Queue
Functions of queue :
1) enque() : use to add elements from queue.
2) dequeue() : this function is used to remove elements from the queue.
3) isEmpty() : function return true if queue is empty else false.
2.2 NON-LINEAR DATA STRUCTURES:
data can be separated by each other in memory space.
2.2.1 TREE:
- Data structures each node is associated with more than one node.
- Tree data structures follow hierarchical patterns.
Diagram of the tree :
- Where a node with no child is called a leaf node.
- Where a node with a child is called a parent node.
- Two nodes share the same parent and have the same level and are known as siblings of each other.
- There are different types of data structures of a tree – binary tree, binary search tree, AVL tree, B tree, B+ tree.
2.2.2 GRAPH:
the graph data structure is non-linear consist of vertex and edges.
Diagram of the graph :
Where 0,1,2,3,4 are vertices of the graph.(0,1) , (0,4) , (1,3) , (1,2) , (1,4) , (3,1) , (3,4) , (3,2) , (2,1) , (2,3) are edges in the shown graph.Graphs are of two types: unidirectional edge and directional edge.
CONCLUSION :
The size of the array is fixed; we cannot change its size during run time.
This disadvantage of java is overcome by a linked list but LinkedList requires more space than an array as it has one additional memory space for pointers.
for non-sequential data structure then graphs and trees are best.
written by: Yatika Yadav
reviewed by: Soutik Maity
If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs