# What Is Data Structure and Its Type With Example?

//

Larry Thompson

What Is Data Structure and Its Type With Example?

Data structure refers to the way data is organized, stored, and manipulated in a computer system. It plays a vital role in optimizing the efficiency and performance of algorithms. In this article, we will explore different types of data structures along with examples.

## Types of Data Structures

There are several types of data structures available. Let’s take a closer look at some commonly used ones:

### 1. Arrays

An array is a collection of elements of the same type that are stored in contiguous memory locations. It provides fast access to elements based on their index. For example, consider an array of integers:

``` int[] numbers = {1, 2, 3, 4, 5}; ```

Here, accessing the third element can be done using numbers[2], where the index starts from 0.

A linked list is a linear data structure consisting of nodes where each node contains data and a reference to the next node. Unlike arrays, linked lists provide dynamic memory allocation and efficient insertion or deletion at any position.

Consider a singly linked list containing strings:

``` class Node { String data; Node next; }```

``` ```

```Node head = new Node(); head.data = "Hello"; Node second = new Node(); second.data = "World"; head.next = second; ```

In this example, the first node’s data is “Hello” and it points to the second node which holds the value “World”.

### 3. Stacks

A stack follows the Last-In-First-Out (LIFO) principle, where elements are inserted and removed from one end called the top. It supports two main operations: push (insert) and pop (remove).

Here’s an example of a stack that stores characters:

``` Stack<Character> stack = new Stack<>(); stack.push('a'); stack.push('b'); stack.push('c');```

``` ```

```while (!stack.isEmpty()) { System.out.println(stack.pop()); } ```

The output will be:
c
b
a

### 4. Queues

A queue follows the First-In-First-Out (FIFO) principle, where elements are added at one end called the rear and removed from the other end called the front. It supports two main operations: enqueue (insert) and dequeue (remove).

Consider a queue of integers:

``` Queue<Integer> queue = new LinkedList<>(); queue.enqueue(10); queue.enqueue(20); queue.enqueue(30);```

``` ```

```while (!queue.println(queue.dequeue()); } ```

The output will be:
10
20
30

### 5. Trees

A tree is a hierarchical data structure consisting of nodes connected by edges. It has a root node and can have zero or more child nodes. Trees are commonly used for representing hierarchical relationships.

Here’s an example of a binary tree:

``` class Node { int data; Node left; Node right; }```

``` Node root = new Node(); root.data = 1; Node leftChild = new Node(); leftChild.data = 2; Node rightChild = new Node(); rightChild.data = 3; ```

```root.left = leftChild; root.right = rightChild; ```

In this example, the root node has two child nodes with values 2 and 3.

## In Conclusion

Data structures play an essential role in computer science and programming. Understanding the different types of data structures, their characteristics, and how to use them is crucial for writing efficient algorithms.

In this article, we explored arrays, linked lists, stacks, queues, and trees along with their examples. Remember to choose the appropriate data structure based on the specific requirements of your program to achieve optimal performance.