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: Please Help: Simple Double Linked List (remove, addBefore, )

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

    Exclamation Please Help: Simple Double Linked List (remove, addBefore, )

    The code: #4887211 - Pastie

    import java.util.*;
     
    // use ListNode to represent the balls.
    class ListNode{
     
        protected int element;
        protected ListNode next= null;
        protected ListNode prev= null;
     
        public ListNode(int item, ListNode prev, ListNode next) {
            this.element = item;
            this.prev = prev;
            this.next = next;
        }
     
        public ListNode(int item) {
            this.element = item;
        }
     
        public int getElement(){return this.element;}
        public ListNode getNext(){return next;} 
        public ListNode getPrev(){return prev;} 
     
    }
     
    class Result{
     
        // declare the member field
        private ListNode head = null;
        private ListNode tail = null;
        private ListNode current = null;
        private ListNode a = null;
        private int num_codes;
     
        // declare the constructor
        public Result(int num){
            num_codes= num;
            System.out.println(num_codes);
            for(int i=1; i<num_codes+1;i++){
                addLast(i,num_codes);
                /**        for(Listnode n = head; n!= null; n=n.next){
                  addLast(n);**/
            }
            }
     
     
            public void print() throws NoSuchElementException {
                if (head == null)
                    throw new NoSuchElementException("Nothing to print...");
     
                ListNode ln = head;
                System.out.printf("List is: ");
                System.out.println(ln + " : "+ ln.getElement());
                for (int i=1; i < num_codes; i++) {
                    ln = ln.next;
                    System.out.print(ln);
                    System.out.println(" : " + ln.getElement());
                }
                System.out.println(".");
            }
     
     
            public void addLast(int i,int num_codes){
                if(head!=null){
     
                    if(i==num_codes){
     
                        tail.next = new ListNode(i, null, null);
                        tail = tail.next;
                    }else{
                        System.out.print(" body ");
                        tail.next = new ListNode(i, null,tail);
                        tail = tail.next;}
                }else{// nothing in head
                    System.out.print(" head ");
                    head = new ListNode(i,null,null);// nothing before and after
                    tail = head;
                }
                num_codes++;
            }
     
            public boolean contains(int item) {
                for (ListNode n = head; n!= null; n=n.next)
                    if (n.getElement()==(item)) 
                        return true;
                return false;
            }
     
            /* implement the operation for A
             *     PRE-Condition  :
             *     POST-Condition :
             */  
            public void doA(int x, int y) {
                ListNode current= null;
                ListNode a = null;
     
                for(ListNode ln=head;ln!= null;ln=ln.next){
                    System.out.println(" iteration " + ln.getElement());    
                    if((ln.getElement())==(y)){
                        current  =ln;
                    }
     
                    if((ln.getElement())==(x)){
                        a = ln;
                    }}
     
                    //implement operation
     
                    //If already to the left to right
                    if((x+1)==y){
                        ;
                    }else{// arrange it.
                        if(current!= null){
                            ListNode temp;
                            if(current.prev!=null){
                                temp = current.prev;
                                current.prev.next = a;
                                current.prev = a;
                                a.next = current;
                                a.prev= temp;
                                print();
                            }else{//if current is head
     
                                if(current.prev==null){
                                    current.prev = a;
                                    head = a;
                                    head.prev= null;
                                    print();
                                }
     
                                }}}
            }
     
     
            /* implement the operation for B
             *     PRE-Condition  :
             *     POST-Condition :
             */  
            public void doB(int x, int y) {
     
            }
     
            /* implement the operation for R
             *     PRE-Condition  :
             *     POST-Condition :
             */
            public void doR(int x) {
     
                ListNode reme=null;
                for(ListNode m = head; m!=null; m=m.next){
     
                    if(m.getElement()==x){
                        reme = m;
                    }}
     
                //execution of removing
                if(reme == null){
                    ;  //throw new NoSuchElementExeption("remove fails");
                }else{
                  num_codes--;
                    if(reme.prev!=null){                  
                        reme.next.prev= reme.prev;
                    }else{ //its a head
                        head = reme.next;
                        reme.prev= null;
     
                    }
     
                    if(reme.next!=null){
     
                        reme.prev.next= reme.next;
                    }else{//its a tail
                        tail=reme.prev;
                        tail.next= null;
     
                    }
                }
     
            }
        }
     
        public class Balls {
     
            public static void main(String[] args) {
     
                // declare the necessary variables    
                int num, ops;
                String task;
     
                // create new object from class Result;
                Result b1;
     
                // declare a Scanner object to read input
                Scanner myScanner = new Scanner(System.in);
     
                // read input and process them accordingly
                num = myScanner.nextInt();
                b1= new Result(num);
                b1.print();
                ops = myScanner.nextInt();
     
                for(int i=0;i<ops;i++){
                    task = myScanner.next();
                    if(task.equals("A")){
                        System.out.println("A");
                        b1.doA(myScanner.nextInt(),myScanner.nextInt());
                        b1.print();
                    }
                    else if (task.equals("B")){
                        System.out.println("B");
                        b1.doB(myScanner.nextInt(), myScanner.nextInt());
                    }
                    else if(task.equals("R")){
                        System.out.println("R");
                        b1.doR(myScanner.nextInt());
                    }
                }
            }
        }

    Problem:
    There are N balls labelled with 1, 2, 3,..., N, from left to right.

    There are 3 kinds of operation.
    1. move the ball in position x before ball in position y, where x ≠ y. Reminder: if x is on the left of y, you may ignore this operation.

    2. move the ball in position x after ball in position y, where x ≠ y. Reminder: if x is on the right of y, you may ignore this operation.

    3. Remove the ball labelled x.

    Inputs are first the number of balls and then the number of operations.
    Then the type of operation(either A, B, R) and the position (x, y).

    ----
    myProblem is not being able to arrange he Ball and remove it. Can anyone take a look at my code and tell me what is wrong with it?
    Last edited by yeohv; September 30th, 2012 at 02:12 PM.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Please Help: Simple Double Linked List (remove, addBefore, )

    You should post your code in the forum rather than in a link. Use [code=java] [/code] tags too to allow your code to be readable.

Similar Threads

  1. trouble with add and remove method on doubly linked list
    By BetterCallSaul in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 22nd, 2012, 08:12 PM
  2. trouble with add and remove method on doubly linked list
    By BetterCallSaul in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2012, 03:51 PM
  3. Help with using iterator remove on double linked list
    By papated21 in forum Collections and Generics
    Replies: 3
    Last Post: October 18th, 2011, 10:22 PM
  4. Simple linked list problem
    By babe20042004 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 5th, 2011, 11:40 AM
  5. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM