Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package myintnodedouble;
- public class MyIntNodeDouble {
- private int num;
- private MyIntNodeDouble next;
- private MyIntNodeDouble prev;
- public MyIntNodeDouble(int num) {
- this.num = num;
- this.next = null;
- this.prev = null;
- }
- public int getNum() { return this.num; }
- public void setNum(int n) { this.num = n; }
- public MyIntNodeDouble getNext() { return this.next; }
- public void setNext(MyIntNodeDouble next) { this.next = next; }
- public MyIntNodeDouble getPrevious() { return this.prev; }
- public void setPrev(MyIntNodeDouble previous) { this.prev = previous; }
- public void insertAfter(MyIntNodeDouble newNext) {
- MyIntNodeDouble oldNext = this.next;
- this.next = newNext;
- newNext.prev = this;
- newNext.next = oldNext;
- }
- public void insertBefore(MyIntNodeDouble newPrev) {
- MyIntNodeDouble oldPrev = this.prev;
- this.prev = newPrev;
- newPrev.prev = oldPrev;
- newPrev.next = this;
- }
- public void delete() {
- this.next.prev = this.prev;
- this.prev.next = this.next;
- }
- public MyIntNodeDouble getFirst() {
- MyIntNodeDouble current = this;
- while (current.prev != null) {
- current = current.prev;
- }
- return current;
- }
- public MyIntNodeDouble getLast() {
- MyIntNodeDouble current = this;
- while (current.next != null) {
- current = current.next;
- }
- return current;
- }
- public void addFirst(int n) {
- this.getFirst().prev = new MyIntNodeDouble(n);
- }
- public void addLast(int n) {
- this.getLast().next = new MyIntNodeDouble(n);
- }
- public int size() {
- MyIntNodeDouble current = this.getFirst();
- if (current == null){
- return 0;
- }
- int size = 1;
- while (current.next != null) {
- size++;
- current = current.next;
- }
- return size;
- }
- public int whereAmI() {
- MyIntNodeDouble current = this.getFirst();
- int pos = 0;
- while (current != this) {
- pos++;
- current = current.next;
- }
- return pos;
- }
- public void reverse() {
- MyIntNodeDouble first = this.getFirst();
- if (first == null) {
- return;
- }
- MyIntNodeDouble lprev = first;
- MyIntNodeDouble current = first.next;
- first.next = null;
- while (current != null) {
- MyIntNodeDouble lnext = current.next;
- current.next = lprev;
- lprev = current;
- current = lnext;
- }
- first = lprev;
- }
- @Override public String toString() {
- String returnString = "A list with " + this.size() + " integers:";
- MyIntNodeDouble current = this.getFirst();
- while (current != null) {
- returnString += " " + current.num;
- current = current.next;
- }
- return returnString;
- }
- public static void main(String[] args) {
- // Create Nodes
- MyIntNodeDouble n1 = new MyIntNodeDouble(1);
- MyIntNodeDouble n2 = new MyIntNodeDouble(2);
- MyIntNodeDouble n3 = new MyIntNodeDouble(3);
- MyIntNodeDouble n4 = new MyIntNodeDouble(4);
- MyIntNodeDouble n5 = new MyIntNodeDouble(5);
- MyIntNodeDouble n6 = new MyIntNodeDouble(6);
- // Link Nodes Together
- n1.setNext(n2); n2.setPrev(n1);
- n2.setNext(n3); n3.setPrev(n2);
- n3.setNext(n4); n4.setPrev(n3);
- n4.setNext(n5); n5.setPrev(n4);
- n5.setNext(n6); n6.setPrev(n5);
- // Test methods
- System.out.println(n1);
- System.out.println(n4.whereAmI());
- n4.delete();
- System.out.println(n1);
- n4.addLast(7);
- System.out.println(n1);
- n3.reverse();
- System.out.println(n3);
- }
- }
- /*
- *run:
- *A list with 5 integers: 1 2 3 4 5
- *3
- *A list with 4 integers: 1 2 3 5
- *A list with 5 integers: 1 2 3 5 6
- *A list with 0 integers:
- *BUILD SUCCESSFUL (total time: 0 seconds)
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement