Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Yurii Piets - nr 6
- import java.util.Scanner;
- class Wagon{
- private String name;
- private Wagon nextWagon;
- private Wagon prevWagon;
- public Wagon(){
- nextWagon = null;
- prevWagon = null;
- }
- public Wagon(String n){
- name = n;
- nextWagon = null;
- prevWagon = null;
- }
- public String getNameW(){
- return name;
- }
- public void setNextW(Wagon n){
- nextWagon = n;
- }
- public Wagon getNextW(){
- return nextWagon;
- }
- public void setPrevW(Wagon p){
- prevWagon = p;
- }
- public Wagon getPrevW(){
- return prevWagon;
- }
- }
- class Pociag{
- public Pociag Trains;
- private String name;
- private Pociag nextPociag;
- public Wagon firstWagon;
- public Wagon lastWagon;
- public Pociag(){
- Trains = null;
- nextPociag = null;
- firstWagon = null;
- lastWagon = null;
- }
- public Pociag (String nameP, String nameW){
- name = nameP;
- firstWagon = new Wagon(nameW);
- nextPociag = null;
- lastWagon = firstWagon;
- }
- /*---------------------------допоміжні методи-----------------------------------------------------*/
- public boolean isEmpty(){
- return Trains==null;
- }
- public Pociag locate(String key){
- Pociag current = Trains;
- while(current != null && !(current.getName().equals(key))){
- current = current.getNext();
- }
- return current;
- }
- public Pociag locatePrevious(String key){
- Pociag current = Trains, previous = Trains;
- while(current != null && !(current.getName().equals(key))){
- previous = current;
- current = current.getNext();
- }
- return previous;
- }
- public void deletePociag(String key){
- Pociag current = locate(key);
- Pociag previous = locatePrevious(key);
- if(current == Trains) Trains = Trains.getNext(); // Если первый элеменn изменить first
- else previous.setNext(current.getNext());
- }
- public void add(String nP2, String nW){
- if(isEmpty()) Trains = new Pociag(nP2,nW);
- else{
- Pociag current = new Pociag(nP2,nW);
- current.setNext(Trains);
- Trains = current;
- }
- }
- /*------------------------------------------------------------------------------------------------*/
- public String getName(){
- return name;
- }
- public void setNext(Pociag p){
- nextPociag = p;
- }
- public Pociag getNext(){
- return nextPociag;
- }
- public void insertFirst(String n){
- Wagon newWagon = new Wagon(n);
- newWagon.setPrevW(null);
- newWagon.setNextW(firstWagon);
- //if(firstWagon.getPrevW() == null) firstWagon.setPrevW(newWagon);
- //else if (firstWagon.getNextW() == null) firstWagon.setNextW(newWagon);
- if(firstWagon.getPrevW() == null && firstWagon.getNextW() == null){
- firstWagon.setNextW(newWagon);
- }
- else if(firstWagon.getPrevW() == null) firstWagon.setPrevW(newWagon);
- else if (firstWagon.getNextW() == null) firstWagon.setNextW(newWagon);
- firstWagon = newWagon;
- }
- public void insertLast(String n){
- Wagon newWagon = new Wagon(n);
- newWagon.setNextW(null);
- newWagon.setPrevW(lastWagon);
- if(lastWagon.getNextW() == null) lastWagon.setNextW(newWagon);
- else if(lastWagon.getPrevW() == null) lastWagon.setPrevW(newWagon);
- lastWagon = newWagon;
- }
- public void display(){
- boolean flag = true;
- Wagon newWagon = firstWagon;
- String s = getName() + ":";
- while(newWagon != lastWagon){
- s = s + " " + newWagon.getNameW();
- if(newWagon.getPrevW() == null){
- if(newWagon == newWagon.getNextW().getPrevW()) flag = true;
- else flag = false;
- newWagon = newWagon.getNextW();
- }
- else if(newWagon.getNextW() == null){
- if(newWagon == newWagon.getPrevW().getNextW()) flag = false;
- else flag = true;
- newWagon = newWagon.getPrevW();
- }
- else if(flag == true){
- if(newWagon == newWagon.getNextW().getPrevW()) flag = true;
- else flag = false;
- newWagon = newWagon.getNextW();
- }
- else{
- if(newWagon == newWagon.getPrevW().getNextW()) flag = false;
- else flag = true;
- newWagon = newWagon.getPrevW();
- }
- }
- System.out.println(s + " " + lastWagon.getNameW());
- }
- public void trainsList(){
- Pociag current = Trains;
- String s = "Trains:";
- while (current != null){
- s = s + " " + current.getName();
- current = current.nextPociag;
- }
- System.out.println(s);
- }
- public void delFirst(){
- //if(firstWagon != null)
- if(firstWagon.getNextW() == null){
- if(firstWagon.getPrevW().getPrevW() == firstWagon) firstWagon.getPrevW().setPrevW(null);
- else if(firstWagon.getPrevW().getNextW() == firstWagon) firstWagon.getPrevW().setNextW(null);
- firstWagon = firstWagon.getPrevW();
- }
- else if(firstWagon.getPrevW() == null){
- if(firstWagon.getNextW().getNextW() == firstWagon) firstWagon.getNextW().setNextW(null);
- else if(firstWagon.getNextW().getPrevW() == firstWagon) firstWagon.getNextW().setPrevW(null);
- firstWagon = firstWagon.getNextW();
- }
- }
- public void delLast(){
- //if(lastWagon != null)
- if(lastWagon.getNextW() == null){
- if(lastWagon.getPrevW().getNextW() == lastWagon) lastWagon.getPrevW().setNextW(null);
- else if(lastWagon.getPrevW().getPrevW() == lastWagon) lastWagon.getPrevW().setPrevW(null);
- lastWagon = lastWagon.getPrevW();
- }
- else if(lastWagon.getPrevW() == null){
- if(lastWagon.getNextW().getPrevW() == lastWagon) lastWagon.getNextW().setPrevW(null);
- else if(lastWagon.getNextW().getNextW() == lastWagon) lastWagon.getNextW().setNextW(null);
- lastWagon = lastWagon.getNextW();
- }
- }
- public void reverse(){
- Wagon buff = firstWagon;
- firstWagon = lastWagon;
- lastWagon = buff;
- }
- public void union(Pociag p2){
- if(lastWagon.getNextW() == null){ //перший нормальний
- lastWagon.setNextW(p2.firstWagon);
- if(p2.firstWagon.getPrevW() == null) p2.firstWagon.setPrevW(lastWagon); //другий нормальний
- else if( p2.firstWagon.getNextW() == null) p2.firstWagon.setNextW(lastWagon); //другий розвернутий
- }
- else{ if(lastWagon.getPrevW() == null ){ //перший ровернуий
- lastWagon.setPrevW(p2.firstWagon);
- if(p2.firstWagon.getPrevW() == null) p2.firstWagon.setPrevW(lastWagon); //другий нормальний
- else if(p2.firstWagon.getNextW() == null) p2.firstWagon.setNextW(lastWagon); //другий розвенруний
- }
- }
- lastWagon = p2.lastWagon;
- }
- }
- public class Source{
- public static Scanner inScan = new Scanner(System.in);
- public static void main(String[] args){
- //Scanner inScan = new Scanner(System.in);
- Integer liczZ = inScan.nextInt();
- while(liczZ-- > 0){
- //for(int i = 0 ; i < liczZ ; i++){
- String nP = "", nW = "", nP1="", nP2="";
- Pociag p = new Pociag();
- Integer liczN = inScan.nextInt();
- while(liczN -- > 0){
- //for(int j = 0; j < liczN; j++){
- String Polecenie = inScan.next();
- /*----------------------------New------------------------------*/
- if ( Polecenie.equals("New") ){
- nP = inScan.next();
- nW = inScan.next();
- p.add(nP,nW);
- //if( p.isEmpty() ) p.Trains = new Pociag(nP,nW);
- //else p.add(nP,nW);
- }
- /*------------------------InsertFirst--------------------------*/
- else if (Polecenie.equals("InsertFirst")){
- nP = inScan.next();
- nW = inScan.next();
- Pociag newP = p.locate(nP);
- //if(newP != null)
- newP.insertFirst(nW);
- }
- /*------------------------InsertLast--------------------------*/
- else if (Polecenie.equals("InsertLast")){
- nP = inScan.next();
- nW = inScan.next();
- Pociag newP = p.locate(nP);
- //if(newP != null)
- newP.insertLast(nW);
- }
- /*--------------------------Display----------------------------*/
- else if( Polecenie.equals("Display")){
- nP = inScan.next();
- Pociag newP = p.locate(nP);
- //if(newP != null)
- newP.display();
- }
- /*--------------------- -----TrainsList----------------------------*/
- else if (Polecenie.equals("TrainsList") ){
- p.trainsList();
- }
- /*--------------------------Reverse----------------------------*/
- else if (Polecenie.equals("Reverse")){
- nP = inScan.next();
- Pociag newP = p.locate(nP);
- //if(newP != null)
- newP.reverse();
- }
- /*------------------------------------------Union-----------------------------------------*/
- else if (Polecenie.equals("Union")){
- nP1 = inScan.next();
- nP2 = inScan.next();
- Pociag p1 = p.locate(nP1);
- Pociag p2 = p.locate(nP2);
- //if(p1 != null && p2 != null)
- p1.union(p2);
- p.deletePociag(nP2);
- }
- /*------------------------------------------DelFirst-----------------------------------------*/
- else if (Polecenie.equals("DelFirst")){
- nP1 = inScan.next();
- nP2 = inScan.next();
- Pociag current = p.locate(nP1);
- //if(current != null){
- nW = current.firstWagon.getNameW();
- if(current.firstWagon.getNextW() == null && current.firstWagon.getPrevW() == null) p.deletePociag(nP1);
- else current.delFirst();
- p.add(nP2, nW);
- // }
- }
- /*------------------------------------------DelLast--------------------------------------------------*/
- else if (Polecenie.equals("DelLast")){
- nP1 = inScan.next();
- nP2 = inScan.next();
- Pociag current = p.locate(nP1);
- // if(current != null){
- nW = current.lastWagon.getNameW();
- if(current.lastWagon.getPrevW() == null && current.lastWagon.getNextW() == null) p.deletePociag(nP1);
- else current.delLast();
- p.add(nP2, nW);
- // }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement