user18588015
user18588015

Reputation:

size of Linkedlist Java

    public class Ledger {
    
      public Payment head;
      public int id;
      public Ledger next;    
      /**
       * Default constructor
       * 
       * You may modify this constructor if you need to (e.g.
       * if you want to initialise extra attributes in the class)
       */
     
      public Ledger() {
        head = null;
        id = 0;
        next = null;      
        // add initialisation of added attributes, if needed
        
      }
       // Assign an id to the current Ledger
      public void setId(int id) {
        this.id = id;
      }
    
      /**
       * 
       * @return the number of payments in the Ledger
       */
      public int size() {
        Payment curr =head;
        int count=0;
         while(curr!=null) {
           curr=curr.next;
           count++;
        
         }
         return count;
      }

public void addPayment(Payment payment) {

// if Payment is null, do nothing
Payment head=payment ;
Payment temp=head;
if(head!=null) {
while(temp.next!=null) {  
  temp=temp.next;
}
temp.next= new Payment();
}

}

Hi I just learnt Linkedlist recently and trying to figure out the size of the linkedlist, somehow it wouldn't pass the test case for some reason. Can anyone point out what I did wrong in the size() method?

I have put up the addPayment method as well. Is it maybe I need to edit the constructor? This is where I am getting confused at.

Please see below for the test case.

public void testLedgerSize() {

    assertTimeoutPreemptively(Duration.ofMillis(1000), () -> {

      String[] fromPerson = { "Eren", "Mikasa", "a", "c", "Armin", "Jean", "Levi", "Sakura", "Naruto", "Erwin",
          "Thomas",
          "Sasuke", "Ichigo", "Luffy", "Scarlet"
      };

      String[] toPerson = { "Levi", "a", "Mikasa", "c", "Jean", "Armin", "Sakura", "Levi", "Sasuke", "Erwin", "Scarlet",
          "Naruto", "Rukia", "Ichigo", "Thomas"
      };

      double[] payment = { 10.0, 100.1, 21.0, 55.5, 29.9, 99.9, 100.0, 29.99, 30.0, 24.95, 35.0, 34.49, 99.95, 50,
          400 };

      // String[] registeredPeople = {"Eren", "Levi", "Mikasa", "Armin", "Erwin",
      // "Jean", "Annie", "Reiner", "Hanji", "Sasha"};

      // Case 1: empty Ledger
      Ledger empty = new Ledger();
      assertEquals(0, empty.size());

      // Case 2: Non empty Ledger size computed accurately
      Ledger nonEmpty = new Ledger();
      for (int i = 0; i < fromPerson.length; i++) {
        nonEmpty.addPayment(new Payment(fromPerson[i], toPerson[i], payment[i], null));
      }

      assertEquals(toPerson.length, nonEmpty.size());

      // Case 3: Ledger with one Payment only
      Ledger singlePayment = new Ledger();
      singlePayment.addPayment(new Payment("Eren", "Levi", 100, null));

      assertEquals(1, singlePayment.size());

    });
    currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
  }

Upvotes: 0

Views: 216

Answers (1)

Ahmad Mtera
Ahmad Mtera

Reputation: 158

You have the attributes part mixed up. In a LinkedList, you need a class for the LinkedList (called Ledger in your case), and this class has to contain a pointer of the node type (called Payment in your case). The required classes will look like this:

  1. Ledger class (LinkedList):

    public class Ledger {
        public Payment head;
        public int id;
    
        /**
         * Default constructor
         * <p>
         * You may modify this constructor if you need to (e.g.
         * if you want to initialise extra attributes in the class)
         */
        public Ledger() {
        head = null;
        id = 0;
        // add initialisation of added attributes, if needed
        }
    
        // Assign an id to the current Ledger
        public void setId(int id) {
            this.id = id;
        }
    
        /**
         * @return the number of payments in the Ledger
         */
        public int size() {
            Payment curr = head;
            int count = 0;
            while (curr != null) {
                curr = curr.next;
                count++;
            }
            return count;
        }
    }
    
  2. Payment class (Node) (PS: I wrote this since you didn't include it in your post – but it should work well as a skeleton to yours):

    public class Payment <T> {
        private T data;
        private Payment<T> next;
    
        public Payment(T data) {
            this.data = data;
        }
    
        public T getData() {
            return data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    
        public Payment<T> getNext() {
            return next;
        }
    
        public void setNext(Payment<T> next) {
            this.next = next;
        }
    
    }
    

Upvotes: 1

Related Questions