Another Version-Singly
Another Version-Singly
peek();
languages.add("Python");
// add element at the end
languages.add("Java");
languages.offer("Swift");
languages.add("C");
System.out.println("LinkedList after offer(): " +
System.out.println("LinkedList: " + languages);
languages);
animals.addFirst("Dog"); }
animals.addLast("Zebra");
// Constructor
Node(int d) { JOptionPane.showMessageDialog(null,
"LinkedList: " + sb.toString());
data = d;
}
next = null;
}
// Method to delete the first node
} (simple deletion)
public static SimpleLinkedList
// Method to insert a new node at the deleteFirst(SimpleLinkedList list) {
end if (list.head == null) {
public static SimpleLinkedList
insert(SimpleLinkedList list, int data) { JOptionPane.showMessageDialog(null,
Node new_node = new Node(data); "List is empty, nothing to delete.");
} else {
if (list.head == null) {
JOptionPane.showMessageDialog(null,
list.head = new_node; "Deleted the first node.");
} else { }
Node last = list.head; return list;
while (last.next != null) { }
last = last.next;
} // Method to search for an element
last.next = new_node; public static void
search(SimpleLinkedList list, int key) {
}
Node currNode = list.head;
return list;
int position = 1;
}
boolean found = false;
// Driver code
JOptionPane.showMessageDialog(null,
public static void main(String[] args) "Invalid choice. Please try again.");
{
}
SimpleLinkedList list = new
} while (choice != 5);
SimpleLinkedList();
}}
int choice;
Simple STUPID
import javax.swing.JOptionPane;
do {
String input =
JOptionPane.showInputDialog( public class SimpleLinkedList {
"1. Insert\n2. Traverse\n3.
Delete First\n4. Search\n5. Exit\nEnter
Node head; // head of list
your choice:");
choice = Integer.parseInt(input);
// Linked list Node class
static class Node {
switch (choice) {
int data;
case 1:
Node next;
String valueStr =
JOptionPane.showInputDialog("Enter
value to insert:");
// Constructor
int value =
Integer.parseInt(valueStr); Node(int d) {
case 2: }
traverse(list); }
break;
// Method to insert a new node at the end
public void insert(int data) { do {
Node newNode = new Node(data); String input =
JOptionPane.showInputDialog("1. Insert\n2.
if (head == null) {
Show List\n3. Exit\nEnter your choice:");
head = newNode;
choice = Integer.parseInt(input);
} else {
Node current = head;
switch (choice) {
while (current.next != null) {
case 1:
current = current.next;
String valueStr =
} JOptionPane.showInputDialog("Enter value to
insert:");
current.next = newNode;
int value =
} Integer.parseInt(valueStr);
} list.insert(value);
break;
// Method to show the list (simple) case 2:
public void showList() { list.showList();
if (head == null) { break;
case 3:
JOptionPane.showMessageDialog(null, "List is
empty.");
JOptionPane.showMessageDialog(null,
return; "Exiting program.");
} break;
default:
Node current = head;
StringBuilder listContent = new JOptionPane.showMessageDialog(null,
StringBuilder(); "Invalid choice. Please try again.");
}
JOptionPane.showMessageDialog(null,
"LinkedList: " + listContent.toString()); public class SimpleLinkedList {
}
Node head; // head of list
// Constructor JOptionPane.showMessageDialog(null,
"LinkedList: " + listContent.toString());
Node(int d) {
}
data = d;
next = null;
// Method to search for an element in the
}
list
}
public void search(int value) {
if (head == null) {
// Method to insert a new node at the end
public void insert(int data) { JOptionPane.showMessageDialog(null, "List is
empty.");
Node newNode = new Node(data);
return;
if (head == null) {
}
head = newNode;
} else {
Node current = head;
Node current = head;
int position = 1;
while (current.next != null) {
boolean found = false;
current = current.next;
}
while (current != null) {
current.next = newNode;
if (current.data == value) {
}
} JOptionPane.showMessageDialog(null, value
+ " found at position " + position);
return;
} if (!found) {
JOptionPane.showMessageDialog(null, value
Node current = head; + " not found in the list.");
StringBuilder listContent = new }
StringBuilder();
}
switch (choice) {
case 1: // Insert
String valueStr =
JOptionPane.showInputDialog("Enter value to
insert:");
int value =
Integer.parseInt(valueStr);
list.insert(value);
break;
case 2: // Traverse
list.traverse();
break;
case 3: // Search
String searchStr =
JOptionPane.showInputDialog("Enter value to
search:");
int searchValue =
Integer.parseInt(searchStr);
list.search(searchValue);
break;
case 4: // Delete
String deleteStr =
JOptionPane.showInputDialog("Enter value to
delete:");
int deleteValue =
Integer.parseInt(deleteStr);
list.delete(deleteValue);
break;
case 5: // Exit
JOptionPane.showMessageDialog(null,
"Exiting program.");
break;
default:
data = value;
next = null;
}
}
}
JOptionPane.showMessageDialog(null, "The
return listContent.toString(); list is empty.");
} return;
}
// Search for a value in the list
public void search(int value) { // If the head node needs to be deleted
if (isEmpty()) { if (head.data == value) {
head = head.next;
JOptionPane.showMessageDialog(null, "The
list is empty.");
JOptionPane.showMessageDialog(null, value
return; + " deleted.");
} return;
int position = findValuePosition(value); }
if (position != -1) {
} else { if (isDeleted) {
} } else {
}
JOptionPane.showMessageDialog(null, value
+ " not found.");
// Helper function to find the position of a }
value
}
private int findValuePosition(int value) {
Node currentNode = head;
// Helper function to delete a node with a
int position = 1; specific value
private boolean deleteNode(int value) { case 3: // Search
Node currentNode = head; String searchStr =
JOptionPane.showInputDialog("Enter value to
Node previousNode = null;
search:");
int searchValue =
while (currentNode != null && Integer.parseInt(searchStr);
currentNode.data != value) {
list.search(searchValue);
previousNode = currentNode;
break;
currentNode = currentNode.next;
case 4: // Delete
}
String deleteStr =
JOptionPane.showInputDialog("Enter value to
delete:");
if (currentNode != null) {
int deleteValue =
previousNode.next = Integer.parseInt(deleteStr);
currentNode.next;
list.delete(deleteValue);
return true;
break;
}
case 5: // Exit
return false; // Not found
} JOptionPane.showMessageDialog(null,
"Exiting program.");
switch (choice) {
case 1: // Insert
String valueStr =
JOptionPane.showInputDialog("Enter value to
insert:");
int value =
Integer.parseInt(valueStr);
list.insert(value);
break;
case 2: // Traverse
list.traverse();
break;
Node last = head;
while (last.next != null) {
last = last.next;
}
last.next = newNode;
newNode.prev = last;
}
JOptionPane.showMessageDialog(null,
"Inserted " + data);
}
JOptionPane.showMessageDialog(null, "List is
DOUBLY STUPID NI SIYAAA empty");
class DoublyLinkedList {
Node current = head;
class Node {
int data; // Find the node to be deleted
} return;
}
} else { }
while (last.next != null) {
if (current.prev != null) { last = last.next;
current.prev.next = current.next; }
}
StringBuilder result = new
StringBuilder("List (backward): ");
JOptionPane.showMessageDialog(null,
"Deleted " + data);
} while (last != null) {
result.append(last.data).append(" ");
// Display the list from head to tail last = last.prev;
public void displayForward() { }
if (head == null) {
JOptionPane.showMessageDialog(null,
JOptionPane.showMessageDialog(null, "List is result.toString());
empty");
}
return;
}
// Menu to perform operations
public void menu() {
Node current = head;
while (true) {
StringBuilder result = new
String choice =
StringBuilder("List (forward): ");
JOptionPane.showInputDialog(
"1. Insert\n" +
while (current != null) {
"2. Delete\n" +
result.append(current.data).append("
"3. Display Forward\n" +
");
"4. Display Backward\n" +
current = current.next;
"5. Exit\n" +
}
"Enter your choice:");
JOptionPane.showMessageDialog(null,
result.toString()); if (choice == null) break;
}
switch (choice) {
// Display the list from tail to head case "1":
public void displayBackward() { String insertData =
JOptionPane.showInputDialog("Enter number
if (head == null) {
to insert:");
if (insertData != null) {
JOptionPane.showMessageDialog(null, "List is
empty");
insert(Integer.parseInt(insertData));
return;
}
}
break;
case "2":
Node last = head;
String deleteData =
JOptionPane.showInputDialog("Enter number
to delete:");
if (deleteData != null) {
delete(Integer.parseInt(deleteData));
}
break;
case "3":
displayForward();
break;
case "4":
displayBackward();
break;
case "5":
return;
default:
JOptionPane.showMessageDialog(null,
"Invalid choice, try again.");
}
}
}