Your SlideShare is downloading. ×
Swapping ball - Nguyễn Đình Nhật - Nguyễn Như Hải Triều
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Swapping ball - Nguyễn Đình Nhật - Nguyễn Như Hải Triều

789
views

Published on

Published in: Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
789
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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 Students name: NGUYỄN ĐÌNH NHẬT NGUYỄN NHƯ HẢI TRIỀU Class: K15CMU-TCD1 Da Nang, 28/05/2011Authors: 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 ASSIGNMENTReqest: Swap N number of blue balls and red ball.Example: Enter N is 3.Total ball: 3 red balls + 3 blue balls = 6 ballsTotal cells: 6 balls + 1 empty = 7 cells Describe project: In this project, we use Double Link List to keep balls. We use Node to keepinformation. Information of Node include Data (Red, Blue or Empty), Next ( Next Node), andPrevious (Previous Node). Additionally, We use HEAD node and TAIL node to define the firstnode 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 Nred 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 ballnext to last). Continue to moving all blue ball to the right. Move the empty cell into positionbetween red and green balls. However, in this algorithm, we have two case to comeback. If N is even number or N isodd number. With N is even number, we swap empty cell for pre-pre-emptycell, and continueto moving Blue ball to an adjacent empty square. With N is odd number, we use a notherway. 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 chartAuthors: 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 Pseudocodeint 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-EMPTYcellAuthors: 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 modelWith N = 2 (even number)Total steps: 11Authors: 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 UNIVERSITYWith N = 3 (odd number)Total steps: 21Authors: 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 UNIVERSITYAuthors: 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
  • 11. Computer Science for Practicing Engineering DUY TAN UNIVERSITY 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){Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 11 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 RESULTAuthors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 14 of 14