What Is Data Structure? **Explain Different Types of Data Structures With Examples**

Data structures are essential elements in computer science and programming. They allow us to store and organize data efficiently, enabling quick access and manipulation. Understanding different types of data structures is crucial for designing efficient algorithms and writing optimized code.

## Types of Data Structures

### 1. Arrays

An array is a collection of elements of the same data type that are stored in contiguous memory locations.

It provides direct access to individual elements using an index. Here’s an example:

```
<pre>
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[0]); // Output: 1
</pre>
```

### 2. Linked Lists

A linked list consists of nodes where each node contains data and a reference to the next node.

Unlike arrays, linked lists can dynamically grow or shrink during program execution. Here’s an example:

```
<pre>
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
</pre>
```

### 3. Stacks

A stack is a Last-In-First-Out (LIFO) data structure that allows adding and removing elements only from the top.

It follows the “push” (add) and “pop” (remove) operations. Here’s an example:

```
<pre>
import java.util.Stack;
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.pop(); // Output: 2
</pre>
```

### 4. Queues

A queue is a First-In-First-Out (FIFO) data structure that allows adding elements at the rear and removing elements from the front.

It follows the “enqueue” (add) and “dequeue” (remove) operations.Queue;

import java.LinkedList;

Queue<Integer> queue = new LinkedList<>();

queue.add(1);

queue.add(2);

queue.remove(); // Output: 1

</pre>

### 5. Trees

A tree is a hierarchical data structure consisting of nodes, where each node can have children.

It has a root node and branches out to form subtrees. Here’s an example of a binary tree:

```
<pre>
class Node {
int data;
Node left, right;
public Node(int data) {
this.left = null;
this.right = null;
}
}
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
</pre>
```

### 6. Graphs

A graph is a collection of nodes connected by edges.

It represents relationships between objects or entities. Graphs can be directed or undirected, weighted or unweighted, cyclic or acyclic.ArrayList;

import java.List;

class Graph {

int v;

List<List<Integer>> adjacencyList;

public Graph(int v) {

this.v = v;

adjacencyList = new ArrayList<>(v);

for (int i = 0; i < v; ++i)

adjacencyList.add(new ArrayList<>());

}

void addEdge(int src, int dest) {

adjacencyList.get(src).add(dest);

adjacencyList.get(dest).add(src);

}

}

Graph graph = new Graph(5);

graph.addEdge(0, 1);

graph.addEdge(1, 2);

</pre>

## Conclusion

Data structures play a vital role in computer science and programming. They provide efficient ways to store, organize, and manipulate data. Understanding different types of data structures and their applications is essential for writing efficient code and designing optimized algorithms.

By incorporating these data structures into your programs, you can enhance their performance and efficiency. So make sure to choose the right data structure based on the requirements of your application.