Array of data structure jobs
/* The programming job 2.1 adds a method named Getmax () to the Higharray class of the Higharray.java program (listing 2.3), which returns the value of the largest keyword in the array and returns-1 when the array is empty. Add some code to main () to use this method. You can assume that all the keywords are positive. 2.2 Modify the method in programming job 2.1 so that it not only returns the largest keyword, but also removes the keyword from the array. Name this method Removemax (). The Removemax () method in 2.3 programming job 2.2 provides a way to sort arrays by keyword values. Implement a sorting scheme that requires that the Higharray class not be modified, simply modify the code in main (). This method requires a second array, and the array data items are sorted in reverse order at the end of the sort. (This method is a variant of the selection sort in the 3rd chapter, "Simple Sorting".) ) 2.4 Modify the Orderedarray.java program (listing 2.4) to make the insert (), delete () and the Find () method use two-point lookups, as suggested in the book. 2.5 Add a merge () method to the Ordarray class of the Orderedarray.java program (listing 2.4) so that it can combine two ordered source arrays into an ordered array of objects. Add code in Main (), insert a random number into the two source array, call the merge () method, and display the result destination array. The number of data items for two source arrays may be different. In the algorithm, it is necessary to compare the keywords in the source array to select the smallest data item to copy to the destination array. Also consider how to solve the situation when the data item of one source array has been exhausted and the other has some data items left. 2.6 Add a Nodup () method to the Higharray class of the Higharray.java program (listing 2.3) so that it can delete all duplicate data items in the array. That is, if the keyword for three data items in the array is the 17,nodup () method, the two are removed. You do not have to consider maintaining the order of data items. One way to do this is to first compare each data item with other data items and overwrite the duplicates with null (or a special value that is not used in the real keyword). Then all the null is deleted, And, of course, reduce the size of the array. */
Packagedata structure operation; Public classHigharray {Private Long[] A; Private intNelems; PublicHigharray (intmax) {a=New Long[Max]; Nelems= 0; } //The implementation of the lookup method Public BooleanFindLongSearchkey) { intJ; for(j=0;j<nelems;j++) { if(A[j] = =Searchkey) { Break; } } if(J = =Nelems) { return false; }Else{ return true; } } //Insert an implementation of an element Public voidInsertLongvalue) {A[nelems]=value; Nelems++; } //Delete an implementation of an element Public BooleanDeleteLongvalue) { intJ; for(j=0;j<nelems;j++) { if(Value = =A[j]) { Break; } } if(J = =Nelems) { return false; }Else{ for(intK = j;k<nelems;k++) {A[k]= A[k+1];////This is not a multi-shift. When k=nelems-1, a[nelems-1] = A[nelems]; At this time A[nelems] is empty} nelems--; return true; } } Public voiddisplay () { for(intj = 0; J < Nelems; J + +) //for each element,System.out.print (A[j] + "");//Display ItSystem.out.println (""); } //Job 2.1 Public LongGetmax () {Longmax =-1;//the value of the largest element for(intj=0;j<nelems;j++) { if(A[j] >max) {Max=A[j]; } } returnMax; } //Job 2.2 Public LongRemovemax () {Longmax =-1;//the value of the largest element intindex =-1;//index number of the largest element for(intj=0;j<nelems;j++) { if(A[j] >max) {Max=A[j]; Index=J; } } if(Index! =-1)//find the value of the largest element { for(inti = index+1;i<nelems;i++) {A[i-1] =A[i]; } nelems--; } returnMax; } /*** Add a Nodup () method to the Higharray class of the Higharray.java program (listing 2.3) so that it can delete all duplicate data items in the array. That is, if the keyword for three data items in the array is the 17,nodup () method, the two are removed. You do not have to consider maintaining the order of data items. One way to do this is to first compare each data item with other data items and overwrite the duplicates with null (or a special value that is not used in the real keyword). Then remove all the null and, of course, reduce the size of the array. */ Public voidNodup () {intNULL =-1;//use-1 as a special value for(intj=0;j<nelems;j++) { for(inti=j+1;i<nelems;i++) { if(A[j]! = NULL && A[j] = =A[i]) {A[i]=NULL; } } } for(inti=0;i<Nelems;) { if(A[i] = = NULL)//Note Do not i++ after the move is complete, check the current position again is not null { for(intj = i+1;j<nelems;j++) {a[j-1] =A[j]; } nelems--; }Else{i++;//is not NULL, it is directly i++. } } } }--------------------------------------------------------------------------------- Packagedata structure operation;/*** Add a method named Getmax to the Higharray array * It returns the value of the largest keyword *@authorAdministrator **/ Public classDemo2 { Public Static voidMain (string[] args) {intmaxSize = 100; Higharray arr=NewHigharray (maxSize); Arr.insert (77);//Insert Ten ItemsArr.insert (99); Arr.insert (44); Arr.insert (55); Arr.insert (22); Arr.insert (88); Arr.insert (11); Arr.insert (00); Arr.insert (66); Arr.insert (33); Arr.display (); //Display Items intSearchkey = 35;//Search for Item if(Arr.find (Searchkey)) System.out.println ("Found" +Searchkey); ElseSystem.out.println ("Can ' t find" +Searchkey); Arr.delete (00);//Delete 3 ItemsArr.delete (55); Arr.delete (99); Arr.display (); //Display items again// ======================================================= //Programming Job 2.1 LongMax =Arr.getmax (); System.out.println ("Found Max is" +max); // ======================================================= //Programming Job 2.2Arr.removemax (); Arr.display (); // ======================================================= //Programming Job 2.3Higharray Sortedarr =NewHigharray (maxSize); inti = 0; Max=Arr.removemax (); while(max! =-1) {Sortedarr.insert (max); Max=Arr.removemax (); } System.out.println ("In reverse order:"); Sortedarr.display (); // ======================================================= Arr.insert (77);//Insert Ten ItemsArr.insert (99); Arr.insert (44); Arr.insert (55); Arr.insert (22); //Duplicate ValueArr.insert (44); Arr.insert (77); Arr.insert (44); Arr.insert (66); Arr.insert (88); Arr.insert (11); Arr.insert (00); Arr.insert (66); Arr.insert (33); System.out.println ("After adding duplicate values:"); Arr.display (); //Arr.nodup (); Arr.nodup (); System.out.println ("After removing duplicate values:"); Arr.display (); // ======================================================= }}
Array of data structure jobs