public class CustomLinkedList<T> {
// Node class to represent each element in the list
private static class Node<T> {
T data;
Node<T> next;
Node(T data) {
this.data = data;
this.next = null;
}
}
private Node<T> head;
private Node<T> tail;
private int size;
// Constructor
public CustomLinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
// Add element at the end
public void add(T data) {
Node<T> newNode = new Node<>(data);
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
size++;
}
// Add element at the beginning
public void addFirst(T data) {
Node<T> newNode = new Node<>(data);
if (head == null) {
head = tail = newNode;
} else {
newNode.next = head;
head = newNode;
}
size++;
}
// Remove and return the first element
public T removeFirst() {
if (head == null) throw new IllegalStateException("List is empty");
T data = head.data;
head = head.next;
if (head == null) {
tail = null;
}
size--;
return data;
}
// Remove and return the last element
public T removeLast() {
if (head == null) throw new IllegalStateException("List is empty");
if (head == tail) {
T data = head.data;
head = tail = null;
size--;
return data;
}
Node<T> current = head;
while (current.next != tail) {
current = current.next;
}
T data = tail.data;
tail = current;
tail.next = null;
size--;
return data;
}
// Get element at a specific index
public T get(int index) {
if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index out of range");
Node<T> current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current.data;
}
// Return the size of the list
public int size() {
return size;
}
// Check if the list is empty
public boolean isEmpty() {
return size == 0;
}
// Print all elements in the list
public void printList() {
Node<T> current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}
public static void main(String[] args) {
CustomLinkedList<String> list = new CustomLinkedList<>();
// Add elements
list.add("A");
list.add("B");
list.add("C");
list.printList(); // A -> B -> C -> null
// Add element at the beginning
list.addFirst("D");
list.printList(); // D -> A -> B -> C -> null
// Remove first and last elements
System.out.println("Removed First: " + list.removeFirst()); // D
System.out.println("Removed Last: " + list.removeLast()); // C
list.printList(); // A -> B -> null
// Get element at index
System.out.println("Element at index 1: " + list.get(1)); // B
// Check size and empty status
System.out.println("Size: " + list.size()); // 2
System.out.println("Is empty: " + list.isEmpty()); // false
}
}
import java.util.*;
public class GraphRepresentation {
// Graph represented using an adjacency matrix
static class AdjacencyMatrixGraph {
private final int[][] matrix;
private final int vertices;
public AdjacencyMatrixGraph(int vertices) {
this.vertices = vertices;
matrix = new int[vertices][vertices];
}
public void addEdge(int src, int dest) {
matrix[src][dest] = 1;
matrix[dest][src] = 1; // For undirected graph
}
public void removeEdge(int src, int dest) {
matrix[src][dest] = 0;
matrix[dest][src] = 0; // For undirected graph
}
public void printGraph() {
System.out.println("Adjacency Matrix:");
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
}
}
}
// Graph represented using an adjacency list
static class AdjacencyListGraph {
private final List<List<Integer>> adjList;
public AdjacencyListGraph(int vertices) {
adjList = new ArrayList<>();
for (int i = 0; i < vertices; i++) {
adjList.add(new ArrayList<>());
}
}
public void addEdge(int src, int dest) {
adjList.get(src).add(dest);
adjList.get(dest).add(src); // For undirected graph
}
public void removeEdge(int src, int dest) {
adjList.get(src).remove(Integer.valueOf(dest));
adjList.get(dest).remove(Integer.valueOf(src)); // For undirected graph
}
public void printGraph() {
System.out.println("Adjacency List:");
for (int i = 0; i < adjList.size(); i++) {
System.out.println(i + " -> " + adjList.get(i));
}
}
}
public static void main(String[] args) {
// Using adjacency matrix
AdjacencyMatrixGraph matrixGraph = new AdjacencyMatrixGraph(5);
matrixGraph.addEdge(0, 1);
matrixGraph.addEdge(0, 2);
matrixGraph.addEdge(1, 3);
matrixGraph.addEdge(2, 4);
matrixGraph.printGraph();
System.out.println();
// Using adjacency list
AdjacencyListGraph listGraph = new AdjacencyListGraph(5);
listGraph.addEdge(0, 1);
listGraph.addEdge(0, 2);
listGraph.addEdge(1, 3);
listGraph.addEdge(2, 4);
listGraph.printGraph();
}
}