Skip to content

Instantly share code, notes, and snippets.

@katychuang
Last active January 10, 2020 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katychuang/bc619562c8a451ca30f3d29de9bc04dd to your computer and use it in GitHub Desktop.
Save katychuang/bc619562c8a451ca30f3d29de9bc04dd to your computer and use it in GitHub Desktop.
CISC 3130 Spring 2020 OER Code Snippets for https://libguides.brooklyn.cuny.edu/cisc3130/
/* Double-ended Linked List with Insert First, Insert Last */
class DoubleEndedLinkedList {
private Node first; // private link to the first element
private Node last;
// constructor sets first to null
public void DoubleEndedLinkedList(){
this.first = null;
this.last = null;
}
public boolean isEmpty(){
return first == null; // returns true if list is empty
}
// Added this method to insert a node to the end
public void insertLast(int i){
Node newNode = new Node(i);
if( isEmpty() ) // if empty list,
first = newNode; // first --> newNode
else
last.next = newNode; // old last --> newNode
last = newNode; // newNode <-- last
}
public Node deleteFirst() {
Node temp = first; // reset the first reference to first.next
if(first.next == null) // if only one item
last = null; // null <-- last
first = first.next; // first --> old next
return temp; // return deleted link
}
}
class LinkQueue {
private DoubleEndedLinkedList theList;
public LinkQueue() { // constructor
theList = new FirstLastList(); // make a nelist
}
public boolean isEmpty() { // true if queue is empty
return theList.isEmpty();
}
public void insert(int i) { // insert, rear of queue
theList.insertLast(j);
}
public int remove() { // remove, front of queue
return theList.deleteFirst();
}
public void displayQueue() {
System.out.print("Queue (front-->rear): ");
theList.displayList();
}
} // end LinkQueue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment