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?