Guest User

Untitled

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