Dear Experts,

I am new to the LinkedList concept and trying out coding a LinkedList problem which require me to do Insert newInteger based on the Index, Remove Index and Change oldInteger to newInteger based on Index.
Please kindly advice what i should code in my

public void insert(int index, int newInteger) {
        // implementation
    }

    public void remove(int index) {
        // implementation
    }
    public void change(int index, int newInteger) {
        // implementation
    }

------------------------------------------------------------------------------------
Sample input

4 8
1 5 2 3
4
I 1 2
R 2
C 1 10
I 4 2

Sample output

YES
NO
NO
NO
------------------------------------------------------------------------------------

Here is my full code:

import java.util.*;

//use ListNode to represent the integers.
class ListNode {
    protected Object element;
    protected ListNode next;

    public ListNode(Object item){
        element = item;
        next = null;
    }
    //declare constructors
    public ListNode(Object item, ListNode n){
        element = item;
        next = n;
    }
    //get the next list node
    public ListNode getNext(){
        return this.next;
    }
    public Object getElement(){
        return this.element();
    }
}

class LinkedList {
    // declare the member field
    protected ListNode head = null;
    protected int num_nodes = 0;

    public boolean isEmpty(){
        return (num_nodes == 0);
    }

    /* add: add a listNode to the linklist
     * 		PRE-Condition  :
     * 		POST-Condition :
     */
    public void add(ListNode listNode) {
        // implementation
        head = new ListNode (item, head);
        num_nodes ++;
    }

    /* insert: insert a newInteger at index
     * 		PRE-Condition  :
     * 		POST-Condition :
     */
    public void insert(int index, int newInteger) {
        // implementation
    }

    /* remove: remove the element at index
     * 		PRE-Condition  :
     * 		POST-Condition :
     */
    public void remove(int index) {
        // implementation
    }

    /* change: change the integer at index with newInteger
     * 		PRE-Condition  :
     * 		POST-Condition :
     */
    public void change(int index, int newInteger) {
        // implementation
    }

    /* isBetter: to compare between this linkedList with prevLinkedList
     * 		PRE-Condition  :
     * 		POST-Condition :
     */
    public String isBetter(LinkedList prevLinkedList) {
		// picking the integers in the newLinkedList and find out the difference between integers in the prevLinkedList
		// and sum the difference up.
		// if the sum of difference is more than K, return "YES"
		// else return "NO"
        int ans = 0;
        for (int i=0; i<prevLinkedList.size(); i ++){
            prevLinkedList.element - element = ans;
        }
        return "YES";
    }
}

public class Main {

    public static void main(String[] args) {
        // declare the necessary variables
        //int [] array;
        LinkedList <Integer>list = new LinkedList<Integer>();
        int N; //indicate the size of array
        int K; //indicate the citeria
        int Q; //indicate the no.of entry
        String operator; //indicate if it is for insert, remove or change
        int index; //indicate the first input of operator
        int newInteger; //indicate the second input of operator
        int sum;

        //declare a Scanner object to read input
        Scanner sc = new Scanner(System.in);

        N = sc.nextInt();
        K = sc.nextInt();

        list = new int [N];

        for (int i=0; i<list.size(); i++){
            list.add(sc.nextInt());
        }

        Q = sc.nextInt();

        for (int i=0; i<Q; i++){
            operator = sc.next();

            if(operator = "I"){
                index = sc.nexInt();
                newInteger = sc.nextInt();
                //int post = index -1;
            }
            else if (operator = "R"){
                index = sc.nextInt();
            }
            else if (operator = "C"){
                index = sc.nextInt();
                newIntegter = sc.nexInt();
                //int post = index -1;
            }
        }
        System.out.println(list.isBetter);
    }
}

Recommended Answers

All 3 Replies

Hmm... There are many places you need to update in your code. I will start with "main" method first.

In main() method:
1.You declared K but I don't see you use it anywhere? What criteria are you going to use?

2.In line 117, 122, and 125, you are assigning the string to "operator". The symbol of one equal (=) means assigning the value on the right to the variable on the left. What you really need to use is equality which is 2 equal signs (==) for testing equality. Better yet, you should use equals() method to compare if it is String class. You can check the class API document at http://download.oracle.com/javase/6/docs/api/java/lang/String.html.

3.In line 131, you are calling for variable isBetter of the ListNode class (and the variable does not exist!) rather than a method isBetter().

In LinkedList class definition:
4.In add() method line 41, you are supposed to either create a new list by assigning the new list to the "head" of the list or entail a new node to an existing list. At the moment, what you do is to assign the incoming node to the head. As a result, you keep replacing the "head" with a new node while you keep counting number of nodes. What you should do is somewhat like...

if (head==null) {  // the list is empty, start a new list
  head = new ListNode (item);  // 1st form of constructor, no need the next node
}
else {  // the list exists, go to the end of the list and entail it
  ListNode currentNode = head;  // think of it as 2 references pointing to the same node
  while (currentNode.next!=null) {  // if the next node is null, it is the end of the list
    currentNode = currentNode.next;  // traversal to the next node
  }
  // after the loop, the currentNode will hold the last node in the list
  // entail it with a new node
  currentNode.next = new ListNode(item);  // still 1st form of constructor
}
num_nodes ++;

5.You need to implement remove(), change(), etc...

In ListNode class definition:
6.This is an awkward implementation of a class. If the class uses "Object" as type, it is very ambiguous. I understand that you may sometimes want to use the class for different type, but using "Object" shouldn't be a way to go. You could use Generic type as <E> or something. I understand that in your case you may accept a String or a number to the class. And when you do the isBetter(), you will need to cast the type and convert to an integer. I believe that this is the class that you get from your instructor... Your instructor needs to learn the right way of programming concept. :(

Note: Your 2nd form of constructor may not be used at all in your assignment, so you may not need to care for. It is to create a new node and use the incoming node as its next in the list... Not really useful here.

I am new to the LinkedList concept

I would strongly suggest reading Wikipedia for a good explanation of Linked Lists as well. It may help you to grasp the concept a bit better: http://en.wikipedia.org/wiki/Linked_list

I think reading the concept is not good enough for him as you can see how he code. He also needs to read more on the language as well. :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.