Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

LinkedLists Java NPTEL Notes

Uploaded by

SANSKRITI SAXENA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

LinkedLists Java NPTEL Notes

Uploaded by

SANSKRITI SAXENA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Linked Lists in Java - NPTEL Exam Notes

1. Basic Concepts:

- Array: Stores elements in contiguous memory locations and has static size (fixed data).

- Linked List: Uses dynamic memory allocation and is linear. It's implemented using nodes and

pointers, where each node contains the data and a reference (pointer) to the next node.

2. Types of Linked Lists:

- Singly Linked List: Each node points to the next node, and the last node points to null.

- Doubly Linked List: Each node contains references to both the next and previous nodes.

- Circular Linked List: The last node points back to the first node, creating a circular chain.

3. Operations on Singly Linked Lists:

- Insertion:

At the beginning:

```

class Node {

int data;

Node next;

Node(int data) {

this.data = data;

next = null;

Node insertAtBegin(Node head, int data) {


Node newNode = new Node(data);

newNode.next = head;

return newNode;

```

- Deletion:

At the beginning:

```

Node deleteAtBegin(Node head) {

if (head == null) return null;

return head.next;

```

- Traversal:

```

void traverse(Node head) {

Node current = head;

while (current != null) {

System.out.print(current.data + " ");

current = current.next;

```
- Reversing:

```

Node reverse(Node head) {

Node prev = null, current = head, next = null;

while (current != null) {

next = current.next;

current.next = prev;

prev = current;

current = next;

return prev;

```

4. Operations on Doubly Linked Lists:

Insertion at the beginning:

```

class DoublyNode {

int data;

DoublyNode next, prev;

DoublyNode(int data) {

this.data = data;

next = prev = null;

DoublyNode insertAtBegin(DoublyNode head, int data) {


DoublyNode newNode = new DoublyNode(data);

newNode.next = head;

if (head != null) head.prev = newNode;

return newNode;

```

5. Applications of Linked Lists:

- Sparse Matrix Multiplication: Linked lists can represent sparse matrices efficiently by only storing

non-zero elements.

- Polynomial Manipulation: Linked lists store polynomials, where each node represents a term,

allowing easy insertion of terms and multiplication operations.

- Memory Management: Linked lists can be used to manage free memory blocks in dynamic

memory allocation systems.

6. Java Collections Framework (JCF) and Linked Lists:

The LinkedList class in Java is part of the Java Collections Framework (JCF). It supports sequential

access to elements and inherits from AbstractSequentialList. It implements both List and Deque

interfaces, providing methods to add, remove, and manipulate elements at both ends.

Salient features of the LinkedList class:

- Linked-List Data Structure: Provides a linked-list data structure where elements are stored

sequentially.

- Allows Duplicates: The LinkedList class can contain duplicate elements.

- Doubly Linked List: Uses a doubly linked list internally to store the elements.

- Maintains Insertion Order: Maintains the order in which elements are inserted.
- Non-Synchronized: The class is not synchronized by default, meaning it is not thread-safe unless

explicitly synchronized.

- Faster Manipulation: No shifting is required, so insertion and deletion are faster than in arrays.

- Versatility: The class can be used to maintain a collection as a linked list, stack, or queue.

You might also like