Arrays Linked List
Arrays Linked List
PrepBytes Blog
ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING
Log In
Language
o
o
o
o
o
Data Structure
o
o
o
o
o
o
o
o
Algorithm
o
o
o
o
o
o
CSE Subjects
o
Company Placement
Interview
o
o
o
o
o
o
Competitive
o
o
o
Others
o
o
o
o
o
o
o
DATA STRUCTURE
Arrays in Java
Array: An array is a linear, contiguous collection of the same data type. Let us
understand this with the help of the diagram shown below.
So, as you can see in the image above, we have an array of lengths 5. All the
elements in this array are integers (same data type).
Now, what do we mean by contiguous memory? So, let us say that the
address of the first block of the array is 4k. This is called the base address of
the array.
So, in the above code, the line: int[] arr; declares a new array with the name
arr. In the memory, the stack section of the memory holds the reference
variable where null will be stored as the reference arr does not hold any valid
address currently.
Java
This creates an array of length 5 in the heap memory. Since we have not filed
any values in the array yet, the array will be filled with all the zeroes.
The above image shows that first the array was declared and the reference
variable had null stored in it. Then, the array was initialised, and the reference
variable stores the base address of the array. Since we did not fill the array,
currently all the values are initialised to 0.
Direct Access
A value in an array can be accessed directly using the valid index. This means
that the index should not be less than 0 or should not be greater than or equal
to the length of the array.
Java
In order to traverse the array, we can use the length property of the array in
Java.
The indices range from 0 to arr.length -1. This is the case for every array.
The indices will start from 0 and the last index will be the length of the array -
1.
So, using the length property of an array, let us now write a program to input
and display an array.
Java
So, an array has all the indices starting from 0 and hence it differs from a
matrix a little bit. Let us understand the 2-D array memory mapping.
Java
Java
This is what happens in the memory when we declare and initialise an array.
So, an array of references is created and it stores the address to the 1-d
arrays. The reference variable arr stores the base address of the reference
array.
We can directly access any value in a 2-D Array. Consider the following
program shown below.
Java
The time complexity for directly accessing a value in a 2-D array is O(1) i.e.
constant.
The length property can be used in the 2-D arrays also. Consider the following
2-D array
Here, arr.length gives us the number of rows and arr[0].length gives us the
number of elements in the array present at arr[0] i.e. the number of columns in
the 0th row
Java
Let us now introduce you to the next data structure i.e. Linked List.
Test your data structure skills by taking this Data Structures in Java Mock
Test designed by experienced mentors at PrepBytes.
This means that they can be present in any order in the actual memory. This
is shown in the image given below.
The first node of a linked list is called the head of the linked list.
Direct access:
Direct access to values is not possible on Linked List. We have to traverse the
list to get the ith value.
The basic unit of the structure of the linked list shown in the above diagram is
called a Node. A node of a linked list contains its data, and a pointer or a
reference to the next node.
A linked list is an inbuilt data structure provided by Java. So, we can use the
inbuilt linked list or we may create our own linked list. Here, we will learn how
to use the inbuilt Linked List data structure of Java.
Doing this creates an empty linked list i.e the head of the linked list will be null
and the reference variable list in the program will also have null stored in it.
This method adds the element to the end of the linked list. So, let us see how
we can add an element to the last of the linked list and create our Linked List
using it.
Java
The image below shows how the linked list grew in the above code.
The time complexity of adding an element to the last of the linked list is O(N).
Add First
The add first operation adds an element to the start of the linked list.
For instance, consider the following program.
Java
The following program shows the syntax of using the methods add(), get(),
and size() on the linked list.
Java
add(): This method adds an element to a particular index in the linked list. The
index values are the same as that of the array i.e. from 0 to size -1. So, the
time complexity of this method is O(N) as in the worst case, we might have to
add it at the end of the linked list.
get(): This method is used to get the value at the ith index in the linked list.
The time complexity is O(N).
size(): This method returns the number of nodes in the linked list. The time
complexity of the size() method is O(1).
So, with this, we have got a basic idea of the linked list data structure. Let us
now learn about the Stack data structure.
Stack in Java
Stack is also a linear data structure that has a LIFO behaviour. LIFO stands
for Last in First out. This means that the last inserted element will be removed
first from the stack.
push(): So, inserting an element into the stack is called a push() operation. It
is an O(1) operation as we just have to insert an element at the top of the
stack.
pop(): Removing an element from the stack is called a pop() operation. So, if
we pop from the above stack, the topmost element of the stack gets removed.
The time complexity is O(1).
size(): This method returns the number of elements in the stack. Its time
complexity is O(1).
We tried to discuss Data Structures in Java in this article. We hope this article
gives you a better understanding of the basics of Data Structures in
Java. Prepbytes also provides a good collection of Foundation Courses that
can help you enhance your coding skills. Want to make sure you ace the
interview in one go? Join our Placement Program that will help you get
prepared and land your dream job at MNCs. Mentors of Prepbytes are highly
experienced and can provide you with basic, in-depth subject knowledge for
better understanding.
Post navigation
Previous
Previous post:
How To Implement Queue Using Doubly Linked List In C
Next
Next post:
Data Structures in Java | Queue | Heap
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Post Comment
Search
Search for:
Pages
ALGORITHMS
ARRAY
BACKTRACKING
C PROGRAMMING LANGUAGE
CAPGEMINI
COMPETITIVE CODING
COMPUTATIONAL GEOMETRY
CSE SUBJECTS
DATA STRUCTURE
DYNAMIC PROGRAMMING
GAME THEORY
GRAPHS
GREEDY ALGORITHM
HASHING
HEAP
INTERVIEW PREPARATION
INTERVIEW TIPS
Languages
LINKED LIST
MATHEMATICS
OPERATING SYSTEM
POINTERS
QUEUE
RECURSION
SEARCHING
SEGMENT TREE
SORTING
STACK
STRING
TREES
Recent Articles
1
2
3
4
5
6
Language
Data Structure
Algorithm
CSE Subjects
Company Placement
Interview
Competitive
Others
Related Post
Dijkstra’s algorithm
AUGUST 25, 2023
2D vector C++
JUNE 29, 2023
FOLLOW US
CONTACT US
+91-7969002111
[email protected]
QUICK LINKS
Interview NotesMock TestsPlacement ProgrammeCoding CoursesMock InterviewAbout
UsBlog
package com.java2novice.ds.stack;
/**
* constructor to create stack with size
* @param size
*/
public MyDynamicStack(int size) {
this.stackSize = size;
this.stackArr = new int[stackSize];
this.top = -1;
}
/**
* This method adds new entry to the top
* of the stack
* @param entry
* @throws Exception
*/
public void push(int entry){
if(this.isStackFull()){
System.out.println(("Stack is full. Increasing the capacity."));
this.increaseStackCapacity();
}
System.out.println("Adding: "+entry);
this.stackArr[++top] = entry;
}
/**
* This method removes an entry from the
* top of the stack.
* @return
* @throws Exception
*/
public int pop() throws Exception {
if(this.isStackEmpty()){
throw new Exception("Stack is empty. Can not remove element.");
}
int entry = this.stackArr[top--];
System.out.println("Removed entry: "+entry);
return entry;
}
/**
* This method returns top of the stack
* without removing it.
* @return
*/
public long peek() {
return stackArr[top];
}
/**
* This method returns true if the stack is
* empty
* @return
*/
public boolean isStackEmpty() {
return (top == -1);
}
/**
* This method returns true if the stack is full
* @return
*/
public boolean isStackFull() {
return (top == stackSize - 1);
}
Power