Advertisement
Guest User

SortedCircularSortedLL

a guest
Nov 24th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. package Lists;
  2.  
  3.  
  4. import java.util.Iterator;
  5. import java.util.NoSuchElementException;
  6.  
  7. import models.Contact;
  8.  
  9.  
  10.  
  11.  
  12. public class SortedCircularDoublyLL<E> implements List<E> {
  13.  
  14. private class Node {
  15. private E value;
  16. private Node next;
  17. private Node prev;
  18.  
  19. public E getValue() {
  20. return value;
  21. }
  22. public void setValue(E value) {
  23. this.value = value;
  24. }
  25. public Node getNext() {
  26. return next;
  27. }
  28. public void setNext(Node next) {
  29. this.next = next;
  30. }
  31. public Node getPrev() {
  32. return prev;
  33. }
  34. public void setPrev(Node prev) {
  35. this.prev = prev;
  36. }
  37.  
  38.  
  39. }
  40.  
  41. private class ListIterator implements Iterator<E>{
  42.  
  43. private Node nextNode;
  44.  
  45. public ListIterator(){
  46. this.nextNode = header.getNext();
  47. }
  48.  
  49. public ListIterator(int i){
  50. this.nextNode = header.getNext();
  51. for(int index=0;index<i;index++){
  52. this.nextNode=this.nextNode.getNext();
  53. }
  54. }
  55.  
  56.  
  57. public boolean hasNext() {
  58. return true;
  59. }
  60.  
  61.  
  62. public E next() {
  63. if (hasNext()){
  64. E result = this.nextNode.getValue();
  65. this.nextNode = this.nextNode.getNext();
  66. return result;
  67. }
  68. else {
  69. throw new NoSuchElementException();
  70. }
  71. }
  72.  
  73.  
  74. public void remove() {
  75. throw new UnsupportedOperationException();
  76.  
  77. }
  78.  
  79. }
  80.  
  81.  
  82. private class ReverseListIterator implements ReverseIterator<E>{
  83.  
  84. private Node prevNode;
  85.  
  86. public ReverseListIterator(){
  87. Node temp = null;
  88. if (!isEmpty()){
  89. for (temp = header.getNext(); temp.getNext() != header; temp = temp.getNext());
  90. prevNode = temp;
  91. }
  92. else {
  93. prevNode = header;
  94. }
  95. }
  96.  
  97. public boolean hasPrev() {
  98. return true;
  99. }
  100.  
  101.  
  102. public E prev() {
  103. if (hasPrev()){
  104. E result = prevNode.getValue();
  105. prevNode = prevNode.getPrev();
  106. return result;
  107. }
  108. else {
  109. throw new NoSuchElementException();
  110.  
  111. }
  112. }
  113.  
  114.  
  115. public void remove() {
  116. throw new UnsupportedOperationException();
  117.  
  118. }
  119.  
  120. }
  121. private Node header, last;
  122. private int currentSize;
  123.  
  124. public SortedCircularDoublyLL(){
  125. this.header = new Node();
  126. this.last = header;
  127. this.header.setNext(last);
  128. this.header.setPrev(last);
  129. this.currentSize = 0;
  130. }
  131.  
  132.  
  133. public Iterator<E> iterator() {
  134. return new ListIterator();
  135. }
  136.  
  137. public Iterator<E> iterator(int i) {
  138. return new ListIterator(i);
  139. }
  140.  
  141. public ReverseIterator<E> reverseIterator() {
  142. return new ReverseListIterator();
  143. }
  144.  
  145.  
  146. public void add(E obj) {
  147.  
  148. if (obj == null){
  149. throw new IllegalArgumentException("Parameter cannot be null.");
  150. }
  151.  
  152. Node temp = this.header.getNext();
  153. Node prevtemp= this.header;
  154.  
  155. if(this.currentSize==0){
  156. Node newNode=new Node();
  157. newNode.setValue(obj);
  158. this.header.setNext(newNode);
  159. this.header.setPrev(newNode);
  160. newNode.setNext(this.header);
  161. newNode.setPrev(this.header);
  162. }
  163. else if(temp.getValue() instanceof Contact && this.currentSize>0){
  164. while(temp!=this.header && ((Contact) obj).getLastName().compareTo(((Contact ) temp.getValue()).getLastName())>0){
  165. temp=temp.getNext();
  166. prevtemp=prevtemp.getNext();
  167. }
  168. if(temp==this.header){
  169. Node newNode = new Node();
  170. newNode.setValue(obj);
  171. newNode.setNext(this.header);
  172. newNode.setPrev(prevtemp);
  173. prevtemp.setNext(newNode);
  174. this.header.setPrev(newNode);
  175. }
  176. else{
  177. Node newNode = new Node();
  178. newNode.setValue(obj);
  179. newNode.setNext(temp);
  180. newNode.setPrev(prevtemp);
  181. prevtemp.setNext(newNode);
  182. temp.setPrev(newNode);
  183. }
  184.  
  185.  
  186. }
  187. else if(!(temp.getValue() instanceof Contact) & this.currentSize>0) {
  188. while(temp!=this.header && ((Comparable) obj).compareTo(temp.getValue()) > 0){
  189. temp=temp.getNext();
  190. prevtemp=prevtemp.getNext();
  191. }
  192. if(temp==this.header){
  193. Node newNode = new Node();
  194. newNode.setValue(obj);
  195. newNode.setNext(this.header);
  196. newNode.setPrev(prevtemp);
  197. prevtemp.setNext(newNode);
  198. this.header.setPrev(newNode);
  199. }
  200. else{
  201. Node newNode = new Node();
  202. newNode.setValue(obj);
  203. newNode.setNext(temp);
  204. newNode.setPrev(prevtemp);
  205. prevtemp.setNext(newNode);
  206. temp.setPrev(newNode);
  207. }
  208. }
  209. this.currentSize++;
  210. }
  211.  
  212.  
  213.  
  214. public boolean remove(E obj) {
  215. if (obj == null){
  216. throw new IllegalArgumentException("Parameter cannot be null.");
  217. }
  218. Node temp = null;
  219. for (temp = header.getNext(); temp != this.header; temp = temp.getNext()){
  220. if (temp.getValue().equals(obj)){
  221. // found first copy
  222. if (temp.getNext() != null){
  223. temp.getNext().setPrev(temp.getPrev());
  224. }
  225. temp.getPrev().setNext(temp.getNext());
  226. temp.setValue(null);
  227. temp.setNext(null);
  228. temp.setPrev(null);
  229. this.currentSize--;
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235.  
  236.  
  237. public boolean remove(int index) {
  238. if ((index < 0) || (index >= this.size())){
  239. throw new IndexOutOfBoundsException();
  240. }
  241.  
  242. Node temp = null;
  243. int counter = 0;
  244. for (temp = header.getNext(); counter < index; temp = temp.getNext(), counter++);
  245. if (temp.getNext() != null){
  246. // i am not at the end of list
  247. temp.getNext().setPrev(temp.getPrev()); // null if temp is the last one
  248. }
  249. temp.getPrev().setNext(temp.getNext());
  250. temp.setValue(null);
  251. temp.setNext(null);
  252. temp.setPrev(null);
  253. this.currentSize--;
  254. return true;
  255. }
  256.  
  257.  
  258. public int removeAll(E obj) {
  259. int counter = 0;
  260. while(this.remove(obj)){
  261. counter++;
  262. }
  263. return counter;
  264. }
  265.  
  266.  
  267. public E get(int index) {
  268. if ((index < 0) || (index >= this.size())){
  269. throw new IndexOutOfBoundsException();
  270. }
  271. else {
  272. int counter = 0;
  273. Node temp = null;
  274. for (temp = header.getNext(); counter < index; temp = temp.getNext(), counter++);
  275. return temp.getValue();
  276. }
  277. }
  278.  
  279.  
  280. public E set(int index, E obj) {
  281. if (obj == null){
  282. throw new IllegalArgumentException("Parameter cannot be null.");
  283. }
  284.  
  285. if ((index < 0) || (index >= this.size())){
  286. throw new IndexOutOfBoundsException();
  287. }
  288. else {
  289. int counter = 0;
  290. Node temp = null;
  291. for (temp = header.getNext(); counter < index; temp = temp.getNext(), counter++);
  292. E result = temp.getValue();
  293. temp.setValue(obj);
  294. return result;
  295. }
  296. }
  297.  
  298.  
  299. public E first() {
  300. if (this.isEmpty()){
  301. return null;
  302. }
  303. else {
  304. return header.getNext().getValue();
  305. }
  306. }
  307.  
  308.  
  309. public E last() {
  310. if (this.isEmpty()){
  311. return null;
  312. }
  313. else {
  314. Node temp = null;
  315. for (temp = header.getNext(); temp.getNext() != this.header; temp = temp.getNext());
  316. return temp.getValue();
  317. }
  318.  
  319.  
  320. }
  321.  
  322. public Node lastNode() {
  323. if (this.isEmpty()){
  324. return null;
  325. }
  326. else {
  327. Node temp = null;
  328. for (temp = header.getNext(); temp.getNext() != null; temp = temp.getNext());
  329. return temp;
  330. }
  331.  
  332.  
  333. }
  334.  
  335.  
  336. public int firstIndex(E obj) {
  337. if (obj == null){
  338. throw new IllegalArgumentException("Parameter cannot be null.");
  339. }
  340. else {
  341. int counter = 0;
  342. Node temp = null;
  343. for (temp = header.getNext(); temp != this.header; temp = temp.getNext(), counter++){
  344. if(temp!=null||temp!=header){
  345. if (temp.getValue().equals(obj)){
  346. return counter;
  347. }
  348. }
  349. }
  350. return -1;
  351. }
  352. }
  353.  
  354.  
  355. public int lastIndex(E obj) {
  356. if (obj == null){
  357. throw new IllegalArgumentException("Parameter cannot be null.");
  358. }
  359. else {
  360. int counter =0, lastSeen = -1;
  361. Node temp = null;
  362. for (temp = header.getNext(); temp != this.header; temp = temp.getNext(), counter++){
  363. if (temp.getValue().equals(obj)){
  364. lastSeen = counter;
  365. }
  366. }
  367. return lastSeen;
  368. }
  369. }
  370.  
  371.  
  372. public int size() {
  373. return this.currentSize;
  374. }
  375.  
  376.  
  377. public boolean isEmpty() {
  378. return this.size() == 0;
  379. }
  380.  
  381.  
  382. public boolean contains(E obj) {
  383. return this.firstIndex(obj) >= 0;
  384. }
  385.  
  386.  
  387. public void clear() {
  388. while (!this.isEmpty()){
  389. this.remove(0);
  390. }
  391.  
  392. }
  393.  
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement