Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. class SLLNode<E> {
  4. protected E element;
  5. protected SLLNode<E> succ;
  6.  
  7. public SLLNode(E elem, SLLNode<E> succ) {
  8. this.element = elem;
  9. this.succ = succ;
  10. }
  11.  
  12. @Override
  13. public String toString() {
  14. return element.toString();
  15. }
  16. }
  17.  
  18. interface Queue<E> {
  19.  
  20. // Elementi na redicata se objekti od proizvolen tip.
  21.  
  22. // Metodi za pristap:
  23.  
  24. public boolean isEmpty ();
  25. // Vrakja true ako i samo ako redicata e prazena.
  26.  
  27. public int size ();
  28. // Ja vrakja dolzinata na redicata.
  29.  
  30. public E peek ();
  31. // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  32.  
  33. // Metodi za transformacija:
  34.  
  35. public void clear ();
  36. // Ja prazni redicata.
  37.  
  38. public void enqueue (E x);
  39. // Go dodava x na kraj od redicata.
  40.  
  41. public E dequeue ();
  42. // Go otstranuva i vrakja pochetniot element na redicata.
  43.  
  44. }
  45. class LinkedQueue<E> implements Queue<E> {
  46.  
  47. // Redicata e pretstavena na sledniot nacin:
  48. // length go sodrzi brojot na elementi.
  49. // Elementite se zachuvuvaat vo jazli dod SLL
  50. // front i rear se linkovi do prviot i posledniot jazel soodvetno.
  51. SLLNode<E> front, rear;
  52. int length;
  53.  
  54. // Konstruktor ...
  55.  
  56. public LinkedQueue () {
  57. clear();
  58. }
  59.  
  60. public boolean isEmpty () {
  61. // Vrakja true ako i samo ako redicata e prazena.
  62. return (length == 0);
  63. }
  64.  
  65. public int size () {
  66. // Ja vrakja dolzinata na redicata.
  67. return length;
  68. }
  69.  
  70. public E peek () {
  71. // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  72. if (front == null)
  73. throw new NoSuchElementException();
  74. return front.element;
  75. }
  76.  
  77. public void clear () {
  78. // Ja prazni redicata.
  79. front = rear = null;
  80. length = 0;
  81. }
  82.  
  83. public void enqueue (E x) {
  84. // Go dodava x na kraj od redicata.
  85. SLLNode<E> latest = new SLLNode<E>(x, null);
  86. if (rear != null) {
  87. rear.succ = latest;
  88. rear = latest;
  89. } else
  90. front = rear = latest;
  91. length++;
  92. }
  93.  
  94. public E dequeue () {
  95. // Go otstranuva i vrakja pochetniot element na redicata.
  96. if (front != null) {
  97. E frontmost = front.element;
  98. front = front.succ;
  99. if (front == null) rear = null;
  100. length--;
  101. return frontmost;
  102. } else
  103. throw new NoSuchElementException();
  104. }
  105.  
  106. }
  107. class Student{
  108. private int indeks;
  109. private String pol;
  110. Student(int indeks,String pol){
  111. this.indeks=indeks;
  112. this.pol=pol;
  113. }
  114. public String getPol(){
  115. return pol;
  116. }
  117. public int getInd(){
  118. return indeks;
  119. }
  120. @Override
  121. public String toString(){
  122. return indeks+" "+pol;
  123. }
  124.  
  125. }
  126. public class podreduvanjePoIndeks {
  127. public static void main(String[] args) throws IOException {
  128. File file = new File("test.txt");
  129.  
  130. BufferedReader br = new BufferedReader(new FileReader(file));
  131. LinkedQueue<Student> maski = new LinkedQueue<>();
  132. LinkedQueue<Student> zenski = new LinkedQueue<>();
  133. String st;
  134. String[] pomniza = {};
  135.  
  136. while ((st = br.readLine()) != null) {
  137. pomniza = st.split(" ");
  138. Student stu = new Student(Integer.parseInt(pomniza[0]), pomniza[1]);
  139. if (stu.getPol().equals("m")) {
  140. maski.enqueue(stu);
  141. } else
  142. zenski.enqueue(stu);
  143. }
  144. BufferedWriter bw = null;
  145. while (!zenski.isEmpty()) {
  146. Student ss = zenski.dequeue();
  147. try {
  148. File fi = new File("zenski.txt");
  149. if (!fi.exists()) {
  150. fi.createNewFile();
  151. }
  152. FileWriter fw = new FileWriter(fi);
  153. bw = new BufferedWriter(fw);
  154. bw.write(ss.getPol() + " " + ss.getInd());
  155. } catch (IOException ioe) {
  156. ioe.printStackTrace();
  157. } finally {
  158. try {
  159. if (zenski.isEmpty())
  160. bw.close();
  161. } catch (Exception ex) {
  162. System.out.println("exc");
  163. }
  164. }
  165. }
  166. while (!maski.isEmpty()) {
  167. Student ss = maski.dequeue();
  168. try {
  169. File fi = new File("maski.txt");
  170. if (!fi.exists()) {
  171. fi.createNewFile();
  172. }
  173. FileWriter fw = new FileWriter(fi);
  174. bw = new BufferedWriter(fw);
  175. bw.write(ss.getPol() + " " + ss.getInd());
  176. } catch (IOException ioe) {
  177. ioe.printStackTrace();
  178. } finally {
  179. try {
  180. if (maski.isEmpty())
  181. bw.close();
  182. } catch (Exception ex) {
  183. System.out.println("exc");
  184. }
  185. }
  186. }
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement