WWW Mygreatlearning Com Blog Data Structures Using Java
WWW Mygreatlearning Com Blog Data Structures Using Java
IT/Sof tware Development Articles Tutorials Interview Questions Free Courses Projects Career Guide
Great Learning Blog IT/Sof tware Development Full Stack Development Data Structures in Java – A Beginners Guide 2023
Career Guide
Table of contents
References
T echnical Guide
Data structures are fundamental to any programming language. The choice of a particular data structure has a
significant impact on the functionality and performance of Java applications, thus it is worthwhile to master data
C++ structures in Java.
Career Guide
This guide will help beginners to understand what is data structures, what is data structures in Java, the types of data
References structures in Java and many more.
T echnical Guide
What is Java?
What are Data Structures?
Java
What are Data Structures in Java?
Career Guide Types of Data Structures in Java
Advantages of Data Structures in java
References
Classif ication of Data Structures
T echnical Guide Data Structures in Java FAQs
What is Java?
Java Programming is a high-level programming language created by sun microsystems. This programming language is
reliable, object-oriented and secure. Java follows the WORA principle, which stands for “Write Once Run Anywhere”.
You can run a java program as many times as you want on a java supported platform after it is compiled.
Array
Linked List
Stack
Queue
Binary Tree
Binary Search Tree
Heap
Hashing
Graph
To learn more about Java Programming, you can take up a free online course offered by Great Learning
Academy and upskill today. If you are already well-versed with the basics, go ahead and enrol yourself in
the Data Structure & Algorithms in Java for Intermediate Level.
Primitive data Structures are also called Primitive Data Types. byte, short, int, float, char, boolean, long, and double
are primitive Data types.
Arrays
Single dimensional Array
Multidimensional Array
St ack
Queue
Linked List
Singly-linked list
Doubly Linked list
Circular Linked List
Non-Linear Data Structures – The elements arranged in a non-linear fashion are called Non-Linear Data Structures.
Here, each element is connected to n-other elements. Non-Linear Data Structures are as follows:
Trees
Binary Tree
Binary Search Tree
AVL Tree
Red-Black Tree
Graph
Heap
MaxHeap
MinHeap
Hash
HashSet
HashMap
St at ic Dat a St ruct ures are the Data structures whose size is declared and f ixed at Compile Time and cannot be changed later
are called Static Data structures.
Example – Arrays
Dynamic Dat a St ruct ures are the Data Structures whose size is not f ixed at compile time and can be decided at runtime
depending upon requirements are called Dynamic Data structures.
Example – Binary Search Tree
Synt ax:
Array declaration
datatype varname []=new datatype[size];
datatype[] varname=new datatype[size];
Array
What is an Array?
An array is the simplest data structure where a collection of similar data elements takes place and each data element
can be accessed directly by only using its index number.
Array Advantages
Random access
Easy sorting and iteration
Replacement of multiple variables
Array Disadvantages
Size is f ixed
Dif f icult to insert and delete
If capacity is more and occupancy less, most of the array gets wasted
Needs contiguous memory to get allocated
Array Applications
import java.util.*;
class JavaDemo {
public static void main (String[] args) {
int[] priceOfPen= new int[5];
Scanner in=new Scanner(System.in);
for(int i=0;i<priceOfPen.length;i++)
priceOfPen[i]=in.nextInt();
for(int i=0;i<priceOfPen.length;i++)
System.out.print(priceOfPen[i]+" ");
}
}
Input:
23 13 56 78 10
Output:
23 13 56 78 10
Linked List
What is Linked List?
Linked list data structure helps the required objects to be arranged in a linear order.
Dynamic in size
No wastage as capacity and size is always equal
Easy insertion and deletion as 1 link manipulation is required
Ef f icient memory allocation
import java.util.*;
class LLNode{
int data;
LLNode next;
LLNode(int data)
{
this.data=data;
this.next=null;
}
}
class Demo{
LLNode head;
if(head==null)
head=ttmp;
else
{
ttmp.next=head;
head=ttmp;
}
return head;
}
LLNode insertInEnd(int key,LLNode head)
{
LLNode ttmp=new LLNode(key);
LLNode ttmp1=head;
if(ttmp1==null)
head=ttmp;
else
{
while(ttmp1.next!=null)
ttmp1=ttmp1.next;
ttmp1.next=ttmp;
return head;
if(pos==1)
{
ttmp.next=head;
head=ttmp;
}
else
{
LLNode ttmp1=head;
for(int i=1;ttmp1!=null && i<pos;i++)
ttmp1=ttmp1.next;
ttmp.next=ttmp1.next;
ttmp1.next=ttmp;
}
return head;
}
prevLNode=curLNode;
curLNode=nextLNode;
}
head=prevLNode;
return head;
}
}
}
Output:
1.Insert In End
2.Insert In Beg
4.Delete At a Pos
5.Length
6.Reverse
7.Display
8.EXIT
enter ur choice :
1
do u want to cont...
1
1.Insert In End
2.Insert In Beg
4.Delete At a Pos
5.Length
6.Reverse
7.Display
8.EXIT
enter ur choice :
1
do u want to cont...
1
2.Insert In Beg
4.Delete At a Pos
5.Length
6.Reverse
7.Display
8.EXIT
enter ur choice :
2
do u want to cont...
1
1.Insert In End
2.Insert In Beg
4.Delete At a Pos
5.Length
6.Reverse
7.Display
8.EXIT
enter ur choice :
7
10 23 56
do u want to cont...
1
1.Insert In End
2.Insert In Beg
4.Delete At a Pos
5.Length
6.Reverse
7.Display
8.EXIT
enter ur choice :
3
do u want to cont...
1
1.Insert In End
2.Insert In Beg
4.Delete At a Pos
5.Length
6.Reverse
7.Display
8.EXIT
enter ur choice :
7
10 23 67 56
do u want to cont...
1
1.Insert In End
2.Insert In Beg
3.Insert At A Particular Pos
4.Delete At a Pos
5.Length
6.Reverse
7.Display
8.EXIT
enter ur choice :
4
2
do u want to cont...
1
1.Insert In End
2.Insert In Beg
4.Delete At a Pos
5.Length
6.Reverse
7.Display
8.EXIT
enter ur choice :
7
10 67 56
do u want to cont...
1
1.Insert In End
2.Insert In Beg
4.Delete At a Pos
5.Length
6.Reverse
7.Display
8.EXIT
enter ur choice :
6
do u want to cont...
1
1.Insert In End
2.Insert In Beg
4.Delete At a Pos
5.Length
6.Reverse
7.Display
8.EXIT
enter ur choice :
7
56 67 10
do u want to cont...
Stack
What is a stack?
A stack is a representation of nodes. There are two components to each node: data and next (storing address of next
node). Each node’s data portion contains the assigned value, and its next pointer directs the user to the node that has
the stack’s subsequent item. The highest node in the stack is referred to as the top.
Features of Stack
Stack Advantages
Stack Disadvantages
Stack Applications
Recursion
Parsing
Browser
Editors
import java.util.*;
class Stack
{
int[] a;
int top;
Stack()
{
a=new int[100];
top=-1;
}
void push(int x)
{
if(top==a.length-1)
System.out.println("overflow");
else
a[++top]=x;
}
int pop()
{
if(top==-1)
{System.out.println("underflow");
return -1;
}
else
return(a[top--]);
}
void display()
{
for(int i=0;i<=top;i++)
System.out.print(a[i]+" ");
System.out.println();
}
boolean isEmpty()
{
if(top==-1)
return true;
else
return false;
}
int peek()
{
if(top==-1)
return -1;
return (a[top]);
}
}
public class Demo
{
public static void main(String args[])
{
do
{System.out.println("\n******** MENU *******");
System.out.println("\n1.PUSH");
System.out.println("\n2.POP");
System.out.println("\n3.PEEK");
System.out.println("\n4 IS EMPTY");
System.out.println("\n5.EXIT");
System.out.println("\n enter ur choice : ");
switch(in.nextInt())
{
case 1:
System.out.println("\nenter the value ");
s.push(in.nextInt());
break;
case 2:
System.out.println("\n popped element : "+ s.pop());
break;
case 3:
System.out.println("\n top element : "+ s.peek());
break;
case 4: System.out.println("\n is empty : "+ s.isEmpty());
break;
case 5: System.exit(0);
break;
default: System.out.println("\n Wrong Choice!");
break;
}
System.out.println("\n do u want to cont... ");
}while(in.nextInt()==1);
}
}
Output:
1.PUSH
2.POP
3.PEEK
4 IS EMPTY
5.EXIT
enter ur choice :
1
do u want to cont...
1
1.PUSH
2.POP
3.PEEK
4 IS EMPTY
5.EXIT
enter ur choice :
1
do u want to cont...
1
1.PUSH
2.POP
3.PEEK
4 IS EMPTY
5.EXIT
enter ur choice :
2
popped element : 56
do u want to cont...
1
1.PUSH
2.POP
3.PEEK
4 IS EMPTY
5.EXIT
enter ur choice :
4
is empty : false
do u want to cont...
1
1.PUSH
2.POP
3.PEEK
4 IS EMPTY
5.EXIT
enter ur choice :
2
popped element : 12
do u want to cont...
import java.util.*;
class LNode
{
int data;
LNode next;
LNode(int d)
{
data=d;
}
class Stack
{
LNode push(int d,LNode head){
if(head==null)
head=tmp1;
else
{
tmp1.next=head;
head=tmp1;
}
return head;
}
if(head==null)
System.out.println("underflow");
else
head=head.next;
return head;
}
System.out.println("no LNodes");
return;
}
LNode tmp=head;
while(tmp!=null){
System.out.print(tmp.data+" ");
tmp=tmp.next;
}
}
do
{System.out.println("\n******** MENU *******");
System.out.println("\n1.PUSH");
System.out.println("\n2.POP");
System.out.println("\n3.PEEK");
System.out.println("\n4 IS EMPTY");
System.out.println("\n5 DISPLAY");
System.out.println("\n6.EXIT");
System.out.println("\n enter ur choice : ");
switch(in.nextInt())
{
case 1:
System.out.println("\nenter the value ");
head=s.push(in.nextInt(),head);
break;
case 2:
head=s.pop(head);
break;
case 3:
System.out.println("\n top element : "+ s.peek(head));
break;
case 4:
System.out.println("\n is empty : "+ s.isEmpty(head));
break;
case 5: s.display(head);
break;
case 6: System.exit(0);
break;
default: System.out.println("\n Wrong Choice!");
break;
}
System.out.println("\n do u want to cont... ");
}while(in.nextInt()==1);
}
}
Output
******** MENU *******
1.PUSH
2.POP
3.PEEK
4 IS EMPTY
5 DISPLAY
6.EXIT
enter ur choice :
1
do u want to cont...
1
1.PUSH
2.POP
3.PEEK
4 IS EMPTY
5 DISPLAY
6.EXIT
enter ur choice :
1
do u want to cont...
1
1.PUSH
2.POP
3.PEEK
4 IS EMPTY
5 DISPLAY
6.EXIT
enter ur choice :
5
list is :
56 12
do u want to cont...
1
1.PUSH
2.POP
3.PEEK
4 IS EMPTY
5 DISPLAY
6.EXIT
enter ur choice :
3
top element : 56
do u want to cont...
1
1.PUSH
2.POP
3.PEEK
4 IS EMPTY
5 DISPLAY
6.EXIT
enter ur choice :
4
is empty : false
do u want to cont...
1
Queue
What is Queue?
The queue is called an abstract data structure. Data is always added to one end (enqueued), and removed from the
other (dequeue). Queue uses the First-In-First-Out approach and data item that was stored initially will be accessed
first in a queue.
Features of Queue
Queue Advantages
Queue Applications
Scheduling
Maintaining playlist
Interrupt handling
import java.util.*;
class Queue{
int front;
int rear;
int[] arr;
Queue()
{
front=rear=-1;
arr=new int[10];
}
void enqueue(int a)
{
if(rear==arr.length-1)
System.out.println("overflow");
else
arr[++rear]=a;
if(front==-1)
front++;
}
int dequeue()
{
int x=-1;
if(front==-1)
System.out.println("underflow");
else
x=arr[front++];
if(rear==0)
rear--;
return x;
}
void display()
{
for(int i=front;i<=rear;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
1 2 3 4 5
2 3 4 5
class LNode{
int data;
LNode next;
LNode(int d)
{
data=d;
}
}
class Queue{
tmp1.next=tmp;
}
return head;
}
System.out.println("no LNodes");
return;
}
LNode tmp=head;
while(tmp!=null){
System.out.print(tmp.data+" ");
tmp=tmp.next;
}
}
head=ob.enqueue(head,1);
head=ob.enqueue(head,2);
head=ob.enqueue(head,3);
head=ob.enqueue(head,4);
head=ob.enqueue(head,5);
ob.display(head);
head=ob.dequeue(head);
ob.display(head);
}
}
Output
list is :
1 2 3 4 5
list is :
2 3 4 5
Binary Tree
What is a Binary Tree?
In a binary tree, the branches of the tree are made up of up to two child nodes for each node. The left and right nodes
are the common names for the two youngsters. Child nodes make references to their parents, whereas parent nodes
are nodes with children.
class TLNode
{
int data;
TLNode left,right;
TLNode(int d)
{
data=d;
}
}
System.out.print(r.data+" ");
preorder(r.left);
preorder(r.right);
}
static void inorder(TLNode r)
{
if(r==null)
return;
inorder(r.left);
System.out.print(r.data+" ");
inorder(r.right);
}
static void postorder(TLNode r)
{
if(r==null)
return;
postorder(r.left);
postorder(r.right);
System.out.print(r.data+" ");
root.left.left=new TLNode(4);
root.left.right=new TLNode(5);
root.right.left=new TLNode(6);
root.right.right=new TLNode(7);
preorder(root);
System.out.println();
inorder(root);
System.out.println();
postorder(root);
System.out.println();
}
}
Output
1 2 4 5 3 6 7
4 2 5 1 6 3 7
4 5 2 6 7 3 1
The binary search tree is an advanced algorithm which is used to analyse the nodes, branches and many more. The
BST was developed using the architecture of a fundamental binary search algorithm, allowing for quicker node lookups,
insertions, and removals.
class TLNode{
int data;
TLNode left,right;
TLNode(int d)
{
data=d;
}
}
TLNode root;
else if(d<=root.data)
root.left=insert(d,root.left);
else
root.right=insert(d,root.right);
return root;
}
void inorder(TLNode r)
{
if(r==null)
return;
inorder(r.left);
System.out.println(r.data);
inorder(r.right);
else
{
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
root.data = minValue(root.right);
return root;
}
int minValue(TLNode root)
{
int minv = root.data;
while (root.left != null)
{
minv = root.left.data;
root = root.left;
}
return minv;
}
TLNode find=ob.search(30,ob.root);
if(find==null)
System.out.println("not found");
else
System.out.println("found : "+find.data);
}
}
Output:
******60
20
20
30
60
70
80
found : 30
Heap
Binary Heap can be visualized array as a complete binary tree
Arr[0] element will be treated as root
length(A) – size of array
heapSize(A) – size of heap
Generally used when we are dealing with minimum and maximum elements
For ith node
(i-1)/2 Parent
Heap Advantages
Heap Disadvantages
Heap Applications
import java.util.*;
class Heap{
int heapSize;
void build_max_heap(int[] a)
{
heapSize=a.length;
for(int i=(heapSize/2);i>=0;i--)
max_heapify(a,i);