Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions SLLBubbleSort/SLL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//************************** SLL.java *********************************
// a generic singly linked list class

public class SLL<T> {

private int size = 0;

protected SLLNode<T> head, tail;
public SLL() {
head = tail = null;
}

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

public void addToHead(T el) {
head = new SLLNode<T>(el,head);
if (tail == null)
tail = head;
size++;
}

public void addToTail(T el) {
if (!isEmpty()) {
tail.next = new SLLNode<T>(el);
tail = tail.next;
}
else head = tail = new SLLNode<T>(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<T> 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<T> 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<T> tmp = head; tmp != null; tmp = tmp.next)
System.out.print(tmp.info + " ");
}

public boolean isInList(T el) {
SLLNode<T> tmp;
for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next);
return tmp != null;
}

public int getLength() {
return size;
}


}
24 changes: 24 additions & 0 deletions SLLBubbleSort/SLLBubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Iden Craven
* CPSC 331
*/

public class SLLBubbleSort {
public static <T extends Comparable<? super T>> void BubbleSort(SLL<T> list) {
if (list.getLength() > 1) {
for (int i = 0; i < list.getLength(); i++) {
SLLNode<T> curr = list.head;
SLLNode<T> 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;
}
}
}
}
}
16 changes: 16 additions & 0 deletions SLLBubbleSort/SLLNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//************************ SLLNode.java *******************************
// node in a generic singly linked list class

public class SLLNode<T> {
public T info;
public SLLNode<T> next;
public SLLNode() {
this(null,null);
}
public SLLNode(T el) {
this(el,null);
}
public SLLNode(T el, SLLNode<T> ptr) {
info = el; next = ptr;
}
}