Question

In: Computer Science

1) Modify all methods that have used count, especially rewrite length method by traversing the list...

1) Modify all methods that have used count, especially rewrite length method by traversing the list and counting no. of elements.

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");
       }
   } // end main
} // end class xxxxxlnk

Solutions

Expert Solution

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);
       return (head == null);
   } // end isEmpty

   // length() returns number of list elements.
   public int length() {
       //return count;
      
       node curr = head; // set curr to head
       int l = 0; // sel l to 0
      
       // loop over the list
       while(curr != null)
       {
           l++; // increment l
           curr = curr.rlink; // move to the next node in the list
       }
      
       return l;
   } // 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) ) {
       if(p < 1 || p > length()+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);
       node curr = head; // set curr to head
       int pos = 0; // set current position to 0
      
       // loop over the list
       while(curr != null)
       {
           pos++; // increment pos by 1
           if(curr.data == x) // x found, return pos
               return pos;
           curr = curr.rlink;
       }
      
       return 0; // x not found
   } // 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");
       }
} // end main
} // end class xxxxxlnk


Related Solutions

Exercise 1 Modify the List class of Figure 21.3 in the textbook to include a method...
Exercise 1 Modify the List class of Figure 21.3 in the textbook to include a method that recursively searches a linked-list for a specified value. Ensure that the name of your method includes your last name. The method must return a reference to the value if it is found; otherwise, it must return null. Use your method in a test program that creates a list of integers. The program must prompt the user for a value to locate in the...
List the health care funding methods used in Canada. State the health care funding method used...
List the health care funding methods used in Canada. State the health care funding method used in your jurisdiction and describe the payroll implication, if any.
List the four health care funding methods used in Canada. State the health care funding method...
List the four health care funding methods used in Canada. State the health care funding method used in your jurisdiction and describe the payroll implication, if any.
List the four health care funding methods used in Canada. State the health care funding method...
List the four health care funding methods used in Canada. State the health care funding method used in your jurisdiction and describe the payroll implication, if any.
List and critically assess the methods and models that Multinational Enterprises (MNEs) have used to minimize...
List and critically assess the methods and models that Multinational Enterprises (MNEs) have used to minimize their tax obligations. Also, identify the primary techniques that MNEs have adopted to maximize their profit potential. Illustrate your answer with specific reasoning, argument and drawing on examples.
Language: Java I have written this code but not all methods are running. First method is...
Language: Java I have written this code but not all methods are running. First method is running fine but when I enter the file path, it is not reading it. Directions The input file must be read into an input array and data validated from the array. Input file format (500 records maximum per file): comma delimited text, should contain 6 fields: any row containing exactly 6 fields is considered to be invalid Purpose of this code is to :...
Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int)...
Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int) of the longest String in a List of Strings. For each recursive call, remove the first String from the list and return the greater of the length of the String you removed or the result of calling the method on the remainder of the list. The base case should be that the size of the list is 0. Write a driver to verify that...
which of the following methods can be used to estimate ending inventory when a physical count...
which of the following methods can be used to estimate ending inventory when a physical count is not possible: 1. lower of cost or market 2. FIFO 3. perpetual cost 4. gross profit Explain the answer in detail.
1. List and describe, then compare and contrast the four methods used in capital investment analysis....
1. List and describe, then compare and contrast the four methods used in capital investment analysis. 2. What is the implication regarding a project's return if the project has an investment cost of $400,000, a minimum rate of return of 12% and a positive NPV of $47,000? Answer the above questions. Answers must be at least 300 – 500 words in length
please modify the method public void addList(SinglyLinkedList<E> s) that add list to another with addlast() and...
please modify the method public void addList(SinglyLinkedList<E> s) that add list to another with addlast() and remove() methods without changing the parameter of addList method. ////////////////////////////////////////////////// public class SinglyLinkedList<E> {      private class Node<E> {            private E element;            private Node<E> next;            public Node(E e, Node<E> n) {                 element = e;                 next = n;            }            public E getElement() {                 return element;            }            public void setElement(E element) {                 this.element = element;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT