public void bubblesortDescending() { //apply bubble sort priciples to a linked list for (MyLinkedListNode i = this.first; i != this.last; i = i.getNext()) { for (MyLinkedListNode j = i.getNext(); j != null; j = j.getNext()) { if (i.getData() < j.getData()) { var temp = i.getData(); i.setData(j.getData()); j.setData(temp); } } } }
//=============================================== //methods: public void addFirst(int data) { MyLinkedListNode node = new MyLinkedListNode(data); //check if empty if (count == 0) { first = last = node; } else { first.setPrevious(node); node.setNext(first); first = node; } count++; }
public void addLast(int data) { MyLinkedListNode node = new MyLinkedListNode(data); //check if empty if (count == 0) { first = last = node; } else { //connect last to node last.setNext(node); //connect node back to last node.setPrevious(last); //make the node the last one last = node; } count++; }
public int getData(int index) { //make sure that the index is valid if (index < 0 || index >= count) { throw new Exception("index out of bound"); } //sequence through the linkedlist until you reach the node //at the given index //return its data MyLinkedListNode temp = first; int i = 0; while (i < index) { temp = temp.getNext(); i++; } //temp should be referencing the node at the given index return(temp.getData()); }
// public int removeLast(){ // // } public int[] toArray() { //copy the entire list to an array an return it //create the array int[] array = new int[count]; //sequence through linkedlist from first node to last //use a temp to start at the first node MyLinkedListNode temp = first; int index = 0; while (temp != null) { //get data from node and copy it to array array[index] = temp.getData(); //increment index index++; //move to next node temp = temp.getNext(); } return(array); }
public void setFirstNode(MyLinkedListNode first) { this.first = first; }
public void setLastNode(MyLinkedListNode last) { this.last = last; }
public MyLinkedList() { first = null; last = null; count = 0; }
public void setPrevious(MyLinkedListNode previous) { this.previous = previous; }
public void setNext(MyLinkedListNode next) { this.next = next; }
//constructor public MyLinkedListNode(int data) { this.data = data; next = null; previous = null; }
MyLinkedList ReverseLinkedList(MyLinkedList myLinkedList) { MyLinkedListNode left = myLinkedList.getFirst(); MyLinkedListNode right = myLinkedList.getLast(); MyLinkedListNode tempNode = null; //Shuffle the right neighbours of left node to point to right. left.getNext().setPrevious(right); //Shuffle the left of neighbours of right node to point to left. right.getPrevious().setNext(left); // make sure that the old left neighbor of right node is now the recent left neighbour of left node // and the old left neighbour of left node is now the recent left neighbor of right node tempNode = left.getPrevious(); left.setPrevious(right.getPrevious()); right.setPrevious(tempNode); // make sure that the old right neighbor of right node is now the recent right neighbour of left node // and the old right neighbour of left node is now the recent right neighbor of right node tempNode = left.getNext(); left.setNext(right.getNext()); right.setNext(tempNode); //Arrange the first and last node as they are at the edge myLinkedList.setFirstNode(right); myLinkedList.setLastNode(left); //Get the first and last after reversing the left and right edge nodes. left = myLinkedList.getFirst(); right = myLinkedList.getLast(); //This is for the remaining inner nodes. for (int i = 1; i < myLinkedList.getCount() / 2; i++) { left = left.getNext(); if (left == right) //left and right have joined for even length of linked list. { break; } right = right.getPrevious(); if (left == right)//left and right have joined for odd length of linked list. { break; } //Shuffle the refences of neighbours of left to point to right. if (left.getNext() == right) // left and right about to join { left.getPrevious().setNext(right); right.getNext().setPrevious(left); left.setNext(right.getNext()); right.setPrevious(left.getPrevious()); left.setPrevious(right); right.setNext(left); break; } left.getNext().setPrevious(right); left.getPrevious().setNext(right); //Shuffle the refences of neighbours of right to point to left. right.getNext().setPrevious(left); right.getPrevious().setNext(left); // make sure that the old left neighbor of right node is now the recent left neighbour of left node // and the old left neighbour of left node is now the recent left neighbor of right node tempNode = left.getPrevious(); left.setPrevious(right.getPrevious()); right.setPrevious(tempNode); // make sure that the old right neighbor of right node is now the recent right neighbour of left node // and the old right neighbour of left node is now the recent right neighbor of right node tempNode = left.getNext(); left.setNext(right.getNext()); right.setNext(tempNode); tempNode = left; left = right; right = tempNode; } return(myLinkedList); }