import java.util.Scanner;
public class Main {
public static class Student {
int id;
String name;
double gpa;
public Student(int id, String name, double gpa) {
this.id = id;
this.name = name;
this.gpa = gpa;
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", GPA: " + gpa;
}
}
// 1. Array
public static class MyArray {
Student[] data = new Student[100];
int size = 0;
public void add(Student s) {
data[size++] = s;
System.out.println("Added to Array.");
}
public void display() {
if (size == 0) {
System.out.println("Array is empty!");
return;
}
for (int i = 0; i < size; i++) {
System.out.println((i+1) + ") " + data[i]);
}
}
public void search(int id) {
boolean found = false;
for (int i = 0; i < size; i++) {
if (data[i].id == id) {
System.out.println("Found: " + data[i]);
found = true;
}
}
if (!found) System.out.println("Not found.");
}
public void update(int id, String name, double gpa) {
for (int i = 0; i < size; i++) {
if (data[i].id == id) {
data[i].name = name;
data[i].gpa = gpa;
System.out.println("Updated successfully.");
return;
}
}
System.out.println("Student ID not found.");
}
}
// 2. Singly Linked List
public static class MySLL {
class Node {
Student st;
Node next;
Node(Student st) {
this.st = st;
this.next = null;
}
}
Node head = null;
public void insertFirst(Student s) {
Node n = new Node(s);
n.next = head;
head = n;
System.out.println("Inserted at first.");
}
public void insertLast(Student s) {
Node n = new Node(s);
if (head == null) {
head = n;
} else {
Node temp = head;
while (temp.next != null) temp = temp.next;
temp.next = n;
}
System.out.println("Inserted at last.");
}
public void insertAt(int pos, Student s) {
if (pos == 0) {
insertFirst(s);
return;
}
Node n = new Node(s);
Node temp = head;
for (int i = 0; i < pos - 1 && temp != null; i++) {
temp = temp.next;
}
if (temp == null) {
System.out.println("Out of bounds!");
} else {
n.next = temp.next;
temp.next = n;
System.out.println("Inserted at position " + pos);
}
}
public void removeFirst() {
if (head == null) {
System.out.println("List is empty.");
return;
}
head = head.next;
System.out.println("First node removed.");
}
public void removeLast() {
if (head == null) {
System.out.println("List is empty.");
return;
}
if (head.next == null) {
head = null;
} else {
Node temp = head;
while (temp.next.next != null) temp = temp.next;
temp.next = null;
}
System.out.println("Last node removed.");
}
public void removeItem(int id) {
if (head == null) return;
if (head.st.id == id) {
head = head.next;
System.out.println("Item removed.");
return;
}
Node temp = head;
while (temp.next != null && temp.next.st.id != id) {
temp = temp.next;
}
if (temp.next == null) {
System.out.println("ID not found.");
} else {
temp.next = temp.next.next;
System.out.println("Item removed.");
}
}
public void search(int id) {
Node temp = head;
while (temp != null) {
if (temp.st.id == id) {
System.out.println("Found: " + temp.st);
return;
}
temp = temp.next;
}
System.out.println("Not found.");
}
public void update(int id, String name, double gpa) {
Node temp = head;
while (temp != null) {
if (temp.st.id == id) {
temp.st.name = name;
temp.st.gpa = gpa;
System.out.println("Updated.");
return;
}
temp = temp.next;
}
System.out.println("Not found.");
}
public void display() {
if (head == null) {
System.out.println("List is empty.");
return;
}
Node temp = head;
while (temp != null) {
System.out.print(temp.st + " --> ");
temp = temp.next;
}
System.out.println("null");
}
}
// 3. Doubly Linked List
public static class MyDLL {
class DNode {
Student st;
DNode next, prev;
DNode(Student st) {
this.st = st;
this.next = null;
this.prev = null;
}
}
DNode head = null;
public void insertFirst(Student s) {
DNode n = new DNode(s);
if (head != null) {
n.next = head;
head.prev = n;
}
head = n;
System.out.println("Inserted first (DLL).");
}
public void insertLast(Student s) {
DNode n = new DNode(s);
if (head == null) {
head = n;
} else {
DNode temp = head;
while (temp.next != null) temp = temp.next;
temp.next = n;
n.prev = temp;
}
System.out.println("Inserted last (DLL).");
}
public void insertAt(int pos, Student s) {
if (pos == 0) {
insertFirst(s);
return;
}
DNode n = new DNode(s);
DNode temp = head;
for (int i = 0; i < pos && temp != null; i++) {
temp = temp.next;
}
if (temp == null) {
insertLast(s);
} else {
n.prev = temp.prev;
n.next = temp;
if (temp.prev != null) temp.prev.next = n;
temp.prev = n;
System.out.println("Inserted at pos.");
}
}
public void removeFirst() {
if (head == null) {
System.out.println("Empty.");
return;
}
head = head.next;
if (head != null) head.prev = null;
System.out.println("Removed first.");
}
public void removeLast() {
if (head == null) {
System.out.println("Empty.");
return;
}
if (head.next == null) {
head = null;
} else {
DNode temp = head;
while (temp.next != null) temp = temp.next;
temp.prev.next = null;
}
System.out.println("Removed last.");
}
public void removeItem(int id) {
if (head == null) return;
if (head.st.id == id) {
removeFirst();
return;
}
DNode temp = head;
while (temp != null && temp.st.id != id) temp = temp.next;
if (temp == null) {
System.out.println("Not found.");
return;
}
if (temp.next != null) temp.next.prev = temp.prev;
if (temp.prev != null) temp.prev.next = temp.next;
System.out.println("Removed.");
}
public void search(int id) {
DNode temp = head;
while (temp != null) {
if (temp.st.id == id) {
System.out.println("Found: " + temp.st);
return;
}
temp = temp.next;
}
System.out.println("Not found.");
}
public void update(int id, String name, double gpa) {
DNode temp = head;
while (temp != null) {
if (temp.st.id == id) {
temp.st.name = name;
temp.st.gpa = gpa;
System.out.println("Updated.");
return;
}
temp = temp.next;
}
System.out.println("Not found.");
}
public void display() {
if (head == null) {
System.out.println("Empty.");
return;
}
DNode temp = head;
while (temp != null) {
System.out.print(temp.st + " <--> ");
temp = temp.next;
}
System.out.println("null");
}
}
// 4. Circular Linked List
public static class MyCLL {
class CNode {
Student st;
CNode next;
CNode(Student st) {
this.st = st;
this.next = null;
}
}
CNode head = null;
CNode tail = null;
public void insertFirst(Student s) {
CNode n = new CNode(s);
if (head == null) {
head = n;
tail = n;
tail.next = head;
} else {
n.next = head;
head = n;
tail.next = head;
}
System.out.println("Inserted first (CLL).");
}
public void insertLast(Student s) {
CNode n = new CNode(s);
if (head == null) {
head = n;
tail = n;
tail.next = head;
} else {
tail.next = n;
tail = n;
tail.next = head;
}
System.out.println("Inserted last (CLL).");
}
public void removeFirst() {
if (head == null) {
System.out.println("Empty.");
return;
}
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
tail.next = head;
}
System.out.println("Removed first.");
}
public void removeLast() {
if (head == null) {
System.out.println("Empty.");
return;
}
if (head == tail) {
head = null;
tail = null;
} else {
CNode temp = head;
while (temp.next != tail) temp = temp.next;
tail = temp;
tail.next = head;
}
System.out.println("Removed last.");
}
public void removeItem(int id) {
if (head == null) return;
if (head.st.id == id) {
removeFirst();
return;
}
CNode temp = head;
while (temp.next != head && temp.next.st.id != id) {
temp = temp.next;
}
if (temp.next == head) {
System.out.println("Not found.");
} else {
temp.next = temp.next.next;
System.out.println("Removed.");
}
}
public void search(int id) {
if (head == null) {
System.out.println("Empty.");
return;
}
CNode temp = head;
do {
if (temp.st.id == id) {
System.out.println("Found: " + temp.st);
return;
}
temp = temp.next;
} while (temp != head);
System.out.println("Not found.");
}
public void update(int id, String name, double gpa) {
if (head == null) return;
CNode temp = head;
do {
if (temp.st.id == id) {
temp.st.name = name;
temp.st.gpa = gpa;
System.out.println("Updated.");
return;
}
temp = temp.next;
} while (temp != head);
System.out.println("Not found.");
}
public void display() {
if (head == null) {
System.out.println("Empty.");
return;
}
CNode temp = head;
do {
System.out.print(temp.st + " --> ");
temp = temp.next;
} while (temp != head);
System.out.println("(Back to Head)");
}
}
// Main
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
MyArray arr = new MyArray();
MySLL sll = new MySLL();
MyDLL dll = new MyDLL();
MyCLL cll = new MyCLL();
while (true) {
System.out.println("\n--- MAIN MENU ---");
System.out.println("1. Array");
System.out.println("2. Singly Linked List");
System.out.println("3. Doubly Linked List");
System.out.println("4. Circular Linked List");
System.out.println("5. Exit");
System.out.print("Choice: ");
int ch = scan.nextInt();
if (ch == 5) break;
switch (ch) {
case 1:
while (true) {
System.out.println("\n[Array] 1.Add 2.Display 3.Search 4.Update 5.Back");
int op = scan.nextInt();
if (op == 5) break;
if (op == 1) {
System.out.print("ID: "); int id = scan.nextInt();
scan.nextLine();
System.out.print("Name: "); String name = scan.nextLine();
System.out.print("GPA: "); double gpa = scan.nextDouble();
arr.add(new Student(id, name, gpa));
} else if (op == 2) arr.display();
else if (op == 3) {
System.out.print("Search ID: "); arr.search(scan.nextInt());
} else if (op == 4) {
System.out.print("ID to update: "); int id = scan.nextInt();
scan.nextLine();
System.out.print("New Name: "); String name = scan.nextLine();
System.out.print("New GPA: "); double gpa = scan.nextDouble();
arr.update(id, name, gpa);
}
} break;
case 2:
while (true) {
System.out.println("\n[SLL] 1.InsFirst 2.InsLast 3.InsAt 4.RemFirst 5.RemLast 6.RemItem 7.Search 8.Update 9.Display 10.Back");
int op = scan.nextInt();
if (op == 10) break;
if (op >= 1 && op <= 3) {
System.out.print("ID: "); int id = scan.nextInt();
scan.nextLine();
System.out.print("Name: "); String name = scan.nextLine();
System.out.print("GPA: "); double gpa = scan.nextDouble();
Student s = new Student(id, name, gpa);
switch (op) {
case 1:
sll.insertFirst(s);
break;
case 2:
sll.insertLast(s);
break;
default:
System.out.print("Position: ");
sll.insertAt(scan.nextInt(), s);
break;
}
} else if (op == 4) sll.removeFirst();
else if (op == 5) sll.removeLast();
else if (op == 6) {
System.out.print("ID to remove: "); sll.removeItem(scan.nextInt());
} else if (op == 7) {
System.out.print("Search ID: "); sll.search(scan.nextInt());
} else if (op == 8) {
System.out.print("ID to update: "); int id = scan.nextInt();
scan.nextLine();
System.out.print("New Name: "); String name = scan.nextLine();
System.out.print("New GPA: "); double gpa = scan.nextDouble();
sll.update(id, name, gpa);
} else if (op == 9) sll.display();
} break;
case 3:
while (true) {
System.out.println("\n[DLL] 1.InsFirst 2.InsLast 3.InsAt 4.RemFirst 5.RemLast 6.RemItem 7.Search 8.Update 9.Display 10.Back");
int op = scan.nextInt();
if (op == 10) break;
if (op >= 1 && op <= 3) {
System.out.print("ID: "); int id = scan.nextInt();
scan.nextLine();
System.out.print("Name: "); String name = scan.nextLine();
System.out.print("GPA: "); double gpa = scan.nextDouble();
Student s = new Student(id, name, gpa);
if (op == 1) dll.insertFirst(s);
else if (op == 2) dll.insertLast(s);
else {
System.out.print("Position: ");
dll.insertAt(scan.nextInt(), s);
}
} else if (op == 4) dll.removeFirst();
else if (op == 5) dll.removeLast();
else if (op == 6) {
System.out.print("ID to remove: "); dll.removeItem(scan.nextInt());
} else if (op == 7) {
System.out.print("Search ID: "); dll.search(scan.nextInt());
} else if (op == 8) {
System.out.print("ID to update: "); int id = scan.nextInt();
scan.nextLine();
System.out.print("New Name: "); String name = scan.nextLine();
System.out.print("New GPA: "); double gpa = scan.nextDouble();
dll.update(id, name, gpa);
} else if (op == 9) dll.display();
} break;
case 4:
while (true) {
System.out.println("\n[CLL] 1.InsFirst 2.InsLast 3.RemFirst 4.RemLast 5.RemItem 6.Search 7.Update 8.Display 9.Back");
int op = scan.nextInt();
if (op == 9) break;
if (op == 1 || op == 2) {
System.out.print("ID: "); int id = scan.nextInt();
scan.nextLine();
System.out.print("Name: "); String name = scan.nextLine();
System.out.print("GPA: "); double gpa = scan.nextDouble();
Student s = new Student(id, name, gpa);
if (op == 1) cll.insertFirst(s);
else cll.insertLast(s);
} else if (op == 3) cll.removeFirst();
else if (op == 4) cll.removeLast();
else if (op == 5) {
System.out.print("ID to remove: "); cll.removeItem(scan.nextInt());
} else if (op == 6) {
System.out.print("Search ID: "); cll.search(scan.nextInt());
} else if (op == 7) {
System.out.print("ID to update: "); int id = scan.nextInt();
scan.nextLine();
System.out.print("New Name: "); String name = scan.nextLine();
System.out.print("New GPA: "); double gpa = scan.nextDouble();
cll.update(id, name, gpa);
} else if (op == 8) cll.display();
} break;
default:
break;
}
}
// scan.close();
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: