Using the class LinkedList below, write a program that uses it to store 100 rand
ID: 1807924 • Letter: U
Question
Using the class LinkedList below, write a program that uses it to store 100 random numbers. Again, consider that each of these random numbers is an integer in the interval [0, 200]. Write the program in such a way that there are no number duplicates. ____________________________________________________________________________________________________________________________________________________________________________________ //week 3 - lecture: LinkedList class publicclassLinkedList { privateNode first; publicLinkedList() { first =newNode(); } publicbooleanisEmpty() { return(first.getNext() ==null); } publicvoiddisplay() { Node current = first.getNext(); while(current !=null) { System.out.print(current.getInfo() +" "); current = current.getNext(); } System.out.println(); } publicbooleansearch(intx) { Node current = first.getNext(); while(current !=null) { if(current.getInfo() == x)returntrue; current = current.getNext(); } returnfalse; } publicvoidinsert(intx) { Node p =newNode(); p.setInfo(x); p.setNext(first.getNext()); first.setNext(p); } publicvoidremove(intx) { Node old = first.getNext(), p = first; //Finding the reference to the node before the one to be deleted booleanfound =false; while(old !=null&& !found) { if(old.getInfo() == x) found =true; else { p = old; old = p.getNext(); } } //if x is in the list, remove it. if(found) p.setNext(old.getNext()); } // PART A publicString toString() { if(this.isEmpty())return"The list is empty, no items to display;"; else { Node current = first.getNext(); String str =""; while(current !=null) { str = str + (current.getInfo() +" ");//add element to the string current = current.getNext();//move to the next element } returnstr; } } // PART B publicintlength(){ if(first==null) return0; Node temp = first.getNext();//Grabs the next node after temp intcount =1;//count is the size of the list, its 1 to start because first exists while(temp!=null){//temp will equal null when the linked list is count+=1;//we want to increment the count as we find more objects temp = temp.getNext();//temp now equals the next node and the loop continues until temp is null (which happens at end of list) } returncount; }//end length // PART C publicvoidclear(){ if(this.length()==0)//if its already empty return; else{ Node temp = first.getNext();//saves a reference to first.next first.Next =null;// clears the reference of first.next // for loop runs through every single node for(inti =0; ithis.length()){ System.err.println("Location entered is larger or equal to the length of the list, please enter a location smaller than the list"); return; } // again we let our get method we created earlier do the work this.get(location).info=item; }//end replace() // PART F publicintget(intlocation){ // its an error if the location is less than length, returns -1 if(locationExplanation / Answer
week 3 - lecture: LinkedList class 002 003 publicclassLinkedList 004 005 { 006 007 privateNode first; 008 009 010 011 publicLinkedList() 012 013 { 014 015 first = newNode(); 016 017 } 018 019 020 021 publicbooleanisEmpty() 022 023 { 024 025 return(first.getNext() == null); 026 027 } 028 029 030 031 publicvoiddisplay() 032 033 { 034 035 Node current = first.getNext(); 036 037 038 039 while(current != null) 040 041 { 042 043 System.out.print(current.getInfo() + " "); 044 045 current = current.getNext(); 046 047 } 048 049 050 051 System.out.println(); 052 053 } 054 055 056 057 publicbooleansearch(intx) 058 059 { 060 061 Node current = first.getNext(); 062 063 064 065 while(current != null) 066 067 { 068 069 if(current.getInfo() == x) returntrue; 070 071 current = current.getNext(); 072 073 } 074 075 076 077 returnfalse; 078 079 } 080 081 082 083 publicvoidinsert(intx) 084 085 { 086 087 Node p = newNode(); 088 089 090 091 p.setInfo(x); 092 093 p.setNext(first.getNext()); 094 095 096 097 first.setNext(p); 098 099 } 100 101 102 103 publicvoidremove(intx) 104 105 { 106 107 Node old = first.getNext(), 108 109 p = first; 110 111 112 113 //Finding the reference to the node before the one to be deleted 114 115 booleanfound = false; 116 117 while(old != null&& !found) 118 119 { 120 121 if(old.getInfo() == x) found = true; 122 123 else 124 125 { 126 127 p = old; 128 129 old = p.getNext(); 130 131 } 132 133 } 134 135 136 137 //if x is in the list, remove it. 138 139 if(found) p.setNext(old.getNext()); 140 } 141 142 143 // PART A 144 publicString toString() 145 { 146 if(this.isEmpty()) return"The list is empty, no items to display;"; 147 else 148 { 149 Node current = first.getNext(); 150 String str = ""; 151 152 while(current != null) 153 { 154 str = str + (current.getInfo() + " "); //add element to the string 155 current = current.getNext(); //move to the next element 156 } 157 returnstr; 158 } 159 160 } 161 162 // PART B 163 164 publicintlength(){ 165 if(first==null) 166 return0; 167 168 Node temp = first.getNext(); //Grabs the next node after temp 169 intcount = 1;//count is the size of the list, its 1 to start because first exists 170 while(temp!=null){ //temp will equal null when the linked list is 171 count+=1; //we want to increment the count as we find more objects 172 temp = temp.getNext(); //temp now equals the next node and the loop continues until temp is null (which happens at end of list) 173 } 174 175 returncount; 176 }//end length 177 178 // PART C 179 180 publicvoidclear(){ 181 182 if(this.length()==0) //if its already empty 183 return; 184 else{ 185 Node temp = first.getNext();//saves a reference to first.next 186 first.Next = null; // clears the reference of first.next 187 188 // for loop runs through every single node 189 for(inti = 0; ithis.length()){ 219 System.err.println("Location entered is larger or equal to the length of the list, please enter a location smaller than the list"); 220 return; 221 } 222 // again we let our get method we created earlier do the work 223 this.get(location).info=item; 224 }//end replace() 225 226 227 // PART F 228 229 publicintget(intlocation){ 230 231 // its an error if the location is less than length, returns -1 232 if(location
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.