0% found this document useful (0 votes)
4 views

Why Dynamic Data Structure

The document explains dynamic data structures, particularly linked lists, highlighting their advantages over static data structures such as efficient memory utilization and ease of insertion and deletion. It details the structure and types of linked lists, Java implementations for creating, displaying, counting nodes, summing elements, finding maximum values, and searching within a linked list. Additionally, it discusses optimization techniques for searching, such as Move to Front and Transposition, to enhance efficiency.

Uploaded by

HARISH S ECE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Why Dynamic Data Structure

The document explains dynamic data structures, particularly linked lists, highlighting their advantages over static data structures such as efficient memory utilization and ease of insertion and deletion. It details the structure and types of linked lists, Java implementations for creating, displaying, counting nodes, summing elements, finding maximum values, and searching within a linked list. Additionally, it discusses optimization techniques for searching, such as Move to Front and Transposition, to enhance efficiency.

Uploaded by

HARISH S ECE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Why Dynamic Data Structure?

1. Introduction to Data Structures

● Static Data Structures: Arrays, fixed in size, memory allocation happens at compile
time.
● Dynamic Data Structures: Size can grow or shrink during runtime, memory allocation
happens at runtime.

2. Limitations of Static Data Structures

● Fixed size leads to memory wastage if underutilized.


● Cannot expand if more elements are needed after allocation.
● Insertion and deletion of elements are expensive operations.

3. Advantages of Dynamic Data Structures

● Efficient Memory Utilization: Memory is allocated as needed.


● Flexibility: Can dynamically grow or shrink.
● Ease of Insertion and Deletion: Operations are more efficient compared to arrays.

4. Real-life Applications of Dynamic Data Structures

● Database management systems


● Implementing graphs and trees
● Task scheduling systems

Part 2: Introduction to Linked List


1. What is a Linked List?

● A dynamic data structure consisting of nodes.


● Each node contains:
○ Data: Stores the value.
○ Pointer (Next): Points to the next node.

2. Types of Linked Lists

● Singly Linked List: Each node points to the next node.


● Doubly Linked List: Each node points to both the next and previous nodes.
● Circular Linked List: Last node points back to the first node.

3. Why Linked List Over Arrays?

● Dynamic memory allocation.


● Efficient insertion and deletion.
● No need for contiguous memory allocation.

4. Representation of a Node in Java


class Node {
int data;
Node next;
}

Part 3: More About Linked List and Displaying a Linked


List
1. Structure of a Linked List

● Head Node: Entry point of the linked list.


● Tail Node: Last node, usually points to null.

2. Creating a Linked List in Java


class LinkedList {
Node head;

public void insert(int data) {


Node newNode = new Node();
newNode.data = data;
newNode.next = head;
head = newNode;
}
}

3. Displaying a Linked List


public void display() {
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}

4. Output Example:

10 -> 20 -> 30 -> null

Part 4: Counting all the Nodes in a Linked List and Sum


of all Elements
1. Counting Nodes in a Linked List

● Traverse the list and count each node.

Java Implementation:

public int countNodes() {


int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}

2. Sum of All Elements in a Linked List

● Traverse the list and sum up all node values.

Java Implementation:

public int sumNodes() {


int sum = 0;
Node current = head;
while (current != null) {
sum += current.data;
current = current.next;
}
return sum;
}

Part 5: Finding Maximum in a Linked List and Searching


in a Linked List
1. Finding Maximum Value in a Linked List

● Traverse the list and compare each node's data.

Java Implementation:

public int findMax() {


int max = Integer.MIN_VALUE;
Node current = head;
while (current != null) {
if (current.data > max) {
max = current.data;
}
current = current.next;
}
return max;
}

2. Searching an Element in a Linked List

● Traverse and check if the value matches.

Java Implementation:

public boolean search(int key) {


Node current = head;
while (current != null) {
if (current.data == key) {
return true;
}
current = current.next;
}
return false;
}
Part 6: Improving Searching in a Linked List
1. Linear Search Optimization

● Move to Front (MTF): Move the found node to the head.


● Transposition: Swap the found node with its previous node to improve future searches.

2. Move to Front (MTF) Implementation


public boolean optimizedSearch(int key) {
Node prev = null;
Node current = head;
while (current != null) {
if (current.data == key) {
if (prev != null) {
prev.next = current.next;
current.next = head;
head = current;
}
return true;
}
prev = current;
current = current.next;
}
return false;
}

3. Advantages of Optimized Searching

● Reduces search time for frequently accessed elements.


● Improves efficiency over time.

This structured content covers the basics and gradually progresses to advanced concepts,
making it ideal for students or learners to understand Linked Lists step by step. Let me know if
you'd like more elaboration or examples on any part!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy