|
| 1 | +import java.util.LinkedList; |
| 2 | +public class UnboundedDeque implements InstructuresDeque |
| 3 | +{ |
| 4 | + private int size = 0; |
| 5 | + private Node sentinel; |
| 6 | + |
| 7 | + public class Node |
| 8 | + { |
| 9 | + private Object element; |
| 10 | + private Node prev; |
| 11 | + private Node next; |
| 12 | + |
| 13 | + public Node(Object e) |
| 14 | + { |
| 15 | + this.element = e; |
| 16 | + size++; |
| 17 | + this.prev = this; |
| 18 | + this.next = this; |
| 19 | + } |
| 20 | + /*public String toString() |
| 21 | + { |
| 22 | + if(this.element==null) |
| 23 | + { |
| 24 | + return ""; |
| 25 | + } |
| 26 | + else |
| 27 | + { |
| 28 | + return " "+this.element; |
| 29 | + } |
| 30 | + }*/ |
| 31 | + } |
| 32 | + |
| 33 | + public UnboundedDeque() |
| 34 | + { |
| 35 | + this.sentinel = new Node(null); |
| 36 | + } |
| 37 | + |
| 38 | + public int size() |
| 39 | + { |
| 40 | + return this.size-1; |
| 41 | + } |
| 42 | + |
| 43 | + public boolean isEmpty() |
| 44 | + { |
| 45 | + if(this.size-1==0) |
| 46 | + { |
| 47 | + return true; |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + public void addTop(Object element) |
| 58 | + { |
| 59 | + //if(this.sentinel.next!=this.sentinel) |
| 60 | + //{ |
| 61 | + Node newNode = new Node(element); |
| 62 | + Node xnd = this.sentinel.next; |
| 63 | + newNode.prev = this.sentinel; |
| 64 | + newNode.next = xnd; |
| 65 | + xnd.prev = newNode; |
| 66 | + this.sentinel.next = newNode; |
| 67 | + //} |
| 68 | + /*else |
| 69 | + { |
| 70 | + Node newNode = new Node(element); |
| 71 | + this.sentinel.next = newNode; |
| 72 | + this.sentinel.prev = newNode; |
| 73 | + newNode.prev = this.sentinel; |
| 74 | + newNode.next = this.sentinel; |
| 75 | + }*/ |
| 76 | + } |
| 77 | + public void addBottom(Object element) |
| 78 | + { |
| 79 | + Node newNode = new Node(element); |
| 80 | + Node xnd = this.sentinel.prev; |
| 81 | + newNode.prev = xnd; |
| 82 | + newNode.next = this.sentinel; |
| 83 | + xnd.next = newNode; |
| 84 | + this.sentinel.prev = newNode; |
| 85 | + } |
| 86 | + /*public String toString() |
| 87 | + { |
| 88 | + String output = ""; |
| 89 | + Node current = this.sentinel.next; |
| 90 | + while(current!=this.sentinel) |
| 91 | + { |
| 92 | + output = output + " " + current; |
| 93 | + current = current.next; |
| 94 | + } |
| 95 | + return output; |
| 96 | + }*/ |
| 97 | + |
| 98 | + public Object removeTop() throws IllegalStateException |
| 99 | + { |
| 100 | + if(this.size==1) |
| 101 | + { |
| 102 | + IllegalStateException e = new IllegalStateException(); |
| 103 | + throw e; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + Node xnd = this.sentinel.next; |
| 108 | + this.sentinel.next = xnd.next; |
| 109 | + xnd.next.prev = this.sentinel; |
| 110 | + this.size--; |
| 111 | + return xnd.element; |
| 112 | + } |
| 113 | + } |
| 114 | + public Object removeBottom() throws IllegalStateException |
| 115 | + { |
| 116 | + if(this.size==1) |
| 117 | + { |
| 118 | + IllegalStateException e = new IllegalStateException(); |
| 119 | + throw e; |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + Node xnd = this.sentinel.prev; |
| 124 | + this.sentinel.prev = xnd.prev; |
| 125 | + xnd.prev.next = this.sentinel; |
| 126 | + this.size--; |
| 127 | + return xnd.element; |
| 128 | + } |
| 129 | + } |
| 130 | + public Object top() |
| 131 | + { |
| 132 | + if(this.size>1) |
| 133 | + { |
| 134 | + return this.sentinel.next.element; |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + return null; |
| 139 | + } |
| 140 | + } |
| 141 | + public Object bottom() |
| 142 | + { |
| 143 | + if(this.size>1) |
| 144 | + { |
| 145 | + return this.sentinel.prev.element; |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + return null; |
| 150 | + } |
| 151 | + } |
| 152 | + public static void main(String[] args) |
| 153 | + { |
| 154 | + //UnboundedDeque a = new UnboundedDeque(); |
| 155 | + //System.out.println(a.size()); |
| 156 | + //a.addTop("1231"); |
| 157 | + //a.addTop("123123"); |
| 158 | + //a.addBottom("12312asdasdasd"); |
| 159 | + //a.addBottom("121eadasderqfafasf"); |
| 160 | + //a.addTop("asdasscpioiqwe"); |
| 161 | + //System.out.println(a.bottom()); |
| 162 | + //System.out.println(a.top()); |
| 163 | + //System.out.println(a.removeTop()); |
| 164 | + //System.out.println(a.removeBottom()); |
| 165 | + |
| 166 | + //a.removeBottom(); |
| 167 | + //System.out.println(a); |
| 168 | + //System.out.println(a.size()); |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +} |
0 commit comments