0% found this document useful (0 votes)
6 views16 pages

Another Version-Singly

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

Another Version-Singly

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

import java.util.LinkedList; String str1 = languages.

peek();

import java.util.Queue; System.out.println("Accessed Element: " + str1);

class Main { // access and remove the first element

public static void main(String[] args) { String str2 = languages.poll();

Queue<String> languages = new LinkedList<>(); System.out.println("Removed Element: " + str2);

System.out.println("LinkedList after poll(): " +


languages);
// add elements

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);

// access the first element

import java.util.LinkedList; System.out.println("LinkedList after addLast(): " +


animals);
import java.util.Deque;

// remove the first element


class Main {
animals.removeFirst();
public static void main(String[] args){
System.out.println("LinkedList after removeFirst(): "
Deque<String> animals = new LinkedList<>();
+ animals);

// add element at the beginning


// remove the last element
animals.add("Cow");
animals.removeLast();
System.out.println("LinkedList: " + animals);
System.out.println("LinkedList after removeLast(): "
+ animals);

animals.addFirst("Dog"); }

System.out.println("LinkedList after addFirst(): " + }


animals);

// add elements at the end

animals.addLast("Zebra");

ANOTHER VERSION: public class LinkedList {


import javax.swing.JOptionPane;
Node head; // head of list
public static void printList(LinkedList list) {
// Linked list Node class Node currNode = list.head;
static class Node { StringBuilder sb = new StringBuilder();

int data; // Traverse through the LinkedList


Node next; while (currNode != null) {
// Append the data at current node
// Constructor sb.append(currNode.data).append("
");
Node(int d) {
// Go to next node
data = d;
currNode = currNode.next;
next = null;
}
}
}
// Display the linked list using
JOptionPane
// Method to insert a new node
JOptionPane.showMessageDialog(null,
public static LinkedList insert(LinkedList "LinkedList: " + sb.toString());
list, int data) {
}
// Create a new node with given data
Node new_node = new Node(data);
// Driver code
public static void main(String[] args) {
// If the Linked List is empty, then make
// Start with an empty list
the new node as head
LinkedList list = new LinkedList();
if (list.head == null) {
list.head = new_node;
// Insert values using JOptionPane for
} else {
input
// Else traverse till the last node and
for (int i = 1; i <= 8; i++) {
insert the new node there
String input =
Node last = list.head;
JOptionPane.showInputDialog("Enter value
while (last.next != null) { for node " + i + ":");

last = last.next; int value = Integer.parseInt(input); //


Convert input to integer
}
list = insert(list, value); // Insert value
into the list
// Insert the new node at last node }
last.next = new_node;
} // Print the LinkedList
printList(list);
// Return the list by head }
return list; }
} SINGLYLINKEDLIST
import javax.swing.JOptionPane;
// Method to print the LinkedList
Node currNode = list.head;
public class SimpleLinkedList { StringBuilder sb = new
StringBuilder();

Node head; // head of list


// Traverse through the LinkedList
while (currNode != null) {
// Linked list Node class
static class Node {
sb.append(currNode.data).append(" ");
int data;
currNode = currNode.next;
Node next;
}

// 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 the Linked List is empty, make list.head = list.head.next; //


the new node the head Move head to the next node

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;

// Method to traverse and print the


LinkedList // Traverse the list
public static void while (currNode != null) {
traverse(SimpleLinkedList list) {
if (currNode.data == key) {
case 3:
JOptionPane.showMessageDialog(null,
list = deleteFirst(list);
key + " found at position " + position);
break;
found = true;
case 4:
break;
String searchStr =
}
JOptionPane.showInputDialog("Enter
currNode = currNode.next; value to search:");
position++; int searchKey =
Integer.parseInt(searchStr);
}
search(list, searchKey);
break;
if (!found) {
case 5:
JOptionPane.showMessageDialog(null,
key + " not found in the list."); JOptionPane.showMessageDialog(null,
"Exiting program.");
}
break;
}
default:

// 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) {

list = insert(list, value); data = d;

break; next = null;

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.");
}

while (current != null) { } while (choice != 3);


}
listContent.append(current.data).append(" }
");
SIMPLEST STUPID ONE THAT I CODE:
current = current.next;
}
import javax.swing.JOptionPane;

JOptionPane.showMessageDialog(null,
"LinkedList: " + listContent.toString()); public class SimpleLinkedList {

}
Node head; // head of list

public static void main(String[] args) {


SimpleLinkedList list = new // Linked list Node class
SimpleLinkedList();
static class Node {
int choice;
int data;
Node next; }

// 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);

// Method to traverse the list and show the found = true;


elements break;
public void traverse() { }
if (head == null) { current = current.next;
position++;
JOptionPane.showMessageDialog(null, "List is
empty."); }

return;
} if (!found) {

JOptionPane.showMessageDialog(null, value
Node current = head; + " not found in the list.");
StringBuilder listContent = new }
StringBuilder();
}

while (current != null) {


// Method to delete the first occurrence of
a node with a specific value
listContent.append(current.data).append("
"); public void delete(int value) {

current = current.next; if (head == null) {


do {
JOptionPane.showMessageDialog(null, "List is
String input =
empty.");
JOptionPane.showInputDialog(
return;
"1. Insert\n2. Traverse\n3.
} Search\n4. Delete\n5. Exit\nEnter your
choice:");
choice = Integer.parseInt(input);
// If the head needs to be removed
if (head.data == value) {
switch (choice) {
head = head.next;
case 1:
JOptionPane.showMessageDialog(null, value String valueStr =
+ " deleted from the list."); JOptionPane.showInputDialog("Enter value to
insert:");
return;
int value =
}
Integer.parseInt(valueStr);
list.insert(value);
// Traverse the list to find the value to
break;
delete
case 2:
Node current = head;
list.traverse();
Node previous = null;
break;
case 3:
while (current != null && current.data !
= value) { String searchStr =
JOptionPane.showInputDialog("Enter value to
previous = current;
search:");
current = current.next;
int searchValue =
} Integer.parseInt(searchStr);
list.search(searchValue);

// If the value was found, remove it break;

if (current != null) { case 4:

previous.next = current.next; String deleteStr =


JOptionPane.showInputDialog("Enter value to
delete:");
JOptionPane.showMessageDialog(null, value
+ " deleted from the list."); int deleteValue =
Integer.parseInt(deleteStr);
} else {
list.delete(deleteValue);

JOptionPane.showMessageDialog(null, value break;


+ " not found in the list.");
case 5:
}
} JOptionPane.showMessageDialog(null,
"Exiting program.");
break;
public static void main(String[] args) {
default:
SimpleLinkedList list = new
SimpleLinkedList();
JOptionPane.showMessageDialog(null,
int choice; "Invalid choice. Please try again.");
}
} while (choice != 5);
}}
MY BEGINNER CODING SHITS:
import javax.swing.JOptionPane;

public class SimpleLinkedList {


Node head; // Start of the list (head)

// Inner class to represent a Node


static class Node {
int data; // Data in the node
Node next; // Link to the next node

// Constructor to create a new node


Node(int value) {
data = value;
next = null;
}
}

// Insert a new node at the end


public void insert(int value) {
Node newNode = new Node(value); //
Create a new node
if (head == null) { // If list is empty,
new node becomes head
head = newNode;
} else {
Node current = head;
while (current.next != null) { //
Traverse to the last node
current = current.next;
}
current.next = newNode; // Link new
node at the end
}
}

// Traverse the list and show the elements


public void traverse() {
if (head == null) { // Delete the first occurrence of a value
public void delete(int value) {
JOptionPane.showMessageDialog(null, "The
if (head == null) {
list is empty.");
return;
JOptionPane.showMessageDialog(null, "The
} list is empty.");
Node current = head; return;
StringBuilder listContent = new }
StringBuilder();
while (current != null) { // Go through
// If the head node needs to be deleted
each node
if (head.data == value) {
listContent.append(current.data).append(" head = head.next;
"); // Add data to string
current = current.next; JOptionPane.showMessageDialog(null, value
+ " deleted.");
}
return;
JOptionPane.showMessageDialog(null,
"Linked List: " + listContent.toString()); }
}

// Search for the value to delete


// Search for a value in the list Node current = head;
public void search(int value) { Node previous = null;
if (head == null) { while (current != null && current.data !
= value) {
JOptionPane.showMessageDialog(null, "The previous = current;
list is empty.");
current = current.next;
return;
}
}
Node current = head;
if (current != null) { // Value found,
int position = 1; remove it
while (current != null) { previous.next = current.next;
if (current.data == value) {
JOptionPane.showMessageDialog(null, value
+ " deleted.");
JOptionPane.showMessageDialog(null, value
+ " found at position " + position); } else { // Value not found
return;
JOptionPane.showMessageDialog(null, value
}
+ " not found.");
current = current.next;
}
position++;
}
}
JOptionPane.showMessageDialog(null,
public static void main(String[] args) {
value + " not found.");
SimpleLinkedList list = new
}
SimpleLinkedList();
int choice;
JOptionPane.showMessageDialog(null,
do {
"Invalid choice. Please try again.");
// Menu for the user to choose an
}
operation
} while (choice != 5);
String input =
JOptionPane.showInputDialog( }
"1. Insert\n2. Traverse\n3. }
Search\n4. Delete\n5. Exit\nEnter your
choice:");
choice = Integer.parseInt(input);

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;
}
}

// Check if the list is empty


private boolean isEmpty() {
return head == null;
}

// Insert a new node at the end


public void insert(int value) {
Node newNode = new Node(value);
if (isEmpty()) {
head = newNode;
} else {
Node lastNode = getLastNode(); //
Get the last node in one step
lastNode.next = newNode;
}
}

// Traverse the list and return the last


node
private Node getLastNode() {
Node currentNode = head;
while (currentNode.next != null) {
currentNode = currentNode.next;
}
OPTIMIZED VERSION:
return currentNode;
import javax.swing.JOptionPane;
}

public class OptimizedLinkedList {


// Traverse the list and return the list as a
Node head; // Start of the list (head) string
public void traverse() {

// Inner class to represent a Node if (isEmpty()) {

static class Node {


JOptionPane.showMessageDialog(null, "The
int data; list is empty.");
Node next; return;
}
Node(int value) {
JOptionPane.showMessageDialog(null, while (currentNode != null) {
"Linked List: " + getListAsString());
if (currentNode.data == value) {
}
return position;
}
// Helper function to convert the list into a
currentNode = currentNode.next;
string
position++;
private String getListAsString() {
}
Node currentNode = head;
return -1; // Not found
StringBuilder listContent = new
StringBuilder(); }
while (currentNode != null) {

// Delete the first occurrence of a value


listContent.append(currentNode.data).appen
d(" "); public void delete(int value) {

currentNode = currentNode.next; if (isEmpty()) {

}
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) {

// Find the node to delete


JOptionPane.showMessageDialog(null, value
+ " found at position " + position); boolean isDeleted = deleteNode(value);

} else { if (isDeleted) {

JOptionPane.showMessageDialog(null, value JOptionPane.showMessageDialog(null, value


+ " not found."); + " deleted.");

} } 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.");

public static void main(String[] args) { break;

OptimizedLinkedList list = new default:


OptimizedLinkedList();
int choice; JOptionPane.showMessageDialog(null,
"Invalid choice. Please try again.");
}
do {
} while (choice != 5);
String input =
JOptionPane.showInputDialog( }

"1. Insert\n2. Traverse\n3. }


Search\n4. Delete\n5. Exit\nEnter your
choice:");
choice = Integer.parseInt(input);

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);
}

// Delete a node with a given value


public void delete(int data) {
if (head == null) {

JOptionPane.showMessageDialog(null, "List is
DOUBLY STUPID NI SIYAAA empty");

import javax.swing.JOptionPane; return;


}

class DoublyLinkedList {
Node current = head;

class Node {
int data; // Find the node to be deleted

Node prev; while (current != null && current.data !


= data) {
Node next;
current = current.next;
}
Node(int d) {
data = d;
if (current == null) {
prev = null;
next = null; JOptionPane.showMessageDialog(null,
} "Element not found");

} return;
}

private Node head = null;


if (current == head) {

// Insert a new node at the end of the list head = current.next;

public void insert(int data) { }

Node newNode = new Node(data);


if (head == null) { if (current.next != null) {

head = newNode; current.next.prev = current.prev;

} 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.");
}
}
}

public static void main(String[] args) {


DoublyLinkedList list = new
DoublyLinkedList();
list.menu();
}
}

You might also like