Chapter 02 Arrays Linked Lists
Chapter 02 Arrays Linked Lists
LINKED LISTS
DATA STRUCTURES IN JAVA
Arrays...........................................................................................................2
Multidimensional Arrays............................................................................3
Singly Linked Lists ..................................................................................... 4
Circularly Linked Lists................................................................................5
Doubly Linked Lists ....................................................................................7
Integers with no size restriction ............................................................... 8
Abstract Data Type (ADT) for Sparse Arrays ........................................... 11
PAGE 1
Arrays
Arrays in Java are implemented as objects, which means that they have
methods that can be used to access and manipulate their elements.
Arrays in Java are also fixed in size, which means that you must specify
the size of the array when it is created.
To create an array in Java, you must first declare the array variable and
specify the data type of the elements in the array. For example, to create
an array of integers, you would use the following code:
int[] myArray;
This creates a variable called myArray that can hold an array of integers.
To create the actual array, you must use the new keyword and specify
the size of the array. For example:
This creates an array of integers with a size of 10. You can also initialize
the elements of the array when you create it, like this:
Once you have created an array, you can access its elements using the
index. The index of the first element in an array is 0, and the index of
the last element is one less than the size of the array. For example, to
PAGE 2
access the first element in an array called myArray, you would use the
following code:
Multidimensional Arrays
Multidimensional arrays are useful for storing data that has a natural
multidimensional structure. For example, a 2D array can be used to
store a matrix, a 3D array can be used to store a volume, and so on.
type arrayName[][];
where type is the data type of the elements in the array, and arrayName
is the name of the array.
array[2][3] = 10;
PAGE 3
Multidimensional arrays can be used to store a variety of data, including
numbers, strings, and objects.
To implement a singly linked list in Java, you first need to create a Node
class that will hold the data and the reference to the next node. Here is
an example of a simple Node class:
PAGE 4
class Node {
int data;
Node next;
Once you have defined the Node class, you can create a singly linked list
by creating a head node and linking it to the next node, and so on, until
you reach the end of the list.
To add a new node to the list, you create a new Node object and set its
data and next values, and then link it to the previous node by setting its
next reference to the new node. To remove a node from the list, you
simply set the next reference of the previous node to the next node in
the list, effectively removing the current node from the list.
One of the key operations on a singly linked list is traversing the list,
which means visiting each node in the list in order. This can be done
using a loop that starts at the head node and iteratively follows the next
references until the end of the list is reached.
Circularly linked lists, which are similar to singly linked lists but with
one key difference - the last node in the list is linked back to the first
node, creating a circular structure.
A circularly linked list is implemented using a Node class that is like the
one used for singly linked lists, but with an additional reference to the
PAGE 5
first node in the list. Here is an example of a simple Node class for a
circularly linked list:
class Node {
int data;
Node next;
To create a circularly linked list, you first create a head node and link it
to the next node in the list. Then, you continue linking each subsequent
node to the next node until you reach the end of the list, at which point
you link the last node back to the head node, creating a circular
structure.
Circularly linked lists can be used in much the same way as singly linked
lists, but with a few additional operations that take advantage of the
circular structure. For example, you can traverse the list by starting at
the head node and following the next references until you return to the
head node, at which point you know you have visited every node in the
list.
One potential downside of circularly linked lists is that they can be more
difficult to implement and manipulate than singly linked lists. However,
they can also provide some advantages in terms of efficiency and
convenience, particularly in situations where the circular structure is a
natural representation of the data being stored.
PAGE 6
Doubly Linked Lists
class Node {
int data;
Node prev;
Node next;
Once we have our Node class defined, we can create a doubly linked list
by initializing the head and tail nodes and linking them together. From
there, we can add and remove nodes from the list by manipulating the
prev and next references.
One of the key advantages of a doubly linked list is that it allows for
efficient traversal of the list in both directions. For example, to traverse
a singly linked list in reverse, you would need to start at the head node
and follow the next references until you reach the end of the list, then
work your way back through the list using a stack or other data structure.
With a doubly linked list, you can simply start at the tail node and follow
the prev references back to the head node.
Another advantage of doubly linked lists is that they allow for efficient
insertion and deletion of nodes in the middle of the list. With a singly
PAGE 7
linked list, deleting a node requires updating the next reference of the
previous node to point to the next node after the deleted node. With a
doubly linked list, however, you can simply update the prev and next
references of the neighboring nodes to remove the deleted node from
the list.
While doubly linked lists can provide some advantages over singly
linked lists, they also come with some additional complexity and
overhead. For example, each node in a doubly linked list requires twice
as many references as a node in a singly linked list, which can lead to
higher memory usage and slower performance in some situations.
Structure:
PAGE 8
Example:
1. Addition
2. Multiplication
Multiply each digit of one number by every digit of the other number,
keeping track of carry, and shift the results appropriately. Multiply each
digit of the first number by each digit of the second number. Shift the
partial results by their positional value (e.g., multiplying by 10, 100, etc.).
Add all partial results using the addition algorithm.
PAGE 9
Add partial results: [8, 6] + [0, 4, 3] → [8, 0, 4] → 408.
3. Exponentiation
ab=(ab/2)2 if b is even.
Example: Computing 34
Step 1: 3×3=9.
Step 2: 9×9=81.
Implementation Notes
Use linked lists for dynamic memory allocation and to handle very large
numbers.
Addition and multiplication must check the signs of the numbers and
adjust the operation (e.g., subtraction for opposite signs).
PAGE 10
Abstract Data Type (ADT) for Sparse Arrays
Basic Operations
Set Value: Set the value of the element at position (i, j). Example: set(i,
j, value).
Get Value: Retrieve the value of the element at position (i, j). Example:
get(i, j).
Get Dimensions: Retrieve the number of rows and columns in the array.
Example: getDimensions() → (rows, columns).
Reset: Reset all elements in the array to a specific value (e.g., 0).
Example: reset(value).
PAGE 11
Given the scenario of a 1000×1000 array where fewer than 10,000
elements are non-zero, a standard implementation would require
memory for 1,000,000 elements, most of which would be unnecessary.
This method stores only the non-zero elements along with their row and
column indices.
Use a list of tuples (row, column, value) for each non-zero element.
005
000
300
Operations:
Get Value: Search the list for the (row, column) pair.
Set Value: Update the existing tuple if it exists or add a new tuple for
non-zero values.
PAGE 12