SlideShare a Scribd company logo
1 of 14
Download to read offline
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
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
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
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
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
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
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
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


                                    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
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
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
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
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
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

More Related Content

What's hot

Introduction - Lattice-based Cryptography
Introduction - Lattice-based CryptographyIntroduction - Lattice-based Cryptography
Introduction - Lattice-based CryptographyAlexandre Augusto Giron
 
Lattice Based Cryptography - GGH Cryptosystem
Lattice Based Cryptography - GGH CryptosystemLattice Based Cryptography - GGH Cryptosystem
Lattice Based Cryptography - GGH CryptosystemVarun Janga
 
Numerical approach for Hamilton-Jacobi equations on a network: application to...
Numerical approach for Hamilton-Jacobi equations on a network: application to...Numerical approach for Hamilton-Jacobi equations on a network: application to...
Numerical approach for Hamilton-Jacobi equations on a network: application to...Guillaume Costeseque
 
Tensorizing Neural Network
Tensorizing Neural NetworkTensorizing Neural Network
Tensorizing Neural NetworkRuochun Tzeng
 
Lattice Cryptography
Lattice CryptographyLattice Cryptography
Lattice CryptographyPriyanka Aash
 
Cyclic code systematic
Cyclic code systematicCyclic code systematic
Cyclic code systematicNihal Gupta
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithmguest862df4e
 
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8Nguyễn Công Hoàng
 
Parallel Optimization in Machine Learning
Parallel Optimization in Machine LearningParallel Optimization in Machine Learning
Parallel Optimization in Machine LearningFabian Pedregosa
 
Java and Deep Learning (Introduction)
Java and Deep Learning (Introduction)Java and Deep Learning (Introduction)
Java and Deep Learning (Introduction)Oswald Campesato
 
Graph Kernels for Chemical Informatics
Graph Kernels for Chemical InformaticsGraph Kernels for Chemical Informatics
Graph Kernels for Chemical InformaticsMukund Raj
 
Particle Filters and Applications in Computer Vision
Particle Filters and Applications in Computer VisionParticle Filters and Applications in Computer Vision
Particle Filters and Applications in Computer Visionzukun
 
Random Forest for Big Data
Random Forest for Big DataRandom Forest for Big Data
Random Forest for Big Datatuxette
 
Eight Formalisms for Defining Graph Models
Eight Formalisms for Defining Graph ModelsEight Formalisms for Defining Graph Models
Eight Formalisms for Defining Graph ModelsJérôme KUNEGIS
 
ON THE DUALITY FEATURE OF P-CLASS PROBLEMS AND NP COMPLETE PROBLEMS
ON THE DUALITY FEATURE OF P-CLASS PROBLEMS AND NP COMPLETE PROBLEMSON THE DUALITY FEATURE OF P-CLASS PROBLEMS AND NP COMPLETE PROBLEMS
ON THE DUALITY FEATURE OF P-CLASS PROBLEMS AND NP COMPLETE PROBLEMScscpconf
 

What's hot (20)

CSC446: Pattern Recognition (LN7)
CSC446: Pattern Recognition (LN7)CSC446: Pattern Recognition (LN7)
CSC446: Pattern Recognition (LN7)
 
Introduction - Lattice-based Cryptography
Introduction - Lattice-based CryptographyIntroduction - Lattice-based Cryptography
Introduction - Lattice-based Cryptography
 
Lattice Based Cryptography - GGH Cryptosystem
Lattice Based Cryptography - GGH CryptosystemLattice Based Cryptography - GGH Cryptosystem
Lattice Based Cryptography - GGH Cryptosystem
 
Lecture26
Lecture26Lecture26
Lecture26
 
Numerical approach for Hamilton-Jacobi equations on a network: application to...
Numerical approach for Hamilton-Jacobi equations on a network: application to...Numerical approach for Hamilton-Jacobi equations on a network: application to...
Numerical approach for Hamilton-Jacobi equations on a network: application to...
 
Tensorizing Neural Network
Tensorizing Neural NetworkTensorizing Neural Network
Tensorizing Neural Network
 
CSC446: Pattern Recognition (LN6)
CSC446: Pattern Recognition (LN6)CSC446: Pattern Recognition (LN6)
CSC446: Pattern Recognition (LN6)
 
Lattice Cryptography
Lattice CryptographyLattice Cryptography
Lattice Cryptography
 
Cyclic code systematic
Cyclic code systematicCyclic code systematic
Cyclic code systematic
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
 
Parallel Optimization in Machine Learning
Parallel Optimization in Machine LearningParallel Optimization in Machine Learning
Parallel Optimization in Machine Learning
 
Java and Deep Learning (Introduction)
Java and Deep Learning (Introduction)Java and Deep Learning (Introduction)
Java and Deep Learning (Introduction)
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
 
Graph Kernels for Chemical Informatics
Graph Kernels for Chemical InformaticsGraph Kernels for Chemical Informatics
Graph Kernels for Chemical Informatics
 
Particle Filters and Applications in Computer Vision
Particle Filters and Applications in Computer VisionParticle Filters and Applications in Computer Vision
Particle Filters and Applications in Computer Vision
 
Random Forest for Big Data
Random Forest for Big DataRandom Forest for Big Data
Random Forest for Big Data
 
Eight Formalisms for Defining Graph Models
Eight Formalisms for Defining Graph ModelsEight Formalisms for Defining Graph Models
Eight Formalisms for Defining Graph Models
 
ON THE DUALITY FEATURE OF P-CLASS PROBLEMS AND NP COMPLETE PROBLEMS
ON THE DUALITY FEATURE OF P-CLASS PROBLEMS AND NP COMPLETE PROBLEMSON THE DUALITY FEATURE OF P-CLASS PROBLEMS AND NP COMPLETE PROBLEMS
ON THE DUALITY FEATURE OF P-CLASS PROBLEMS AND NP COMPLETE PROBLEMS
 

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

AnswerThe new program with the required constructor and with a te.pdf
AnswerThe new program with the required constructor and with a te.pdfAnswerThe new program with the required constructor and with a te.pdf
AnswerThe new program with the required constructor and with a te.pdfnipuns1983
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
COMP2710: Software Construction - Linked list exercises
COMP2710: Software Construction - Linked list exercisesCOMP2710: Software Construction - Linked list exercises
COMP2710: Software Construction - Linked list exercisesXiao Qin
 
Write code in C++ Write a program to perform a topological sort on a g.docx
Write code in C++ Write a program to perform a topological sort on a g.docxWrite code in C++ Write a program to perform a topological sort on a g.docx
Write code in C++ Write a program to perform a topological sort on a g.docxkarlynwih
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 
Write code in c++ Program to a topological sort on a graph Program pl.docx
Write code in c++ Program to a topological sort on a graph  Program pl.docxWrite code in c++ Program to a topological sort on a graph  Program pl.docx
Write code in c++ Program to a topological sort on a graph Program pl.docxnoreendchesterton753
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfarjunstores123
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docxajoy21
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
 
Use C++class Node{public   Node ( int = 0 );       constru.pdf
Use C++class Node{public   Node ( int = 0 );        constru.pdfUse C++class Node{public   Node ( int = 0 );        constru.pdf
Use C++class Node{public   Node ( int = 0 );       constru.pdfoptokunal1
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docxajoy21
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 

Similar to Swapping ball - Nguyễn Đình Nhật - Nguyễn Như Hải Triều (20)

AnswerThe new program with the required constructor and with a te.pdf
AnswerThe new program with the required constructor and with a te.pdfAnswerThe new program with the required constructor and with a te.pdf
AnswerThe new program with the required constructor and with a te.pdf
 
Link list
Link listLink list
Link list
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
COMP2710: Software Construction - Linked list exercises
COMP2710: Software Construction - Linked list exercisesCOMP2710: Software Construction - Linked list exercises
COMP2710: Software Construction - Linked list exercises
 
Linked list
Linked listLinked list
Linked list
 
Dsprograms(2nd cse)
Dsprograms(2nd cse)Dsprograms(2nd cse)
Dsprograms(2nd cse)
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
Write code in C++ Write a program to perform a topological sort on a g.docx
Write code in C++ Write a program to perform a topological sort on a g.docxWrite code in C++ Write a program to perform a topological sort on a g.docx
Write code in C++ Write a program to perform a topological sort on a g.docx
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
Write code in c++ Program to a topological sort on a graph Program pl.docx
Write code in c++ Program to a topological sort on a graph  Program pl.docxWrite code in c++ Program to a topological sort on a graph  Program pl.docx
Write code in c++ Program to a topological sort on a graph Program pl.docx
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
Use C++class Node{public   Node ( int = 0 );       constru.pdf
Use C++class Node{public   Node ( int = 0 );        constru.pdfUse C++class Node{public   Node ( int = 0 );        constru.pdf
Use C++class Node{public   Node ( int = 0 );       constru.pdf
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 

More from mrcoffee282

Conflict Resolution Skill - Teamwork Team 1 - K15CMU-TCD1
Conflict Resolution Skill - Teamwork  Team 1 - K15CMU-TCD1Conflict Resolution Skill - Teamwork  Team 1 - K15CMU-TCD1
Conflict Resolution Skill - Teamwork Team 1 - K15CMU-TCD1mrcoffee282
 
Bai tap tham khao CSPE
Bai tap tham khao CSPEBai tap tham khao CSPE
Bai tap tham khao CSPEmrcoffee282
 
Database Project 2011
Database Project 2011Database Project 2011
Database Project 2011mrcoffee282
 
Database Project 2011
Database Project 2011Database Project 2011
Database Project 2011mrcoffee282
 
Ôn tập KTTMDT
Ôn tập KTTMDTÔn tập KTTMDT
Ôn tập KTTMDTmrcoffee282
 

More from mrcoffee282 (6)

Conflict Resolution Skill - Teamwork Team 1 - K15CMU-TCD1
Conflict Resolution Skill - Teamwork  Team 1 - K15CMU-TCD1Conflict Resolution Skill - Teamwork  Team 1 - K15CMU-TCD1
Conflict Resolution Skill - Teamwork Team 1 - K15CMU-TCD1
 
My Hash
My HashMy Hash
My Hash
 
Bai tap tham khao CSPE
Bai tap tham khao CSPEBai tap tham khao CSPE
Bai tap tham khao CSPE
 
Database Project 2011
Database Project 2011Database Project 2011
Database Project 2011
 
Database Project 2011
Database Project 2011Database Project 2011
Database Project 2011
 
Ôn tập KTTMDT
Ôn tập KTTMDTÔn tập KTTMDT
Ôn tập KTTMDT
 

Recently uploaded

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 

Recently uploaded (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

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
  • 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 RESULT Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 14 of 14