Swapping ball - Nguyễn Đình Nhật - Nguyễn Như Hải Triều
1. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
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
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 1 of 14
2. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
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.
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 2 of 14
3. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
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:
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 3 of 14
4. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Describe algorithm by chart
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 4 of 14
5. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
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
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 5 of 14
6. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
Describe by model
With N = 2 (even number)
Total steps: 11
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 6 of 14
7. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
With N = 3 (odd number)
Total steps: 21
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 7 of 14
8. 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
9. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
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(){
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 9 of 14
10. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
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;
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 10 of 14
12. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
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;
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 12 of 14
13. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
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);
}
}
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 13 of 14
14. Computer Science for Practicing Engineering DUY TAN UNIVERSITY
RESULT
Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 14 of 14