Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: LinkedList insert, remove and change

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default LinkedList insert, remove and change

    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);
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: LinkedList insert, remove and change

    Recommended reading: http://www.javaprogrammingforums.com...e-posting.html

    What have you tried? Where are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. From List<String> to a generic LinkedList
    By johnrmsn@Msn.com in forum Collections and Generics
    Replies: 2
    Last Post: July 2nd, 2011, 12:46 PM
  2. LinkedList outputs ONLY last element
    By hexwind in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 30th, 2011, 04:57 AM
  3. Reversing lines using LinkedList
    By velop in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 14th, 2011, 06:10 AM
  4. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM
  5. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 03:00 AM