SashkoKlincharov

[Java][АПС] - Email

Feb 17th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.93 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Iterator;
  5. import java.util.NoSuchElementException;
  6.  
  7.  
  8. class SLLNode<E> {
  9. protected E naslov;
  10. protected E tekst;
  11. protected SLLNode<E> succ;
  12.  
  13. public SLLNode(E naslov,E tekst, SLLNode<E> succ) {
  14. this.naslov = naslov;
  15. this.tekst = tekst;
  16. this.succ = succ;
  17. }
  18.  
  19. @Override
  20. public String toString() {
  21. return naslov.toString() + " " + tekst.toString();
  22. }
  23. }
  24.  
  25. class SLL<E> {
  26. private SLLNode<E> first;
  27.  
  28. public SLL() {
  29. // Construct an empty SLL
  30. this.first = null;
  31. }
  32.  
  33. public void deleteList() {
  34. first = null;
  35. }
  36.  
  37. public int length() {
  38. int ret;
  39. if (first != null) {
  40. SLLNode<E> tmp = first;
  41. ret = 1;
  42. while (tmp.succ != null) {
  43. tmp = tmp.succ;
  44. ret++;
  45. }
  46. return ret;
  47. } else
  48. return 0;
  49.  
  50. }
  51.  
  52. @Override
  53. public String toString() {
  54. String ret = new String();
  55. if (first != null) {
  56. SLLNode<E> tmp = first;
  57. ret += tmp + "\n";
  58. while (tmp.succ != null) {
  59. tmp = tmp.succ;
  60. ret += tmp + "\n";
  61. }
  62. } else
  63. ret = "Prazna lista!!!";
  64. return ret;
  65. }
  66.  
  67. public void insertFirst(E o,E k) {
  68. SLLNode<E> ins = new SLLNode<E>(o,k, first);
  69. first = ins;
  70. }
  71.  
  72. public void insertAfter(E o,E k, SLLNode<E> node) {
  73. if (node != null) {
  74. SLLNode<E> ins = new SLLNode<E>(o,k, node.succ);
  75. node.succ = ins;
  76. } else {
  77. System.out.println("Dadenot jazol e null");
  78. }
  79. }
  80.  
  81. public void insertBefore(E o, E k, SLLNode<E> before) {
  82.  
  83. if (first != null) {
  84. SLLNode<E> tmp = first;
  85. if(first==before){
  86. this.insertFirst(o,k);
  87. return;
  88. }
  89. //ako first!=before
  90. while (tmp.succ != before)
  91. tmp = tmp.succ;
  92. if (tmp.succ == before) {
  93. SLLNode<E> ins = new SLLNode<E>(o, k, before);
  94. tmp.succ = ins;
  95. } else {
  96. System.out.println("Elementot ne postoi vo listata");
  97. }
  98. } else {
  99. System.out.println("Listata e prazna");
  100. }
  101. }
  102.  
  103. public void insertLast(E o, E k) {
  104. if (first != null) {
  105. SLLNode<E> tmp = first;
  106. while (tmp.succ != null)
  107. tmp = tmp.succ;
  108. SLLNode<E> ins = new SLLNode<E>(o, k, null);
  109. tmp.succ = ins;
  110. } else {
  111. insertFirst(o,k);
  112. }
  113. }
  114.  
  115. public E deleteFirst() {
  116. if (first != null) {
  117. SLLNode<E> tmp = first;
  118. first = first.succ;
  119. return tmp.naslov;
  120. } else {
  121. System.out.println("Listata e prazna");
  122. return null;
  123. }
  124. }
  125.  
  126. public E delete(SLLNode<E> node) {
  127. if (first != null) {
  128. SLLNode<E> tmp = first;
  129. if(first ==node){
  130. return this.deleteFirst();
  131. }
  132. while (tmp.succ != node && tmp.succ.succ != null)
  133. tmp = tmp.succ;
  134. if (tmp.succ == node) {
  135. tmp.succ = tmp.succ.succ;
  136. return node.naslov;
  137. } else {
  138. System.out.println("Elementot ne postoi vo listata");
  139. return null;
  140. }
  141. } else {
  142. System.out.println("Listata e prazna");
  143. return null;
  144. }
  145.  
  146. }
  147.  
  148. public SLLNode<E> getFirst() {
  149. return first;
  150. }
  151.  
  152. public SLLNode<E> find(E o, E k) {
  153. if (first != null) {
  154. SLLNode<E> tmp = first;
  155. while (tmp.naslov != o && tmp.succ != null)
  156. tmp = tmp.succ;
  157. if (tmp.naslov == o) {
  158. return tmp;
  159. } else {
  160. System.out.println("Elementot ne postoi vo listata");
  161. }
  162. } else {
  163. System.out.println("Listata e prazna");
  164. }
  165. return first;
  166. }
  167.  
  168. public Iterator<E> iterator () {
  169. // Return an iterator that visits all elements of this list, in left-to-right order.
  170. return new LRIterator<E>();
  171. }
  172.  
  173. // //////////Inner class ////////////
  174.  
  175. private class LRIterator<E> implements Iterator<E> {
  176.  
  177. private SLLNode<E> place, curr;
  178.  
  179. private LRIterator() {
  180. place = (SLLNode<E>) first;
  181. curr = null;
  182. }
  183.  
  184. public boolean hasNext() {
  185. return (place != null);
  186. }
  187.  
  188. public E next() {
  189. if (place == null)
  190. throw new NoSuchElementException();
  191. E nextElem = place.naslov;
  192. curr = place;
  193. place = place.succ;
  194. return nextElem;
  195. }
  196.  
  197. public void remove() {
  198. //Not implemented
  199. }
  200. }
  201.  
  202. public void mirror(){
  203. if (first != null) {
  204. //m=nextsucc, p=tmp,q=next
  205. SLLNode<E> tmp = first;
  206. SLLNode<E> newsucc = null;
  207. SLLNode<E> next;
  208.  
  209. while(tmp != null){
  210. next = tmp.succ;
  211. tmp.succ = newsucc;
  212. newsucc = tmp;
  213. tmp = next;
  214. }
  215. first = newsucc;
  216. }
  217.  
  218. }
  219.  
  220. public void merge (SLL<E> in){
  221. if (first != null) {
  222. SLLNode<E> tmp = first;
  223. while(tmp.succ != null)
  224. tmp = tmp.succ;
  225. tmp.succ = in.getFirst();
  226. }
  227. else{
  228. first = in.getFirst();
  229. }
  230. }
  231. }
  232.  
  233. public class EmailPoraki {
  234. public static void main(String [] args) throws IOException {
  235. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  236. int n = Integer.parseInt(br.readLine());
  237.  
  238. SLL<String> lista = new SLL<String>();
  239. SLLNode<String> tmp = null;
  240. String str = "";
  241. for(int i=0;i<n;i++){
  242. tmp = lista.getFirst();
  243. str = "";
  244.  
  245. String [] line = br.readLine().split(" ");
  246. String operacija = line[0]; /// zimanje info
  247. String naslov = "[" +line[1] + "]:";///
  248. int k = 2;
  249. for(k=2;k<line.length-1;k++){
  250. str += line[k] + " ";
  251. }
  252. str+=line[k];
  253. str = str.trim();
  254. if(operacija.equals("DEL")&&tmp==null)
  255. break;
  256. else
  257. if(operacija.equals("DEL")){
  258. while(true){
  259. String m = tmp.naslov.split(" ")[0].trim();
  260. if(m.equals(naslov)){
  261. lista.delete(tmp);
  262. break;
  263. }
  264. tmp = tmp.succ;
  265. }
  266. }
  267. if(tmp==null){
  268. if(operacija.equals("SND")){
  269. lista.insertLast(naslov + "", str);
  270. }
  271. else if(operacija.equals("RCV")){
  272. lista.insertLast(naslov + " >", str);
  273. }
  274. }
  275. else{
  276. while(tmp!=null){
  277. String [] split = tmp.naslov.split(" ");
  278. String gettmpnaslov = split[0].trim();
  279. if(gettmpnaslov.equals(naslov.trim())){
  280. if(operacija.equals("SND")){
  281. tmp.naslov = naslov + " >";
  282. tmp.tekst = str;
  283. break;
  284. }
  285. else if(operacija.equals("RCV")){
  286. tmp.naslov = naslov + "";
  287. tmp.tekst = str;
  288. break;
  289. }
  290. }
  291. tmp = tmp.succ;
  292. }
  293. if(tmp==null){
  294. if(operacija.equals("SND")){
  295. lista.insertLast(naslov + " >",str);
  296. }
  297. else if(operacija.equals("RCV")){
  298. lista.insertLast(naslov + " ",str);
  299. }
  300. }
  301. }
  302. }
  303. SLLNode<String> tmp2;
  304. tmp = lista.getFirst().succ;
  305. str = "";
  306.  
  307. while(tmp!=null){
  308. tmp2 = lista.getFirst();
  309.  
  310. while(tmp2!=tmp){
  311. String str1 = "";
  312. String str2 = "";
  313. if(tmp.naslov.contains(">"))
  314. str1 = tmp.naslov.split(" ")[0].trim();
  315. else
  316. str1 = tmp.naslov.trim();
  317. if(tmp2.naslov.contains(">"))
  318. str2 = tmp2.naslov.split(" ")[0].trim();
  319. else
  320. str2 = tmp2.naslov.trim();
  321.  
  322. if(str1.length()>str2.length()){
  323.  
  324. }
  325. if(str1.length()==str2.length()){
  326. if(str1.compareTo(str2)<0){
  327. // System.out.println(str1 + " " + str2);
  328. // System.out.println(lista + " opdasaspdsodpkdopas");
  329. SLLNode<String> help = tmp;
  330. SLLNode<String> help2 = tmp2;
  331. lista.insertAfter(tmp2.naslov,tmp2.tekst,tmp);
  332. lista.insertBefore(tmp.naslov,tmp.tekst,tmp2);
  333. lista.delete(help);
  334. lista.delete(help2);
  335. tmp2 = tmp2.succ;
  336. break;
  337. }
  338. }
  339. tmp2 = tmp2.succ;
  340. }
  341.  
  342. tmp = tmp.succ;
  343. }
  344. System.out.println(lista);
  345.  
  346.  
  347. }
  348. }
Add Comment
Please, Sign In to add comment