In computer science, data structures are used to organize and store data in a way that allows for efficient retrieval and manipulation. There are two main types of data structures: linear and non-linear. Let’s explore these two types and provide examples of each.

## Linear Data Structures

Linear data structures are those in which the elements are arranged in a sequential manner. Each element has a unique predecessor and successor, except for the first and last elements. The most common examples of linear data structures include arrays, linked lists, stacks, and queues.

### Arrays

An array is a fixed-size collection of elements of the same type. Elements in an array are stored sequentially in memory, allowing for constant-time access to any element using its index. For example:

`<p>`**int** numbers[] = {1, 2, 3, 4, 5};

```
```

`</p>`

### Linked Lists

A linked list is a dynamic data structure where each element (node) contains a value and a reference to the next node. Unlike arrays, linked lists can grow or shrink dynamically at runtime. For example:

`<p>class Node {`

**int** value;

Node next;

}

Node head = **new** Node();

head.value = 1;

Node second = **new** Node();

second.value = 2;

head.next = second;

```
```

`</p>`

### Stacks

A stack is an abstract data type that follows the Last-In-First-Out (LIFO) principle. Elements can only be inserted or removed from the top of the stack.

The push operation adds an element to the top, while the pop operation removes the topmost element. For example:

`<p>Stack<Integer> stack = `**new** Stack<>();

stack.push(1);

stack.push(2);

stack.push(3);

```
```

`</p>`

### Queues

A queue is an abstract data type that follows the First-In-First-Out (FIFO) principle. Elements can only be inserted at one end (rear) and removed from the other end (front).

The enqueue operation adds an element to the rear, while dequeue removes an element from the front. For example:

`<p>Queue<String> queue = `**new** LinkedList<>();

queue.enqueue("John");

queue.enqueue("Jane");

queue.enqueue("Alice");

```
```

`</p>`

## Non-Linear Data Structures

Non-linear data structures are those in which elements are not arranged in a sequential manner. Each element may have multiple predecessors and successors, creating complex relationships between elements. The most common examples of non-linear data structures include trees and graphs.

### Trees

A tree is a hierarchical data structure consisting of nodes connected by edges. It starts with a root node and each node can have zero or more child nodes.

Trees are often used to represent hierarchical relationships, such as file systems or organization charts. For example:

`<p>class Node {`

**int** value;

List<Node> children;

}

Node root = **new** Node();

root.value = 1;

Node child1 = **new** Node();

child1.value = 2;

Node child2 = **new** Node();

child2.value = 3;

root.children.add(child1);

root.add(child2);

```
```

`</p>`

### Graphs

A graph is a collection of nodes (vertices) connected by edges. Unlike trees, graphs can have cycles and multiple connections between nodes.

Graphs are widely used to represent complex relationships, such as social networks or transportation networks. For example:

`<p>class Node {`

**int** value;

List<Node> neighbors;

}

Node node1 = **new** Node();

node1.value = 1;

Node node2 = **new** Node();

node2.value = 2;

node1.neighbors.add(node2);

node2.add(node1);

```
```

`</p>`

In conclusion, linear and non-linear data structures serve different purposes and are used in various applications. Understanding the characteristics and examples of these data structures is crucial for designing efficient algorithms and solving real-world problems.