In: Computer Science
I'm getting an error for this code? it won't compile
import java.util.*;
import java.io.*;
public class Qup3 implements xxxxxlist {// implements
interface
// xxxxxlnk class variables
// head is a pointer to beginning of rlinked
list
private node head;
// no. of elements in the list
// private int count;
// xxxxxlnk class constructor
Qup3() {
head = null;
count = 0;
} // end Dersop3 class constructor
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;
} // class node constructor
} // end class node
// isEmpty() returns true if list is empty, false
otherwise.
public boolean isEmpty() {
return (count == 0);
} // end isEmpty
// length() returns number of list elements.
public int length() {
return count;
} // end length
// insert x at position p, for successful
insertion:
// list should not be full and 1 <= p <=
count+1
public int insert(int x, int p) {
int i;
prt.printf("\n Insert %4d at
position %2d:", x, p);
if (p < 1 || p > ( count + 1)
) {
return 0;
}
node tmp = new node(x);
// p == 1 Inserts x to front of
list,
// is a special case where head
changes
if (p == 1) {
tmp.rlink =
head;
head =
tmp;
} else {// traverse the list till
element before p
node current =
head;
// Find node
before p
for (i = 2; i
< p; i++, current = current.rlink)
; // end for
// insert node
after cur node
tmp.rlink =
current.rlink;
current.rlink =
tmp;
}
count++;
return 1; // successful
insertion
} // end insert
// delete x at position p, for successful
deletion:
// list should not be empty and 1 <= p <=
count
public int delete(int p) {
prt.printf("\n Delete element at
position %2d:", p);
if (isEmpty() || p < 1 || p >
length())
return 0; //
invalid deletion
int count = length();
node 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
node before p
node current =
head;
for (int i = 2;
i < p; i++, current = current.rlink; ) ;// end for
// Delete node
after current node
tmp =
current.rlink;
current.rlink =
tmp.rlink;
tmp.rlink =
null; // delete tmp;
}
count--;
return 1; // successful
deletion
} // end delete
// sequential serach for x in the list
// if successful return position of x in the
// list otherwise return 0;
public int searchx(int x) {
prt.printf("\n search for %4d:",
x);
// complete the rest
//.........
return 0;
} // end searchx
// print list elements formatted
public void printlist() {
prt.print("\n List contents:
");
for (node current = head; current
!= null; current = current.rlink)
prt.printf("%4d,
", current.data);
// end for
} // end printlist
public static void main(String args[]) throws
Exception {
int j, m, k, p, x, s;
try {
// open input
file
Scanner inf =
new Scanner(System.in);
// Create a List
of type Integer of size n
Qup3 lst = new
Qup3();
// read no.
of elements to insert
m =
inf.nextInt();
System.out.printf("\n\tInsert %2d elements in the list.", m);
for (j = 1; j
<= m; j++) {
x = inf.nextInt(); // read x
p = inf.nextInt(); // read position
s = lst.insert(x, p); // insert x at position
p
if (s == 1)
System.out.printf("
Successful insertion.");
else
System.out.printf(" %2d is
invalid position for insertion.", p);
} // end
for
lst.printlist();
// print linked list elements
// read no. of
elements to search in the list
m =
inf.nextInt();
System.out.printf("\n\tSearch for %d elements in the list.",
m);
for (j = 1; j
<= m; j++) {
x = inf.nextInt(); // read x
p = lst.searchx(x); // search for x
if (p > 0)
System.out.printf(" found at
position %d.", p);
else
System.out.printf(" is not
found.");
} // end
for
// read no. of positions to delete from
list
m =
inf.nextInt();
System.out.printf("\n\tDelete %d elements from list.", m);
for (j = 1; j
<= m; j++) {
p = inf.nextInt(); // read position
s = lst.delete(p); // delete position p
if (s == 1)
System.out.printf("
Successful deletion.");
else
System.out.printf(" %2d is
invalid position for deletion.", p);
} // end
for
lst.printlist();
// print array elements
inf.close(); //
close input file
} catch (Exception e) {
System.out.print("\nException " + e + "\n");
}
System.out.print("\tAuthor: G. Dastghaibyfard \tDate:
" +
java.time.LocalDate.now());} // end main
} // end class xxxxxlnk
Now, this code compiles fine.
There were 2 errors in the belowcode.
1.You have used the class variable 'count' in multiple places, but the declaration statement 'private int count;' was commented.
2.your class 'Qup3' implements the interface xxxxxlist. but xxxxxlist was not there in the code. If you have it with you, then it is fine. else please create the interface like below in a separate file with xxxxxlist as the file name.
public interface xxxxxlist {
}
If you are using a text editor, then it is better to put the interface and the class in the same folder.
These are the errors in the code that prevented it from compiling successfully. Now the above code compiles and runs fine.
import java.util.*;
import java.io.*;
public class Qup3 implements xxxxxlist {// implements interface
// xxxxxlnk class variables
// head is a pointer to beginning of rlinked list
private node head;
// no. of elements in the list
private int count;
// xxxxxlnk class constructor
Qup3() {
head = null;
count = 0;
} // end Dersop3 class constructor
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;
} // class node constructor
} // end class node
// isEmpty() returns true if list is empty, false otherwise.
public boolean isEmpty() {
return (count == 0);
} // end isEmpty
// length() returns number of list elements.
public int length() {
return count;
} // end length
// insert x at position p, for successful insertion:
// list should not be full and 1 <= p <= count+1
public int insert(int x, int p) {
int i;
prt.printf("\n Insert %4d at position %2d:", x, p);
if (p < 1 || p > ( count + 1) ) {
return 0;
}
node tmp = new node(x);
// p == 1 Inserts x to front of list,
// is a special case where head changes
if (p == 1) {
tmp.rlink = head;
head = tmp;
} else {// traverse the list till element before p
node current = head;
// Find node before p
for (i = 2; i < p; i++, current = current.rlink)
; // end for
// insert node after cur node
tmp.rlink = current.rlink;
current.rlink = tmp;
}
count++;
return 1; // successful insertion
} // end insert
// delete x at position p, for successful deletion:
// list should not be empty and 1 <= p <= count
public int delete(int p) {
prt.printf("\n Delete element at position %2d:", p);
if (isEmpty() || p < 1 || p > length())
return 0; // invalid deletion
int count = length();
node 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 node before p
node current = head;
for (int i = 2; i < p; i++, current = current.rlink) ;// end for
// Delete node after current node
tmp = current.rlink;
current.rlink = tmp.rlink;
tmp.rlink = null; // delete tmp;
}
count--;
return 1; // successful deletion
} // end delete
// sequential serach for x in the list
// if successful return position of x in the
// list otherwise return 0;
public int searchx(int x) {
prt.printf("\n search for %4d:", x);
// complete the rest
//.........
return 0;
} // end searchx
// print list elements formatted
public void printlist() {
prt.print("\n List contents: ");
for (node current = head; current != null; current = current.rlink)
prt.printf("%4d, ", current.data);
// end for
} // end printlist
public static void main(String args[]) throws Exception {
int j, m, k, p, x, s;
try {
// open input file
Scanner inf = new Scanner(System.in);
// Create a List of type Integer of size n
Qup3 lst = new Qup3();
// read no. of elements to insert
m = inf.nextInt();
System.out.printf("\n\tInsert %2d elements in the list.", m);
for (j = 1; j <= m; j++) {
x = inf.nextInt(); // read x
p = inf.nextInt(); // read position
s = lst.insert(x, p); // insert x at position p
if (s == 1)
System.out.printf(" Successful insertion.");
else
System.out.printf(" %2d is invalid position for insertion.", p);
} // end for
lst.printlist(); // print linked list elements
// read no. of elements to search in the list
m = inf.nextInt();
System.out.printf("\n\tSearch for %d elements in the list.", m);
for (j = 1; j <= m; j++) {
x = inf.nextInt(); // read x
p = lst.searchx(x); // search for x
if (p > 0)
System.out.printf(" found at position %d.", p);
else
System.out.printf(" is not found.");
} // end for
// read no. of positions to delete from list
m = inf.nextInt();
System.out.printf("\n\tDelete %d elements from list.", m);
for (j = 1; j <= m; j++) {
p = inf.nextInt(); // read position
s = lst.delete(p); // delete position p
if (s == 1)
System.out.printf(" Successful deletion.");
else
System.out.printf(" %2d is invalid position for deletion.", p);
} // end for
lst.printlist(); // print array elements
inf.close(); // close input file
} catch (Exception e) {
System.out.print("\nException " + e + "\n");
}
System.out.print("\tAuthor: G. Dastghaibyfard \tDate: " +
java.time.LocalDate.now());} // end main
} // end class xxxxxlnk