Question

I want to create a linked list then populate it with 1-100. After that I print out all the even numbers without actually removing the odd numbers from the list, then print out the even numbers again, but double them. After these things, I remove the odd numbers from the linked list and print out the list. The last step that I have mentioned is where I am hung up. Everything else works fine, just my remove method removes all the odd numbers except 1. In my main method I use an if statement that says if the number contained in the node % 2 equals zero, remove the node. It works for every node except the first one. Thank you guys for any help you can give me. My code follows.

import java.util.*;

/*
 * My ListNode class
 */
class ListNode<Integer> {
private Integer item;
private ListNode<Integer> next;

public ListNode(Integer item) {
    this.item = item;
    next = null;
}

public ListNode(Integer item, ListNode<Integer> next) {
    this.item = item;
    this.next = next;
}

public Integer getItem() {
    return item;
}

public ListNode<Integer> getNext() {
    return next;
}

public void setItem(Integer item) {
    this.item = item;
}

public void setNext(ListNode<Integer> next) {
    this.next = next;

}   
}

/*
 * My LinkedList class
 */
class LinkedList<Integer> {
public ListNode<Integer> front;

public LinkedList() {
    front = null;
}

public boolean isEmpty() {
    return front == null;
}

 public boolean contains(int target) {
     for (ListNode<Integer> node = front;
          node != null;
          node = node.getNext()) {
         if (node.getItem().equals(target)) {
             return true;
         }
     }
     return false;
 }

 public int size() {
     int count = 0;
     for (ListNode<Integer> node = front;
          node != null;
          node = node.getNext()) {
         count++;
     }
     return count;
 }

 public String toString() {
     String result = "( ";
     for (ListNode<Integer> node = front;
          node != null;
          node = node.getNext()) {
         result += node.getItem() + " ";
     }
     return result + ")";
 }

 public Integer get(int index) {
     ListNode<Integer> node = front;
     for (int i = 0; i < index; i++) {
         node = node.getNext();
     }
     return node.getItem();
 }

 public void set(int index, Integer target) {
     ListNode<Integer> node = front;
     for (int i = 0; i < index; i++) {
         node = node.getNext();
     }
     node.setItem(target);
 }

 public void add(int index, int target) {
     if (isEmpty()) {
         front = new ListNode(target);
     } else {
         ListNode last = front;
         while (last.getNext() != null) {
             last = last.getNext();
         }
         last.setNext(new ListNode(target));
     }
 }

 public Integer remove(int index) {
     ListNode<Integer> node = front;
     ListNode<Integer> prev = front;
     for (int i = 0; i < index; i++) {
         prev = node;
         node = node.getNext();
 }
     prev.setNext(node.getNext());
     return node.getItem();
}
 }
public class LinkedListTest {
//interface Removal {
    //Integer remove (Integer item);
//}
public static void main(String[] args) {
    LinkedList<Integer> list = new LinkedList<Integer>();

    System.out.println(list);

    System.out.println("The list size is " + list.size());      
    System.out.println();

    /*
     * This adds the numbers 1 through 100 to a LinkedList
     */
    for (int i = 1; i <= 100; i++)
        list.add(0, i);

    System.out.println(list);


    System.out.println("The list size is " + list.size());
    System.out.println();

    /*
     * This prints out only even numbers by excluding indexes that are even,
     * because all the even numbers are held in the odd numbered indexes, thus 
     * index 0 is 1 but index 1 is 2, index 3 is 4
     */
    for (int i = 0; i < list.size(); i++) 
        if (i % 2 == 1) {
            System.out.print(list.get(i) + " ");
        }

    System.out.println();

    System.out.println("The list size is " + list.size());
    System.out.println();

    /*
     * This doubles even numbers                    
     */
    for (int i = 0; i < list.size(); i++) 
        if (i % 2 == 1) {
            int result = list.get(i) * 2;
        System.out.print(result + " ");
        }
    System.out.println();
    System.out.println("The list size is " + list.size());
    System.out.println();

    for (int i = 0; i < list.size(); i++) 
            if (list.get(i) % 2 == 1) {
                list.remove(i);
            }

    System.out.print(list);
    System.out.println();
    System.out.println("The list size is " + list.size());
    System.out.println();
    /*
     * These contain methods only work for the first list created
     */
    System.out.println("Does the list contain 32? " + list.contains(32));

    System.out.println("Does the list contain 33? " + list.contains(33));

}   
}
Was it helpful?

Solution

Your remove method doesn't actually work when value of index is 0.. Because both your node and prev are initialized to front and the loop is not being executed because the i<index condition is false. So you have to add another condition for the case of index=0 Adding these lines at the starting of your remove method solves the problem..

if(index==0){
    ListNode<integer>temp=front;
    front=front.getNext();
    return temp.getItem();
}

hope it helped...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top