Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package com.franklinhool.lab2;
  2.  
  3. import java.util.Collection;
  4.  
  5.  
  6.  
  7. /**
  8. * Course: EECS 114 Fall 2015
  9. *
  10. * First Name: Franklin Last Name: Hool Lab Section: 1A email address:
  11. * fhool@uci.edu
  12. *
  13. * Assignment: Lab 2 Filename : DoublyLinkedList
  14. *
  15. * I hereby certify that the contents of this file represent my own original
  16. * individual work. Nowhere herein is there code from any outside resources such
  17. * as another individual, a website, or publishings unless specifically
  18. * designated as permissible by the instructor or TA.
  19. *
  20. * @param <E>
  21. */
  22. public class DoublyLinkedList<T> {
  23.  
  24. Node head;
  25. Node tail;
  26.  
  27. class Node<T> {
  28. // TODO: add fields here
  29. private T t;
  30. private Node<T> next;
  31. private Node<T> prev;
  32.  
  33. public Node() {
  34. // TODO Auto-generated constructor stub
  35. this(null);
  36. }
  37.  
  38. public Node(T t) {
  39. this.t = t;
  40. }
  41. }
  42.  
  43. public <T> DoublyLinkedList() {
  44. this.head = null;
  45. this.tail = null;
  46. }
  47.  
  48. public DoublyLinkedList(Collection<? extends T> c) {
  49.  
  50. }
  51.  
  52. public void add(int index, T t) {
  53. Node<T> temp = new Node<>(t);
  54. if (index == 0) {
  55. temp.prev = null;
  56. temp.next = this.head;
  57. this.head.prev = temp;
  58. this.head = temp;
  59. } else if (index == -1) {
  60. temp.prev = this.tail;
  61. temp.next = null;
  62. this.tail.next = temp;
  63. this.tail = temp;
  64. }
  65.  
  66. }
  67.  
  68. public T remove(int index) {
  69.  
  70. if (index == 0) {
  71. this.head = this.head.next;
  72. } else if (index == -1) {
  73. this.tail = this.tail.prev;
  74. }
  75. return null;
  76. }
  77.  
  78. public T remove(Object o) {
  79. Node<T> temp = this.head;
  80. while ((temp = temp.next) != null) {
  81. if (temp.equals(o)) {
  82. temp.prev.next = temp.next;
  83. temp.next.prev = temp.prev;
  84. temp.next = null;
  85. temp.prev = null;
  86. break;
  87. }
  88.  
  89. }
  90. return null;
  91. }
  92.  
  93. /**
  94. * * Returns the element at the specified position. * * @param index the
  95. * position in which to look for. * @return the element at the given
  96. * position in the list. If the index is out of range (index < 0 || index
  97. * >=size())
  98. */
  99. public T get(int index) {
  100. Node<T> temp = this.head;
  101. int count=0;
  102. while ((temp = temp.next) != null) {
  103. if (index == count) {
  104. return temp.t;
  105. }
  106. count++;
  107. }
  108. return null;
  109. }
  110.  
  111. public int size() {
  112. Node<T> temp = this.head;
  113. int count=0;
  114. while ((temp = temp.next) != null) {
  115. count++;
  116. }
  117. return count;
  118. }
  119.  
  120. public boolean isEmpty() {
  121. if(this.head==null || this.tail==null)
  122. {
  123. return false;
  124. }
  125. else
  126. {
  127. return true;
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement