Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- interface Queue<E> {
- // Elementi na redicata se objekti od proizvolen tip.
- // Metodi za pristap:
- public boolean isEmpty ();
- // Vrakja true ako i samo ako redicata e prazena.
- public int size ();
- // Ja vrakja dolzinata na redicata.
- public E peek ();
- // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
- // Metodi za transformacija:
- public void clear ();
- // Ja prazni redicata.
- public void enqueue (E x);
- // Go dodava x na kraj od redicata.
- public E dequeue ();
- // Go otstranuva i vrakja pochetniot element na redicata.
- }
- class LinkedQueue<E> implements Queue<E> {
- // Redicata e pretstavena na sledniot nacin:
- // length go sodrzi brojot na elementi.
- // Elementite se zachuvuvaat vo jazli dod SLL
- // front i rear se linkovi do prviot i posledniot jazel soodvetno.
- SLLNode<E> front, rear;
- int length;
- // Konstruktor ...
- public LinkedQueue () {
- clear();
- }
- public boolean isEmpty () {
- // Vrakja true ako i samo ako redicata e prazena.
- return (length == 0);
- }
- public int size () {
- // Ja vrakja dolzinata na redicata.
- return length;
- }
- public E peek () {
- // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
- if (front == null)
- throw new NoSuchElementException();
- return front.element;
- }
- public void clear () {
- // Ja prazni redicata.
- front = rear = null;
- length = 0;
- }
- public void enqueue (E x) {
- // Go dodava x na kraj od redicata.
- SLLNode<E> latest = new SLLNode<E>(x, null);
- if (rear != null) {
- rear.succ = latest;
- rear = latest;
- } else
- front = rear = latest;
- length++;
- }
- public E dequeue () {
- // Go otstranuva i vrakja pochetniot element na redicata.
- if (front != null) {
- E frontmost = front.element;
- front = front.succ;
- if (front == null) rear = null;
- length--;
- return frontmost;
- } else
- throw new NoSuchElementException();
- }
- }
- class Gragjanin {
- String namesurname;
- int licna;
- int pass;
- int driving;
- public Gragjanin(String name,int lKarta,int pasos,int vozacka)
- {
- namesurname = name;
- licna = lKarta;
- pass = pasos;
- driving = vozacka;
- }
- public boolean zavrsen()
- {
- return (licna == 0&&pass == 0 && driving == 0);
- }
- public String getNamesurname() {
- return namesurname;
- }
- public void setNamesurname(String namesurname) {
- this.namesurname = namesurname;
- }
- public int getLicna() {
- return licna;
- }
- public void setLicna(int licna) {
- this.licna = licna;
- }
- public int getPass() {
- return pass;
- }
- public void setPass(int pass) {
- this.pass = pass;
- }
- public int getDriving() {
- return driving;
- }
- public void setDriving(int driving) {
- this.driving = driving;
- }
- }
- public class MVR {
- public static void main(String[] args) {
- Scanner br = new Scanner(System.in);
- int N = Integer.parseInt(br.nextLine());
- LinkedQueue<Gragjanin> n1 = new LinkedQueue<Gragjanin>();
- LinkedQueue<Gragjanin> n2 = new LinkedQueue<Gragjanin>();
- LinkedQueue<Gragjanin> n3 = new LinkedQueue<Gragjanin>();
- for(int i=1;i<=N;i++){
- String imePrezime = br.nextLine();
- int lKarta = Integer.parseInt(br.nextLine());
- int pasos = Integer.parseInt(br.nextLine());
- int vozacka = Integer.parseInt(br.nextLine());
- Gragjanin covek = new Gragjanin(imePrezime,lKarta,pasos,vozacka);
- if(covek.getLicna() == 1)
- n1.enqueue(covek);
- else if(covek.getPass() == 1)
- n2.enqueue(covek);
- else if(covek.getDriving() == 1)
- n3.enqueue(covek);
- }
- while(!n1.isEmpty())
- {
- Gragjanin pom = n1.dequeue();
- pom.setLicna(0);
- if(pom.getPass() == 1)
- n2.enqueue(pom);
- else if(pom.getDriving() == 1)
- n3.enqueue(pom);
- if(pom.zavrsen())
- System.out.println(pom.getNamesurname());
- }
- while(!n2.isEmpty())
- {
- Gragjanin pom = n2.dequeue();
- pom.setPass(0);
- if(pom.getDriving() == 1)
- n3.enqueue(pom);
- if(pom.zavrsen())
- System.out.println(pom.getNamesurname());
- }
- while(!n3.isEmpty())
- {
- Gragjanin pom = n3.dequeue();
- pom.setDriving(0);
- if(pom.zavrsen())
- System.out.println(pom.getNamesurname());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement