Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DList<E> {
- //declares our variables
- DListNode<E> head;
- DListNode<E> tail;
- private int length;
- public DList()
- {
- this.length = 0; //initializes length to zero.
- this.head = null; //initializes head to null.
- this.tail = null; //initializes tail to null.
- }
- public void addAtHead(E element) //method which adds an element to the Head node.
- {
- DListNode<E> node = new DListNode<E>(element, null, null);
- this.addAtHead(node); //creates a new node at the head.
- }
- public void addAtTail(E element) //method which adds an element to the tail node
- {
- DListNode<E> node = new DListNode<E>(element, null, null);
- this.addAtTail(node); //creates a new node at the tail
- }
- public void addAtHead(DListNode<E> node) //method which adds a new node at the head
- {
- if(head == null){ // if the value of head is null, sets the head and tail equal to node
- head = node;
- tail = node;
- }
- else if(head != null) {
- head.setPredecessor(node); // if head isn't null sets the new node as the head nodes predecessor.
- node.setSuccessor(head); //sets the new node as the current heads successor.
- head = node; //sets the new node equal to the head since it becomes the new head.
- }
- this.length++; //increments the size of the list.
- }
- public void addAtTail(DListNode<E> node) //method which adds a new node at the tail
- {
- if(tail == null){ // if the value of tail is null, sets the head and tail equal to new node
- head = node;
- tail = node;
- }
- else if(tail != null){
- tail.setSuccessor(node); //if the tail isn't null, sets the new node as the current tails successor.
- node.setPredecessor(head); //sets the new node as the current heads predecessor
- tail = node; // sets the tail equal to the new node
- }
- this.length++; // increments the length
- }
- public void removeFromHead() //method which removes node from head.
- {
- if (length == 1) { //if the length is 1, the tail and head are both null.
- head = null;
- tail = null;
- }
- else if(length >1)
- {
- DListNode<E> remove = head.getSuccessor(); //declares a new variable remove set equal to the heads successor.
- remove.setPredecessor(null); //sets removes predecessor to null.
- head = remove; //sets remove and head equal to each other.
- }
- length--; //decrements the length
- }
- public void removeFromTail()
- {
- if(length == 1){ //if the length is 1, the tail and head are both null.
- head = null;
- tail = null;
- }
- else if(length > 1){
- DListNode<E> remove = tail.getPredecessor(); //declares a new variable remove set equal to the tails predecessor.
- remove.setSuccessor(null); //sets the variable removes successor to null.
- tail = remove; //sets tail and remove equal to another.
- }
- this.length--; //decrements the length
- }
- public DListNode<E> getHead() //method which returns the head
- {
- return this.head;
- }
- public void setHead(DListNode<E> head) //method which sets the head to value head
- {
- this.head = head;
- }
- public int getLength() //method which gets the length
- {
- return this.length;
- }
- public void setLength(int length) //method which sets the length to value length
- {
- this.length = length;
- }
- public DListNode<E> getTail() //method which gets the tail
- {
- return this.tail;
- }
- public void setTail(DListNode<E> tail) //method which sets the tail to value tail.
- {
- this.tail = tail;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement