Swapping ball - Nguyễn Đình Nhật - Nguyễn Như Hải Triều
-
Upload
mrcoffee282 -
Category
Education
-
view
764 -
download
2
description
Transcript of Swapping ball - Nguyễn Đình Nhật - Nguyễn Như Hải Triều
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 1 of 14
DUY TAN UNIVERSITY
INTERNATIONAL SCHOOL
ASSIGNMENT Computer Science for Practicing Engineering
**********
Faculty: HUỲNH BÁ DIỆU
Student's name: NGUYỄN ĐÌNH NHẬT
NGUYỄN NHƯ HẢI TRIỀU Class: K15CMU-TCD1
Da Nang, 28/05/2011
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 2 of 14
ASSIGNMENT
Reqest: Swap N number of blue balls and red ball.
Example: Enter N is 3. Total ball: 3 red balls + 3 blue balls = 6 balls
Total cells: 6 balls + 1 empty = 7 cells
Describe project:
In this project, we use Double Link List to keep balls. We use Node to keep information. Information of Node include Data (Red, Blue or Empty), Next ( Next Node), and Previous (Previous Node). Additionally, We use HEAD node and TAIL node to define the first
node and the last node.
Describe the way of sort:
Enter any N “number”. We create Double Link List to include 2*N+1 Node. It incude N red balls, N blue balls and 1 Empty Node. Move by one blue ball from left to right. Then move the box from right to left (blue ball
next to last). Continue to moving all blue ball to the right. Move the empty cell into position between red and green balls. However, in this algorithm, we have two case to comeback. If N is even number or N is
odd number. With N is even number, we swap empty cell for pre-pre-emptycell, and continue to moving Blue ball to an adjacent empty square. With N is odd number, we use a nother
way. We need two steps to move the ball on the desired location.
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 3 of 14
Describe algorithm:
Create Node:
Node Describe Type
Data Red, Blue or Empty Char
Next Next Node Node
Prev Previous Node Node
Enter N number: 2 Create Double Link List:
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 4 of 14
Describe algorithm by chart
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 5 of 14
Describe algorithm by Pseudocode
int checkList = 0;
while(checkList!=n) nodeRef = Head; while(nodeRef.next.data!='*')
nodeRef = nodeRef.next; recheck condition.
// nodeRef == REDball while (REDball != Tail && Next-REDball == EMPTYcell) change REDball for EMPTYcell in the next.
if(REDball !=Tail && Next-REDball == BLUEball) change EMPTYcell (behind REDball) for BLUEball (in front of it);
if(++checkList==n) Finish algorithm and print result. nullNode = nodeRef.prew;
// nodeRef ==EMPTYcell while (Prev-EMPTYcell != BLUEball && Prev-EMPTYcell != Head)
if ( Prev-Prev-EMPTYcell != BLUEball) change EMPTYcell for Prev-Prev-EMPTYcell. else
change EMPTYcell for Prev-Prev-EMPTYcell. change EMPTYcell for Next-EMPTYcell change EMPTYcell for Next-Next-EMPTYcell
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 6 of 14
Describe by model
With N = 2 (even number) Total steps: 11
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 7 of 14
With N = 3 (odd number)
Total steps: 21
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 8 of 14
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 9 of 14
CODE: LinkList class
/* * Object: Computer Science for Practicing Engineering
* Faculty Huynh Ba Dieu * Authors: * 1. Nguyen Dinh Nhat
* 2. Nguyen Nhu Hai Trieu * Class: K15CMU-TCD1 -- International School -- Duy Tan University * 05/2011
*/ import java.util.*;
public class LinkList {
// Node class class Node { char data;
Node next,prew; //Method: create a node
Node(char x) { data=x;
next=prew=null; } Node(char x, Node t)
{ data = x; next = prew = null;
if(t!=null) { next = t;
t.prew = this; }
} }
Node Head, Tail; Node nullNode; Node nodeRef;
static int count=0; static int n;
//Method: create null Link List LinkList(){
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 10 of 14
Head=Tail=null;
}
//Method: create Link Link with data void createLinkList() { char x = 'B';
for(int i=1;i<=2*n+1;i++) { if(i<n+1)x='B'; else
if(i==n+1)x='*'; else x='R';
Node t = new Node(x,null); if(Head==null) Head=Tail=t;
else { Tail.next = t; t.prew = Tail; Tail = t;} } }
//Method: display LinkList from HEAD to TAIL void display() {
Node p = Head; System.out.print("| "); while(p!=null) {
System.out.print(p.data + " | "); p = p.next; }
System.out.println(""); }
//Method: display LinkList from TAIL to HEAD void display2() { Node p = Tail;
System.out.print("| "); while(p!=null) {
System.out.print(p.data + " | "); p = p.prew; }
System.out.println(""); }
//Method: swap ball void changeBall() { int checkList = 0;
while(checkList!=n){ nodeRef = Head;
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 11 of 14
while(nodeRef.next.data!='*') {
nodeRef = nodeRef.next; } while(nodeRef!=Tail && nodeRef.next.data=='*'){
this.change2Ball(nodeRef, nodeRef.next); if(nodeRef!=Tail && nodeRef.next.data=='R') { this.change3Ball(nodeRef.prew, nodeRef, nodeRef.next);
} }
if(++checkList==n) return; nullNode = nodeRef.prew;
while(nullNode.prew.data!='B' && nullNode.prew!=Head){
if(nullNode.prew.prew.data!='B') this.change3Ball(nullNode.prew.prew, nullNode.prew, nullNode); else {
this.change3Ball(nullNode.prew.prew, nullNode.prew, nullNode); this.change2Ball(nullNode, nullNode.next);
this.change3Ball(nullNode, nullNode.next, nullNode.next.next); } }
} }
//Sub method void display3(){ count++;
System.out.print(" " + count + " times:\t"); this.display(); }
//Method: move a ball to an adjacent empty square (forwards or backwards)
void change2Ball(Node a, Node b) { if(a.prew!=null && b.next!=null){ b.next.prew = a;
a.prew.next = b; a.next = b.next; b.prew = a.prew;
b.next = a; a.prew = b; }
else if(a == Head){
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 12 of 14
b.prew = null;
b.next.prew = a; a.next = b.next; b.next = a;
a.prew = b; Head = b; }
else if(b == Tail){ a.next = null;
a.prew.next = b; b.prew = a.prew; b.next = a;
a.prew = b; Tail = a;
} this.display3(); }
//Method: jump a single adjacent ball into an empty square (forwards or backwards) void change3Ball(Node a, Node b, Node c) {
if(a == Head && c == Tail){ a.next = null; a.prew = b;
c.next = b; c.prew = null; b.next = a;
b.prew = c; Head = c; Tail = a;
} else if(c.next!=null && a.prew!=null){
c.next.prew = a; c.prew = a.prew;
a.prew.next = c; a.next = c.next; a.prew = b;
b.prew = c; c.next = b; b.next = a;
} else if(a == Head){
c.next.prew = a; a.prew = b;
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 13 of 14
b.prew = c;
c.prew = null; a.next = c.next; b.next = a;
c.next = b; Head = c; }
else if(c == Tail){ a.prew.next = c;
c.next = b; b.next = a; a.next = null;
c.prew = a.prew; b.prew = c;
a.prew = b; Tail = a; }
this.display3(); }
//Main method public static void main (String [] ndnhat){ LinkList ll = new LinkList();
Scanner kb = new Scanner(System.in); System.out.println(" ********* Final-Project: Swap Ball ********"); System.out.print(" Enter N: ");
n = kb.nextInt(); ll.createLinkList(); System.out.print(" LinkList :\t");
ll.display(); System.out.println("\n"); ll.changeBall();
System.out.println(""); System.out.print(" LinkList :\t");
ll.display(); System.out.println(" Total times of swap: "+count); }
}
Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 14 of 14
RESULT