From aef13be6b9092a00fb74bb47eb55c5a0ad8c8103 Mon Sep 17 00:00:00 2001 From: idenc Date: Sat, 20 Oct 2018 12:38:38 -0600 Subject: [PATCH] Bubble sort in Java using a Singly linked list --- SLLBubbleSort/SLL.java | 99 ++++++++++++++++++++++++++++++++ SLLBubbleSort/SLLBubbleSort.java | 24 ++++++++ SLLBubbleSort/SLLNode.java | 16 ++++++ 3 files changed, 139 insertions(+) create mode 100644 SLLBubbleSort/SLL.java create mode 100644 SLLBubbleSort/SLLBubbleSort.java create mode 100644 SLLBubbleSort/SLLNode.java diff --git a/SLLBubbleSort/SLL.java b/SLLBubbleSort/SLL.java new file mode 100644 index 0000000..9d0663b --- /dev/null +++ b/SLLBubbleSort/SLL.java @@ -0,0 +1,99 @@ +//************************** SLL.java ********************************* +// a generic singly linked list class + +public class SLL { + + private int size = 0; + + protected SLLNode head, tail; + public SLL() { + head = tail = null; + } + + public boolean isEmpty() { + return head == null; + } + + public void addToHead(T el) { + head = new SLLNode(el,head); + if (tail == null) + tail = head; + size++; + } + + public void addToTail(T el) { + if (!isEmpty()) { + tail.next = new SLLNode(el); + tail = tail.next; + } + else head = tail = new SLLNode(el); + + size++; + } + + public T deleteFromHead() { // delete the head and return its info; + if (isEmpty()) + return null; + T el = head.info; + if (head == tail) // if only one node on the list; + head = tail = null; + else head = head.next; + + size--; + return el; + } + + public T deleteFromTail() { // delete the tail and return its info; + if (isEmpty()) + return null; + T el = tail.info; + if (head == tail) // if only one node in the list; + head = tail = null; + else { // if more than one node in the list, + SLLNode tmp; // find the predecessor of tail; + for (tmp = head; tmp.next != tail; tmp = tmp.next); + tail = tmp; // the predecessor of tail becomes tail; + tail.next = null; + } + size--; + return el; + } + + public void delete(T el) { // delete the node with an element el; + if (!isEmpty()) { + if (head == tail && el.equals(head.info)) // if only one + head = tail = null; // node on the list; + else if (el.equals(head.info)) // if more than one node on the list; + head = head.next; // and el is in the head node; + else { // if more than one node in the list + SLLNode pred, tmp;// and el is in a nonhead node; + for (pred = head, tmp = head.next; + tmp != null && !tmp.info.equals(el); + pred = pred.next, tmp = tmp.next); + if (tmp != null) { // if el was found; + pred.next = tmp.next; + if (tmp == tail) // if el is in the last node; + tail = pred; + } + } + size--; + } + } + + public void printAll() { + for (SLLNode tmp = head; tmp != null; tmp = tmp.next) + System.out.print(tmp.info + " "); + } + + public boolean isInList(T el) { + SLLNode tmp; + for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next); + return tmp != null; + } + + public int getLength() { + return size; + } + + +} diff --git a/SLLBubbleSort/SLLBubbleSort.java b/SLLBubbleSort/SLLBubbleSort.java new file mode 100644 index 0000000..47f84aa --- /dev/null +++ b/SLLBubbleSort/SLLBubbleSort.java @@ -0,0 +1,24 @@ +/** + * Iden Craven + * CPSC 331 + */ + +public class SLLBubbleSort { + public static > void BubbleSort(SLL list) { + if (list.getLength() > 1) { + for (int i = 0; i < list.getLength(); i++) { + SLLNode curr = list.head; + SLLNode next = list.head.next; + while (next != null) { + if (curr.info.compareTo(next.info) > 0) { + T temp = curr.info; + curr.info = next.info; + next.info = temp; + } + curr = next; + next = next.next; + } + } + } + } +} diff --git a/SLLBubbleSort/SLLNode.java b/SLLBubbleSort/SLLNode.java new file mode 100644 index 0000000..0705632 --- /dev/null +++ b/SLLBubbleSort/SLLNode.java @@ -0,0 +1,16 @@ +//************************ SLLNode.java ******************************* +// node in a generic singly linked list class + +public class SLLNode { + public T info; + public SLLNode next; + public SLLNode() { + this(null,null); + } + public SLLNode(T el) { + this(el,null); + } + public SLLNode(T el, SLLNode ptr) { + info = el; next = ptr; + } +}