Question
a) Complete both insert and delete methods. If it works correctly 100 points each. b) Do not call the search method in delete method. c)
a) Complete both insert and delete methods. If it works correctly 100 points each.
b) Do not call the search method in delete method.
c) Make sure to write your name in main method.
d) DO NOT MODIFY PROCESS METHOD.
3- Create a proper input file with respect to process method to test all cases in both
insert and delete methods.
4- Compile and execute your program at the command prompt. Check your output, if it
is correct
//xxxxxH5.java is linked list implementation of lstinterface.
//Eliminate count form both insert and delete methods
// If works correctly, 200 points
import java.util.*;
import java.io.*;
//lstinterface is an interface
public class xxxxxH5 implements lstinterface {
// xxxxxH5 class variables
//head is a pointer to beginning of linked list
private node head = null;
//use prt for System.out to save typing
PrintStream prt = System.out;
// class node
private class node{
// class node variables
int data;
node rlink; //right link
// class node constructor
node(int x){
data = x;
rlink = null;
} // end class node constructor
}// class node
// insert x at position p,
// if successful return 1 otherwise return 0
public int insert(int x, int p){
prt.printf("\n\t\tInsert %5d at position %2d:", x, p);
// complete this method
return 0; // successful insertion
} // end insert
// delete x at position p
// if successful return 1 otherwise return 0
public int delete(int p){
prt.printf("\n\t\tDelete element at position %2d:", p);
// complete this method
return 0; // successful insertion
} // end delete
// sequential serach for x in the list
// if successful return its position otherwise return 0;
public int search(int x){
// Local variables
node cur;
prt.printf("\n\t\tSearch for %5d:", x);
// Complete the rest of the method
int pos = 0;
for (cur = head; cur != null ; cur = cur.rlink){
pos ++;
if (cur.data == x){
prt.printf(" Found at position %2d, ", pos);
return pos;
} // end if
} // end for
prt.printf(" Not Found.");
return 0; // x is not found.
} // end search
// print list elements formatted
public void printlst() {
// Local variables
node cur;
prt.printf("\n\tList contents: ");
for (cur = head; cur != null ; cur = cur.rlink)
prt.printf("%5d,", cur.data);
// end for
} // end printlst
// insert delete and search in the list
private void process(String fn){
// Local variables
int j, ins, del, srch, k, p;
int x;
prt.printf("\tLinked List implementation of int list without"+
//\n\tusing count, gets input file name from method argument, then
reads:"+
"\n\tinteger No. of elements to insert(ins)"+
" followed by elements to insert and their positions,"+
"\n\tinteger No. of elements to search(srch) followed by element to
search"+
"\n\tinteger No. of elements to delete(del) followed by position of"+
"elements to delete" +
"\n\t\tTo compile: javac xxxxxH5.java" +
"\n\t\tTo execute: java xxxxxH5 inputfilename");
try{
// open input file
Scanner inf = new Scanner(new File(fn));
//read no. of elements to insert
ins = inf.nextInt();
prt.printf("\n\tInsert %d elements in the list.", ins);
for(j = 1; j <= ins; j++){
x = inf.nextInt(); // read x
p = inf.nextInt(); // read position
k = insert(x, p); //insert x at position p
} // end for
printlst();//print list elements
//read no. of elements to search in list
srch = inf.nextInt();
prt.printf("\n\tSearch for %d elements in list.", srch);
for(j = 1; j <= srch; j++){
x = inf.nextInt(); // read x to serach
k = search(x); //delete position p
}// end for
//read no. of positions to delete from list
del = inf.nextInt();
prt.printf("\n\tDelete %d elements from list.", del);
for(j = 1; j <= del; j++){
p = inf.nextInt(); // read position
k = delete(p); //delete position p
}// end for
printlst();//print linked list elements
// close input file
inf.close();
}catch (Exception e){prt.printf("\n\tRead Error! %s", e);}
} // end process
// main method
public static void main(String args[]) throws Exception{
// declare variables
int cnt = args.length; // get no. of arguments
String fname;
if (cnt < 1){
System.out.printf("\n\n\tInvalid No. of arguments! EXIT.\n");
return;
}
// get input file name
fname = args[0];
//create an instance of a class
xxxxxH5 lst = new xxxxxH5();
lst.process(fname);
//Pleas replace ????????? with your name in next line
System.out.printf("\n\tAuthor: ????????? Date: %s\n",
java.time.LocalDate.now());
} // end main
}// end class xxxxxH5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started