Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- 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 Student{
- private int indeks;
- private String pol;
- Student(int indeks,String pol){
- this.indeks=indeks;
- this.pol=pol;
- }
- public String getPol(){
- return pol;
- }
- public int getInd(){
- return indeks;
- }
- @Override
- public String toString(){
- return indeks+" "+pol;
- }
- }
- public class podreduvanjePoIndeks {
- public static void main(String[] args) throws IOException {
- File file = new File("test.txt");
- BufferedReader br = new BufferedReader(new FileReader(file));
- LinkedQueue<Student> maski = new LinkedQueue<>();
- LinkedQueue<Student> zenski = new LinkedQueue<>();
- String st;
- String[] pomniza = {};
- while ((st = br.readLine()) != null) {
- pomniza = st.split(" ");
- Student stu = new Student(Integer.parseInt(pomniza[0]), pomniza[1]);
- if (stu.getPol().equals("m")) {
- maski.enqueue(stu);
- } else
- zenski.enqueue(stu);
- }
- BufferedWriter bw = null;
- while (!zenski.isEmpty()) {
- Student ss = zenski.dequeue();
- try {
- File fi = new File("zenski.txt");
- if (!fi.exists()) {
- fi.createNewFile();
- }
- FileWriter fw = new FileWriter(fi);
- bw = new BufferedWriter(fw);
- bw.write(ss.getPol() + " " + ss.getInd());
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } finally {
- try {
- if (zenski.isEmpty())
- bw.close();
- } catch (Exception ex) {
- System.out.println("exc");
- }
- }
- }
- while (!maski.isEmpty()) {
- Student ss = maski.dequeue();
- try {
- File fi = new File("maski.txt");
- if (!fi.exists()) {
- fi.createNewFile();
- }
- FileWriter fw = new FileWriter(fi);
- bw = new BufferedWriter(fw);
- bw.write(ss.getPol() + " " + ss.getInd());
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } finally {
- try {
- if (maski.isEmpty())
- bw.close();
- } catch (Exception ex) {
- System.out.println("exc");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement