Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. //package application;
  2. //Assignment #: 10
  3. //Name:Raphael David
  4. //StudentID: 1214549752
  5. //Lab Lecture: MWF 9:40
  6. //A linked list is a sequence of nodes with efficient
  7. //element insertion and removal.
  8. //This class contains a subset of the methods of the
  9. //standard java.util.LinkedList class.
  10.  
  11.  
  12. import java.util.NoSuchElementException;
  13.  
  14. public class LinkedList
  15. {
  16. //nested class to represent a node
  17. private class Node
  18. {
  19. public Object data;
  20. public Node next;
  21. }
  22.  
  23. //only instance variable that points to the first node.
  24. private Node first;
  25.  
  26. // Constructs an empty linked list.
  27. public LinkedList()
  28. {
  29. first = null;
  30. }
  31.  
  32.  
  33. // Returns the first element in the linked list.
  34. public Object getFirst()
  35. {
  36. if (first == null)
  37. {
  38. NoSuchElementException ex
  39. = new NoSuchElementException();
  40. throw ex;
  41. }
  42. else
  43. return first.data;
  44. }
  45.  
  46. // Removes the first element in the linked list.
  47. public Object removeFirst()
  48. {
  49. if (first == null)
  50. {
  51. NoSuchElementException ex = new NoSuchElementException();
  52. throw ex;
  53. }
  54. else
  55. {
  56. Object element = first.data;
  57. first = first.next; //change the reference since it's removed.
  58. return element;
  59. }
  60. }
  61.  
  62. // Adds an element to the front of the linked list.
  63. public void addFirst(Object element)
  64. {
  65. //create a new node
  66. Node newNode = new Node();
  67. newNode.data = element;
  68. newNode.next = first;
  69. //change the first reference to the new node.
  70. first = newNode;
  71. }
  72.  
  73. // Returns an iterator for iterating through this list.
  74. public ListIterator listIterator()
  75. {
  76. return new LinkedListIterator();
  77. }
  78.  
  79.  
  80. /*********************************************************
  81. Add your methods here
  82.  
  83. *********************************************************/
  84. public String toString()
  85. {
  86. String result = "[ ";
  87. ListIterator li = this.listIterator();
  88.  
  89. while(li.hasNext() == true) {
  90. result += li.next() + " ";//adds the list of objects
  91.  
  92. }
  93. result += "]\n";
  94. return result;
  95. }
  96.  
  97. public boolean isEmpty() {
  98. ListIterator li = this.listIterator();
  99.  
  100. if(li.hasNext() == true) {// if the next element is not empty, return false
  101. return false;
  102.  
  103. }
  104. else {
  105. return true;
  106. }
  107.  
  108. }
  109.  
  110. public void addElementAt(Object element, int index) {
  111.  
  112. ListIterator li = this.listIterator();
  113.  
  114.  
  115. if(index < 0) {
  116. throw new IndexOutOfBoundsException();
  117. }
  118. for (int i = 0; i < index ; i ++) { // iterate until i reaches index
  119. if(li.hasNext()) {
  120. li.next();}
  121. else { // must be within bounds
  122. throw new IndexOutOfBoundsException();
  123. }
  124.  
  125. }
  126. li.add(element);
  127. }
  128.  
  129. public void removeElementAt(int index) {
  130.  
  131. ListIterator ab = this.listIterator();
  132. //get size
  133. int size = 0;
  134. while(ab.hasNext() != false) {
  135. size++;
  136. ab.next();
  137. }
  138. ListIterator li = this.listIterator();
  139. Object temp = new Object();
  140.  
  141.  
  142.  
  143. if (index != 0 && index > -1) {
  144. for (int i = 0; i <= index ; i ++) {
  145. // iterate until i reaches index
  146. if (li.hasNext() && index <= size) {
  147. li.next();
  148. }
  149. else {
  150. throw new IndexOutOfBoundsException();
  151.  
  152.  
  153. }
  154. }
  155.  
  156.  
  157. li.remove();
  158.  
  159.  
  160. }
  161. else {
  162. if(index > -1) {
  163. li = this.listIterator();
  164.  
  165. //if index = 0
  166. //removes head by changing first to
  167. //temp and moving first to next index
  168. this.removeFirst();
  169. li.next();
  170. }
  171. else {
  172. throw new IndexOutOfBoundsException();
  173. }
  174.  
  175. }
  176.  
  177. }
  178.  
  179. public String getElement(int index) {
  180. ListIterator li = this.listIterator();
  181.  
  182. //throwing errors
  183. // if index is less than 0
  184. if ( index < 0 ) {
  185. throw new IndexOutOfBoundsException();
  186. }
  187.  
  188. for (int i = 0; i < index ; i ++) { // iterate until i reaches index
  189. if(li.hasNext()) {
  190. li.next();
  191. }
  192. else {
  193. throw new IndexOutOfBoundsException();
  194.  
  195. }
  196. }
  197. return li.next().toString();
  198.  
  199. }
  200.  
  201. public String findSmallestAndRemove() { //this one i need help w
  202.  
  203. //if current node is smaller than the next, set equal to smallest
  204. //compare next node to smallest
  205. ListIterator li = this.listIterator();
  206.  
  207.  
  208. String smallest = "";
  209. String current = "";
  210. //String temp = "";
  211.  
  212. int size = 0;
  213. while(li.hasNext() != false) {
  214. size++;
  215. li.next();
  216. }
  217.  
  218.  
  219. if(isEmpty()) {
  220. return null;
  221. }
  222. ListIterator op = this.listIterator();
  223.  
  224. for (int i =0; i< size ; i++) {
  225. //if index is at zero then smallest is the current
  226. if(i == 0) {
  227. current = op.next().toString();
  228. smallest = current;
  229.  
  230. }
  231. //then keep making the current one the next one
  232. else {
  233. current = op.next().toString();
  234. }
  235.  
  236. //if the length of the current is less than the smallest, then make current smallest
  237. if(current.toString().compareTo(smallest.toString()) < 0 )
  238. {
  239. smallest = current;
  240. }
  241.  
  242. }
  243.  
  244. ListIterator ob = this.listIterator();
  245. //code to remove
  246. for (int i =0; i< size; i++) {
  247. current = ob.next().toString();
  248. //if curretn is the smallest, remove it
  249. if(current.compareTo(smallest) == 0)
  250. {
  251. ob.remove();
  252. }
  253.  
  254. }
  255.  
  256.  
  257. return smallest.toString();
  258.  
  259.  
  260. }
  261.  
  262. public int countHowManyStringsWithGivenLength(int length) {
  263. ListIterator li = this.listIterator();
  264. //get size
  265. int size = 0;
  266. while(li.hasNext() != false) {
  267. size++;
  268. li.next();
  269.  
  270.  
  271. }
  272. ListIterator counter = this.listIterator();
  273. //inc count if string length = parameter length
  274. int count = 0;
  275. for (int i =0; i < size; i++) {
  276. if(counter.next().toString().length() == length) {
  277. count++;
  278. }
  279. }
  280. return count;
  281. }
  282.  
  283. public void duplicateFirstSome(int howMany) {
  284.  
  285. //duplicates the howMany elements
  286. String temp = "";
  287. //need a temp variable
  288. //temp will be equal to the element in the array you want to copy
  289. //copy elemetn into temp and insert it at index
  290. ListIterator ab = this.listIterator();
  291. //get size
  292. int size = 0;
  293. while(ab.hasNext() != false) {
  294. size++;
  295. ab.next();
  296. }
  297.  
  298. ListIterator li = this.listIterator();
  299.  
  300. if(li.hasNext() && howMany <= size) {
  301.  
  302. for (int i = 0;i < howMany; i++) {
  303.  
  304. temp = li.next().toString();
  305. li.add(temp);
  306.  
  307. }
  308.  
  309.  
  310.  
  311. }
  312.  
  313. li = this.listIterator();
  314. if (howMany > size) {
  315. for (int i = 0;i < size; i++) {
  316.  
  317. temp = li.next().toString();
  318. li.add(temp);
  319.  
  320. }
  321. }
  322.  
  323.  
  324.  
  325. }
  326. //nested class to define its iterator
  327. private class LinkedListIterator implements ListIterator
  328. {
  329. private Node position; //current position
  330. private Node previous; //it is used for remove() method
  331.  
  332. // Constructs an iterator that points to the front
  333. // of the linked list.
  334.  
  335. public LinkedListIterator()
  336. {
  337. position = null;
  338. previous = null;
  339. }
  340.  
  341. // Tests if there is an element after the iterator position.
  342. public boolean hasNext()
  343. {
  344. if (position == null) //not traversed yet
  345. {
  346. if (first != null)
  347. return true;
  348. else
  349. return false;
  350. }
  351. else
  352. {
  353. if (position.next != null)
  354. return true;
  355. else
  356. return false;
  357. }
  358. }
  359.  
  360. // Moves the iterator past the next element, and returns
  361. // the traversed element's data.
  362. public Object next()
  363. {
  364. if (!hasNext())
  365. {
  366. NoSuchElementException ex = new NoSuchElementException();
  367. throw ex;
  368. }
  369. else
  370. {
  371. previous = position; // Remember for remove
  372.  
  373. if (position == null)
  374. position = first;
  375. else
  376. position = position.next;
  377.  
  378. return position.data;
  379. }
  380. }
  381.  
  382. // Adds an element after the iterator position
  383. // and moves the iterator past the inserted element.
  384. public void add(Object element)
  385. {
  386. if (position == null) //never traversed yet
  387. {
  388. addFirst(element);
  389. position = first;
  390. }
  391. else
  392. {
  393. //making a new node to add
  394. Node newNode = new Node();
  395. newNode.data = element;
  396. newNode.next = position.next;
  397. //change the link to insert the new node
  398. position.next = newNode;
  399. //move the position forward to the new node
  400. position = newNode;
  401. }
  402. //this means that we cannot call remove() right after add()
  403. previous = position;
  404. }
  405.  
  406. // Removes the last traversed element. This method may
  407. // only be called after a call to the next() method.
  408. public void remove()
  409. {
  410. if (previous == position) //not after next() is called
  411. {
  412. IllegalStateException ex = new IllegalStateException();
  413. throw ex;
  414. }
  415. else
  416. {
  417. if (position == first)
  418. {
  419. removeFirst();
  420. }
  421. else
  422. {
  423. previous.next = position.next; //removing
  424. }
  425. //stepping back
  426. //this also means that remove() cannot be called twice in a row.
  427. position = previous;
  428. }
  429. }
  430.  
  431. // Sets the last traversed element to a different value.
  432. public void set(Object element)
  433. {
  434. if (position == null)
  435. {
  436. NoSuchElementException ex = new NoSuchElementException();
  437. throw ex;
  438. }
  439. else
  440. position.data = element;
  441. }
  442. } //end of LinkedListIterator class
  443. } //end of LinkedList class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement