Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Arrays;
- import java.util.Scanner;
- class DLLNode<E> {
- protected E element;
- protected DLLNode<E> pred, succ;
- public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
- this.element = elem;
- this.pred = pred;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class DLL<E> {
- private DLLNode<E> first, last;
- public void setFirst(DLLNode<E> first) {
- this.first = first;
- }
- public void setLast(DLLNode<E> last) {
- this.last = last;
- }
- public DLL() {
- // Construct an empty SLL
- this.first = null;
- this.last = null;
- }
- public void deleteList() {
- first = null;
- last = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- DLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public void insertFirst(E o) {
- DLLNode<E> ins = new DLLNode<E>(o, null, first);
- if (first == null)
- last = ins;
- else
- first.pred = ins;
- first = ins;
- }
- public void insertLast(E o) {
- if (first == null)
- insertFirst(o);
- else {
- DLLNode<E> ins = new DLLNode<E>(o, last, null);
- last.succ = ins;
- last = ins;
- }
- }
- public void insertAfter(E o, DLLNode<E> after) {
- if (after == last) {
- insertLast(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
- after.succ.pred = ins;
- after.succ = ins;
- }
- public void insertBefore(E o, DLLNode<E> before) {
- if (before == first) {
- insertFirst(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
- before.pred.succ = ins;
- before.pred = ins;
- }
- public E deleteFirst() {
- if (first != null) {
- DLLNode<E> tmp = first;
- first = first.succ;
- first.pred = null;
- if (first == null)
- last = null;
- return tmp.element;
- } else
- return null;
- }
- public E deleteLast() {
- if (first != null) {
- if (first.succ == null)
- return deleteFirst();
- else {
- DLLNode<E> tmp = last;
- last = last.pred;
- last.succ = null;
- return tmp.element;
- }
- }
- // else throw Exception
- return null;
- }
- public E delete(DLLNode<E> node) {
- if (node == first) {
- deleteFirst();
- return node.element;
- }
- if (node == last) {
- deleteLast();
- return node.element;
- }
- node.pred.succ = node.succ;
- node.succ.pred = node.pred;
- return node.element;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- DLLNode<E> tmp = first;
- ret += tmp + "<->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public DLLNode<E> getLast() {
- return last;
- }
- public void swapDLLNodes(DLLNode<E> i, DLLNode<E> j) {
- DLLNode<E> iFollowing = i.succ;
- DLLNode<E> jFollowing = j.succ;
- DLLNode<E> iPrevious = i.pred;
- DLLNode<E> jPrevious = j.pred;
- i.succ = jFollowing;
- j.succ = iFollowing;
- iPrevious.succ = j;
- jPrevious.succ = i;
- jPrevious.pred = i;
- iPrevious.pred = j;
- i.pred = jPrevious;
- j.pred = iPrevious;
- }
- public void swapAdjacentDLLNodes(DLLNode<E> i, DLLNode<E> j) {
- if (i == null || j == null) {
- return;
- }
- DLLNode<E> iFollowing = i.succ;
- DLLNode<E> jFollowing = j.succ;
- DLLNode<E> iPrevious = i.pred;
- DLLNode<E> jPrevious = j.pred;
- if (i == null || j == null) {
- return;
- }
- i.succ = jFollowing;
- j.succ = i;
- iPrevious.succ = j;
- i.succ.pred = i;
- j.pred = iPrevious;
- i.pred = j;
- }
- public void swapFirstAndAdjacentDLLNodes(DLLNode<E> firstNode, DLLNode<E> j) {
- if (firstNode == null || j == null) {
- return;
- }
- DLLNode<E> iFollowing = firstNode.succ;
- DLLNode<E> jFollowing = j.succ;
- DLLNode<E> iPrevious = firstNode.pred;
- DLLNode<E> jPrevious = j.pred;
- j.succ = firstNode;
- first = j;
- firstNode.succ = jFollowing;
- jFollowing.pred = firstNode;
- j.pred = null;
- firstNode.pred = j;
- }
- public void swapLastAndAdjacentDLLNodes(DLLNode<E> i, DLLNode<E> lastNode) {
- if (i == null || lastNode == null) {
- return;
- }
- DLLNode<E> iFollowing = i.succ;
- DLLNode<E> jFollowing = lastNode.succ;
- DLLNode<E> iPrevious = i.pred;
- DLLNode<E> jPrevious = lastNode.pred;
- i.succ = null;
- lastNode.succ = i;
- iPrevious.succ = lastNode;
- lastNode.pred = iPrevious;
- i.pred = lastNode;
- last = i;
- }
- public DLLNode<E> get(int index) {
- if (0 <= index && index < this.length()) {
- DLLNode<E> temp = this.getFirst();
- for (int i = 0; i < index; i++) {
- temp = temp.succ;
- }
- return temp;
- } else {
- return this.getFirst();
- }
- }
- }
- /* Changed the class name from BubbleSortDLL to BubbleSortLL */
- public class BubbleSortDLL {
- public static void bubbleSort(DLL<Integer> list) {
- int length = list.length();
- boolean sorted = false;
- for (int i = 0; i < length && !sorted; i++) {
- sorted = true;
- for (DLLNode<Integer> j = list.getFirst(); j.succ != null; j = j.succ) {
- if (j.element > j.succ.element) {
- if (j == list.getFirst()) {
- list.swapFirstAndAdjacentDLLNodes(j, j.succ);
- sorted = false;
- } else if (j.succ == list.getLast()) {
- list.swapLastAndAdjacentDLLNodes(j, j.succ);
- j = j.pred;
- sorted = false;
- } else {
- list.swapAdjacentDLLNodes(j, j.succ);
- sorted = false;
- }
- }
- }
- }
- /* Print the List */
- DLLNode<Integer> tmp = list.getFirst();
- while (tmp != null) {
- System.out.print(tmp.element);
- if (tmp.succ != null)
- System.out.print(" ");
- tmp = tmp.succ;
- }
- }
- public static void main(String[] args) throws IOException {
- DLL<Integer> list = new DLL<Integer>();
- Scanner scanner = new Scanner(System.in);
- int length = scanner.nextInt();
- scanner.nextLine(); // Finish off the first line
- String line = scanner.nextLine();
- String parts[] = line.split(" ");
- for (int i = 0; i < length; i++) {
- list.insertLast(Integer.parseInt(parts[i]));
- }
- bubbleSort(list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment