fix the code below Do the following modifications in the xxxxxH2.java file: a) Replace public class xxxxxH2.java
Question:
fix the code below
Do the following modifications in the xxxxxH2.java file: a) Replace public class xxxxxH2.java implements lstinterface{ With public class xxxxxH2.java { b) Replace public void process(String fn){ With private void process(String fn){ c) Replace public void prtlst() { With private void prtlst() { d) Replace int x with String x in the file. e) Replace insertlst, searchlst and deletelst methods with the following methods. private int insertSorted(String x){ // insert x in a sorted list (position not given). private int searchSorted(String x){ // search sequentially for x in a sorted list and delete it if exist (position not given). private int deleteSorted(String x){ // search sequentially for x in a sorted list and return its position if exist. NOTE: Sequential search should stop when array element > x. f) comment line: 106 p = inf.nextInt(); // read position g) replace line: 148 k = insertlst(x, p); //insert x at position p with: k = insertSorted(x); h) replace line: 157 k = searchlst(x); // Search for x with: k = searchSorted(x);
i) replace line: 165 k = deletelst(p); // delete element at position p with: k = deleteSorted(x); 3- Input file for xxxxxH2.java is similar to input file for xxxxxH1.java by eliminating the array size from it (i.e., first input). Make sure you insert 15 or more strings and search 5 or more strings and delete 5 or more strings in the process method.
******** code
//lstinterface is an interface public class xxxxxlstlnk implements lstinterface { // xxxxxlstlnk class variables //head is a pointer to beginning of linked list private node head = null; // count is no. of elements in the list private int count = 0; //use prt for System.out to save typing PrintStream prt = System.out; // class node private class node{ // class node variables int data; node rlink; // class node constructor node(int x){ data = x; rlink = null; } // end class node constructor }// class node // insert x at position p, // for successful insertion: 1 <= p <= count+1 public int insertlst(int x, int p){ // Local variables node tmp, cur; int i; prt.printf(" \tInsert %4d at position %2d:", x, p); if (p < 1 || p > count + 1){ prt.printf(" Failed insertion."); return 0; // invalid insertion } // end if // Allocate space foe new element tmp = new node(x); // p == 1 Inserts x to front of list. // This is a special case where head changes if (p == 1){ tmp.rlink = head; head = tmp; } else { cur = head; //Find addrres of node before p for (i = 2; i < p; i ++, cur = cur.rlink);//for p=1 or p=2, loop will not execute // end for //insert node after cur node tmp.rlink = cur.rlink; cur.rlink = tmp; } // end if count ++; // increment no. of elements. prt.printf(" Successful insertion."); return 1; // successful insertion } // end insertlst // delete x at position p, for successful deletion: // list should not be empty and 1 <= p <= count public int deletelst(int p){ // Local variables node tmp, cur; prt.printf(" \t Delete element at position %2d:", p); if (head == null || p < 1 || p > count){ prt.printf(" Failed deletion."); return 0; // invalid deletion } // end if tmp = head; // p == 1 deletes front of list. // This is a special case where head changes if (p == 1){//Delete Front of List head = head.rlink; tmp.rlink = null; } else {//Find address of node before p cur = head; // for p=1 or p=2, loop will not execute for (int i = 2; i < p; i ++, cur = cur.rlink; // end for // Delete node after cur node tmp = cur.rlink; cur.rlink = tmp.rlink; tmp.rlink = null; // delete tmp; } // end if count --; // decrement no of list elements prt.printf(" Successful deletion."); return 1; // successful deletion } // end deletelst // sequential serach for x in the list // if successful return position of x in the // list otherwise return 0; public int searchlst(int x){ // Local variables node cur; // Complete the rest of the method return 0; // x is not found. } // end searchlst // print list elements formatted public void prtlst() { // Local variables node cur; prt.printf(" \tList contents(%d): ", count); for (cur = head; cur != null ; cur = cur.rlink) prt.printf("%4d, ", cur.data); // end for } // end prtlst // insert, delete and search in the list private void process(String fn){ // Local variables int j, nIns, nSrch, nDel, k, p, x; prt.print("\tLinked List implementation of list gets input file "+ "name from program argument, then reads:"+ " \tnIns: No. of elements to insert followed by elements to "+ " insert and their positions,"+ " \tnSrch: No. of elements to search, followed by element to "+ " search"+ " \t nDel: No. of elements to delete followed by position of "+ " elements to delete" + " \t\tTo compile: javac xxxxxlstlnk.java" + " \t\tTo execute: java xxxxxlstlnk inputfilename"); try{ // open input file Scanner inf = new Scanner(new File(fn)); //read no. of elements to insert nIns = inf.nextInt(); prt.printf(" \tInsert %2d elements in the list.", nIns); for(j = 1; j <= nIns; j++){ x = inf.nextInt(); // read x p = inf.nextInt(); // read position k = insertlst(x, p); //insert x at position p } // end for prtlst();//print list elements //read no. of elements to search in list nSrch = inf.nextInt(); prt.printf(" \tSearch for %d elements in list.", nSrch); for(j = 1; j <= nSrch; j++){ x = inf.nextInt(); // read x to serach k = searchlst(x); //delete position p }// end for //read no. of positions to delete from list nDel = inf.nextInt(); prt.printf(" \tDelete %d elements from list.", nDel); for(j = 1; j <= nDel; j++){ p = inf.nextInt(); // read position k = deletelst(p); //delete position p }// end for prtlst();//print linked list elements // close input file inf.close(); }catch (Exception e){prt.printf(" \tRead Error! %s", e);} // Total input: 1+nIns*2 +1+nSrch +1+nDel= nIns*2 + nSrch + nDel + 3 } // end process // main method public static void main(String args[]) throws Exception{ // declare variables int cnt = args.length; // get no. of atguments String fname; if (cnt < 1){ System.out.printf(" \tInvalid No. of aguments! EXIT. "); return; } // get input file name fname = args[0]; //make an instance of a class xxxxxlstlnk lst = new xxxxxlstlnk(); lst.process(fname); //Pleas replace G. Dastghaibyfard with your name in next line System.out.printf(" \tAuthor: G. Dastghaibyfard Date: %s ", java.time.LocalDate.now()); } // end main }// end class xxxxxlstlnk
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill