# Java Data Structures – Linear Data Structures in Java

## 1. Java Data Structure – Objective

In this Java Data Structure tutorial, we are going to learn about Linear Data Structures in **Java Programming Language** and a list of Data Structures in Java. Here, we are going to study the linear Data structure in Java: Java Array, Java Linked List, Java Stack, and Java Queue. At last, we will see the examples of Linear Data structures in Java.

So, let’s start Linear Data Structure in Java Programming.

## 2. What are Linear Data Structures in Java?

Linear data structures in Java is a way to organize the data in the language in a particular way so to use them in the most effective way. The main reason to classify them is that we need less complexity and less space. The main linear data structures in Java are:

- Java Array
- Linked List
- Stack
- Queue

**Do you know about Packages in Java Programming Language**

## 3. Java Array

**An array** is a linear data structures in Java, is to store data of the type homogenous at a contiguous place, size for the array is to define beforehand.

Let the size of Java array be n.

**Accessing Time:**O(1) [This is possible because it stores elements at contiguous locations]**Search Time:**O(n) for Sequential Search: O(log n) for Binary Search [If Array is sorted]**Insertion Time:**O(n) [The worst case occurs when insertion happens at the Beginning of an array and requires shifting all of the elements]**Deletion Time:**O(n) [This is the worst case occurs when deletion occurs at the starting of an array and requires shifting all of the elements]

**Java Array Example **

For storing the marks of a student we can create an array as this saves us from using a different variable for every subject, we can simply add data by traversing the array.

**Read about** **Java Access Modifiers** & **Loops in Java **in detail

## 4. Java Linked List

A linked list is linear data structures in Java, is similar to Java array with the only difference that here every element (we call it a ‘node’) is treated as a separate object, every node has two parts, one the data and the reference to the next node.

Types of Java linked list in Data Structure –

### a. Singly linked list

Singly linked list stores data and the reference to the next node or a null value.

**For example –** 1>2>3>NULL

**Do you know How Java Inheritance is used?**

### b. Doubly linked list

It is the same as a double Java linked list with the difference that it has two references, one for the last node and one for the previous node. This helps us traverse in both the directions and also we don’t need explicit permission for deletion of nodes.

**Example –** NULL<-1<->2<->3->NULL

### c. Circular Linked List

In this type of Java Linked List, all the nodes align to form a circle and also there is no NULL at the end, it helps us to define any node as first and also helps to implement a circular queue.

**Example –** 1->2->3->1

**Accessing time of an element:**O(n)**Search time of an element:**O(n)**Insertion of an Element:**O(1) [If we are at the position where we have to insert an element]**Deletion of an Element:**O(1) [If we know the address of node previous the node to be deleted]

**Example –**

Consider the last example (Circular Linked List) where we need to add the marks of the students and a new subject is added to the list, we cannot add this to array as the size is already fixed and if we make an array which is larger than there will be empty spaces left, so to overcome this drawback we use linked lists.

- The only drawback is that we cannot randomly access the nodes which were quite easy in arrays.

Let’s study **Java String** & **Java Regular Expressions **in detail

## 5. Java Stack

Java Stack, is one of the linear data structures in Java runs on the principal* Last In First Out* (LIFO), it allows the user to have two operations, viz., Push and Pop, Push allows us to add elements while Pop allows removing the last element. Both operations can take place at the same end.

**Insertion:**O(1)**Deletion:**O(1)**Access Time:**O(n) [Worst Case]- It allows Insertion and Deletion on only one end.

**Example –** Stacks in Java are utilized for keeping up work calls (the last called work must complete execution first), we can simply evacuate recursion with the assistance of stacks. Stack data Structures in Java is additionally utilized as a part of situations where we need to turn around a word, check for an adjusted bracket and in editors where the word you wrote the latter is the first to evacuate when you utilize fix activity. Correspondingly, to execute back usefulness in web programs.

Let’s look at **Java Methods- Declaration and Calling** & **Inheritance in Java** in detail

## 6. Java Queue

A Queue is a last linear data structures in Java, offers the option First In, First Out (FIFO), which helps us to save a collection of data, it is an abstract data type. It provides two major options enqueue, the way toward adding a component to the collection. The component is included from the back side and dequeue, the way toward expelling the principal component that was included. The component is expelled from the front side. It can actualize by utilizing both exhibit and connected rundown.

**Insertion:**O(1)**Deletion:**O(1)**Access Time:**O(n) [Worst Case]

**Example –** Queue in Java as the name says is the information structure worked by the lines of transport stop or prepare where the individual who is remaining in the front of the queue(standing for a very long time) is the first to get the ticket. So any circumstance where assets are shared among various clients and served on first start things out server premise. Cases incorporate CPU planning, Disk Scheduling. Another utilization of queue is when information is exchanged non-concurrently (information did not really get at the same rate as sent) between two procedures. Illustrations incorporate IO Buffers, channels, document IO, and so on.

### a. Circular Queue

The upside of this Linear data structures in Java is that it diminishes wastage of room in the event of cluster execution, as the inclusion of the (n+1)’th component is done at the 0’th record on the off chance that it is vacant.

So, this was all about Linear Data Structure in Java Tutorial. Hope you like our explanation.

## 7. Conclusion

In this Java tutorial, we learned about the data structures in Java, its first type, i.e., the linear Data Structures in Java: Array in data structure, Java Array Example, Java linked list in Data Structure, Types of Java linked lists in Data Structure, Singly linked list, doubly linked list, Circular Linked List, Stack Data Structure with some examples of Linear Data structures. Furthermore, if you have any query, feel free to ask in the comment section.

In the next tutorial, we will learn about the **Hierarchical Data Structure in Java**.

Hello,

I found your article very useful and the tools you talked about were unique.

thanks for posting..

Hello, Kiran

Thank you for showing interest in our “Java Data Structure” Tutorial. We hope you refer all relevant link which is in this Java tutorial, feel free to share this blogs with your peer groups.

Regards,

Data-Flair