Abstract
Abstract
Definition
Abstract Data Type (ADT) is a data structure defined by a set of values and operations.
• It specifies *what* operations are performed, but *not how* they are implemented.
• Provides an *implementation-independent* view, making it abstract.
Key Concept
Features of ADT
1. Abstraction: Users see what an ADT can do, but not how it's implemented.
2. Encapsulation: Internal details are hidden, and a public interface is provided for interaction.
Common ADTs
1. List ADT
Description
A list stores elements in a sequence with a structure for count and pointers.
Operations
2. Stack ADT
Description
• Stack follows a Last-In-First-Out (LIFO) principle.
• Only the top element is accessible.
Operations
3. Queue ADT
Description
Operations
Advantages of ADTs
Disadvantages of ADTs
1. Overhead: Extra memory and processing can affect performance.
2. Complexity: Large or complex data structures may be difficult to implement.
3. Learning Curve: Requires understanding of ADT concepts and implementations.
4. Limited Flexibility: Some ADTs may not be suitable for certain data types.
5. Cost: Development effort and resources may increase due to complexity
Array: Definition & Need
• Definition: An array is a linear data structure that stores a collection of elements of the
same data type. It allows multiple elements to be stored together in contiguous memory
locations.
• Need: Arrays are useful when there’s a need to store multiple elements and offer efficient
O(1) random access due to direct memory addressing. This makes operations like lookups
extremely fast.
Key Concepts
Array Representation
• An array is visualized as a collection of buckets, where each bucket holds one element.
• Example:
Array: [10, 20, 30, 40]
Indices: 0 1 2 3
• Access is done using the index, such as array[2] to get the third element (30).
int [] array = {10, 20, 30, 40}; // Declaring and initializing an array
2. Insertion Operation
3. Deletion Operation
4. Search Operation
5. Update Operation
6. Display Operation
Advantages of Arrays
• Fast access: O(1) time complexity for accessing elements using an index.
• Contiguous memory: Ensures efficient storage and quick traversal.
Linked List
Definition
A linked list is a linear data structure composed of nodes connected via pointers. Each node has:
Key Features
Basic Operations
1. Insertion
Add a node to the list.
a. At Beginning: Insert a new node at the start.
b. At End: Insert a new node at the end.
c. At a Given Position: Insert a node after a specified node.
2. Deletion
Remove a node from the list.
a. At Beginning: Remove the head node.
b. At End: Remove the last node.
c. At a Given Position: Delete a node by key or position.
3. Traversal
Walk through all nodes sequentially to access their values.
4. Search
Locate a node by its value using a key.
5. Reversal
Reverse the order of nodes in the list.
Insertion Operations
1. Insertion at Beginning
2. Insertion at End
Deletion Operations
1. Deletion at Beginning
2. Deletion at End
Reversal Operation
Search Operation
Advantages
Disadvantages
Definition
A Doubly Linked List is a variation of a linked list where each node contains pointers to both its
next and previous nodes, enabling traversal in both forward and backward directions.
Key Terms
Features
Representation
• Each node contains data, a pointer to the next node, and a pointer to the previous node.
• The head points to the first node, and the tail points to the last node.
Basic Operations
1. Insertion
a. At Beginning: Adds a node at the start.
b. At End: Adds a node at the end.
c. After a Node: Inserts a node after a specified node.
2. Deletion
a. At Beginning: Removes the first node.
b. At End: Removes the last node.
c. By Key: Deletes a node containing a specific key.
3. Traversal
a. Forward: Traverses from the head to the tail.
b. Backward: Traverses from the tail to the head.
Code Examples
1. Insertion Operations
Insertion at Beginning
2. Deletion Operations
Deletion at Beginning
Deletion at End
Deletion by Key
3. Traversal Operations
Forward Traversal
Backward Traversal
Advantages
Disadvantages
A Circular Linked List is a variation of a linked list in which the last node points back to the
first node, creating a circular structure. It can be implemented as either a Singly Circular
Linked List or a Doubly Circular Linked List.
In a Singly Circular Linked List, the next pointer of the last node points to the first node,
creating a circular reference.
In a Doubly Circular Linked List, the next pointer of the last node points to the first node, and
the previous pointer of the first node points back to the last node, forming a circular reference in
both directions.
Insertion is done at the start of the circular linked list. The steps for inserting at the start are as
follows:
import java.util.*;
class Node {
int data;
int key;
Node next;
}
public class CircularLinkedList {
static Node head = null;
static boolean isEmpty() {
return head == null;
}
// Insert at the start of the list
static void insertFirst(int key, int data) {
Node newNode = new Node();
newNode.key = key;
newNode.data = data;
if (isEmpty()) {
head = newNode;
head.next = head;
} else {
newNode.next = head;
head = newNode;
}
}
// Display the list
static void printList() {
Node ptr = head;
if (head != null) {
do {
System.out.print("(" + ptr.key + "," + ptr.data + ") ");
ptr = ptr.next;
} while (ptr != head);
}
System.out.println();
}
Output:
The deletion operation removes a node from the list, which can be done at the beginning, middle,
or end. For deletion:
1. If the list has only one node, set the head to null.
2. If the node to be deleted is at the start, set the head to the next node.
3. If the node is at the end, set the second-to-last node's next pointer to the head.
4. For a middle node, remove the node by linking its previous node to its next node.
deleteFirst();
System.out.print("List after deleting the first item: ");
printList();
}
}
Output:
To display the list, we traverse the list starting from the head and continue until we return to the
head node, printing each element during the traversal.
Output:
A complete implementation would support insertion, deletion, and display functions along with
other operations such as length calculation.
boolean isEmpty() {
return head == null;
}
while (!linkedList.isEmpty()) {
Node temp = linkedList.deleteFirst();
System.out.println("Deleted value: (" + temp.key + "," + temp.data
+ ")");
}
linkedList.printList();
}
}
Output: