SashkoKlincharov

[Java][АПС] - Компанија

Jan 23rd, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. Компанија Problem 2 (1 / 22)
  2. Податоците за плати на вработените во една компанија привремено се чуваат во двострано поврзана листа. Во секој јазол од листата се чува единствен ID на вработениот и неговата плата. Потребно е да се отстранат сите вработени со помали плати од даден износ, а остатокот да се прикажат во опаѓачки редослед во однос на ID-то.
  3.  
  4. Во првиот ред од влезот е даден бројот на вработени, потоа наизменично се дадени ID-та и платата за секој од вработените и во последниот ред е износот во однос на кој ќе се отстрануваат вработените. На излез се печати листа (ID, плата) во опаѓачки редослед според ID-то на секој од вработените.
  5.  
  6. Доколку нема вработени со плата поголема од дадената да се испечати: nema
  7.  
  8. Име на класата: DLLKompanija
  9.  
  10. Sample input
  11. 3
  12. 1111
  13. 19000
  14. 2222
  15. 22000
  16. 1155
  17. 60000
  18. 30000
  19. Sample output
  20. 1155 60000
  21.  
  22.  
  23. import java.util.Scanner;
  24. import java.util.Iterator;
  25. import java.util.NoSuchElementException;
  26.  
  27. class SLL<E> {
  28. private SLLNode<E> first;
  29.  
  30. public SLL() {
  31. // Construct an empty SLL
  32. this.first = null;
  33. }
  34.  
  35. public void deleteList() {
  36. first = null;
  37. }
  38.  
  39. public int length() {
  40. int ret;
  41. if (first != null) {
  42. SLLNode<E> tmp = first;
  43. ret = 1;
  44. while (tmp.succ != null) {
  45. tmp = tmp.succ;
  46. ret++;
  47. }
  48. return ret;
  49. } else
  50. return 0;
  51.  
  52. }
  53.  
  54. @Override
  55. public String toString() {
  56. String ret = new String();
  57. if (first != null) {
  58. SLLNode<E> tmp = first;
  59. ret += tmp + "->";
  60. while (tmp.succ != null) {
  61. tmp = tmp.succ;
  62. ret += tmp + "->";
  63. }
  64. } else
  65. ret = "Prazna lista!!!";
  66. return ret;
  67. }
  68.  
  69. public void insertFirst(E o, int p) {
  70. SLLNode<E> ins = new SLLNode<E>(o,p, first);
  71. first = ins;
  72. }
  73.  
  74. public void insertAfter(E o,int p, SLLNode<E> node) {
  75. if (node != null) {
  76. SLLNode<E> ins = new SLLNode<E>(o,p, node.succ);
  77. node.succ = ins;
  78. } else {
  79. System.out.println("Dadenot jazol e null");
  80. }
  81. }
  82.  
  83. public void insertBefore(E o,int p, SLLNode<E> before) {
  84.  
  85. if (first != null) {
  86. SLLNode<E> tmp = first;
  87. if(first==before){
  88. this.insertFirst(o,p);
  89. return;
  90. }
  91. //ako first!=before
  92. while (tmp.succ != before)
  93. tmp = tmp.succ;
  94. if (tmp.succ == before) {
  95. SLLNode<E> ins = new SLLNode<E>(o,p, before);
  96. tmp.succ = ins;
  97. } else {
  98. System.out.println("Elementot ne postoi vo listata");
  99. }
  100. } else {
  101. System.out.println("Listata e prazna");
  102. }
  103. }
  104.  
  105. public void insertLast(E o, int p) {
  106. if (first != null) {
  107. SLLNode<E> tmp = first;
  108. while (tmp.succ != null)
  109. tmp = tmp.succ;
  110. SLLNode<E> ins = new SLLNode<E>(o,p, null);
  111. tmp.succ = ins;
  112. } else {
  113. insertFirst(o,p);
  114. }
  115. }
  116.  
  117. public E deleteFirst() {
  118. if (first != null) {
  119. SLLNode<E> tmp = first;
  120. first = first.succ;
  121. return tmp.element;
  122. } else {
  123. System.out.println("Listata e prazna");
  124. return null;
  125. }
  126. }
  127.  
  128. public E delete(SLLNode<E> node) {
  129. if (first != null) {
  130. SLLNode<E> tmp = first;
  131. if(first ==node){
  132. return this.deleteFirst();
  133. }
  134. while (tmp.succ != node && tmp.succ.succ != null)
  135. tmp = tmp.succ;
  136. if (tmp.succ == node) {
  137. tmp.succ = tmp.succ.succ;
  138. return node.element;
  139. } else {
  140. System.out.println("Elementot ne postoi vo listata");
  141. return null;
  142. }
  143. } else {
  144. System.out.println("Listata e prazna");
  145. return null;
  146. }
  147.  
  148. }
  149.  
  150. public SLLNode<E> getFirst() {
  151. return first;
  152. }
  153.  
  154. public SLLNode<E> find(E o) {
  155. if (first != null) {
  156. SLLNode<E> tmp = first;
  157. while (tmp.element != o && tmp.succ != null)
  158. tmp = tmp.succ;
  159. if (tmp.element == o) {
  160. return tmp;
  161. } else {
  162. System.out.println("Elementot ne postoi vo listata");
  163. }
  164. } else {
  165. System.out.println("Listata e prazna");
  166. }
  167. return first;
  168. }
  169.  
  170. public Iterator<E> iterator () {
  171. // Return an iterator that visits all elements of this list, in left-to-right order.
  172. return new LRIterator<E>();
  173. }
  174.  
  175. // //////////Inner class ////////////
  176.  
  177. private class LRIterator<E> implements Iterator<E> {
  178.  
  179. private SLLNode<E> place, curr;
  180.  
  181. private LRIterator() {
  182. place = (SLLNode<E>) first;
  183. curr = null;
  184. }
  185.  
  186. public boolean hasNext() {
  187. return (place != null);
  188. }
  189.  
  190. public E next() {
  191. if (place == null)
  192. throw new NoSuchElementException();
  193. E nextElem = place.element;
  194. curr = place;
  195. place = place.succ;
  196. return nextElem;
  197. }
  198.  
  199. public void remove() {
  200. //Not implemented
  201. }
  202. }
  203.  
  204. public void mirror(){
  205. if (first != null) {
  206. //m=nextsucc, p=tmp,q=next
  207. SLLNode<E> tmp = first;
  208. SLLNode<E> newsucc = null;
  209. SLLNode<E> next;
  210.  
  211. while(tmp != null){
  212. next = tmp.succ;
  213. tmp.succ = newsucc;
  214. newsucc = tmp;
  215. tmp = next;
  216. }
  217. first = newsucc;
  218. }
  219.  
  220. }
  221.  
  222. public void merge (SLL<E> in){
  223. if (first != null) {
  224. SLLNode<E> tmp = first;
  225. while(tmp.succ != null)
  226. tmp = tmp.succ;
  227. tmp.succ = in.getFirst();
  228. }
  229. else{
  230. first = in.getFirst();
  231. }
  232. }
  233. }
  234.  
  235. class SLLNode<E> {
  236. protected E element;
  237. protected int plata;
  238. protected SLLNode<E> succ;
  239.  
  240. public SLLNode(E elem,int plata, SLLNode<E> succ) {
  241. this.element = elem;
  242. this.plata = plata;
  243. this.succ = succ;
  244. }
  245.  
  246. @Override
  247. public String toString() {
  248. return element.toString();
  249. }
  250. }
  251.  
  252. public class SLLKompanija {
  253. public static void company(SLL<Integer> lista,int iznos){
  254. int suma = 0;
  255. SLLNode<Integer> tmp = lista.getFirst();
  256. while(tmp!=null) {
  257. if(tmp.plata<iznos) {
  258. lista.delete(tmp);
  259. }
  260. tmp = tmp.succ;
  261. }
  262. //System.out.print(lista.length());
  263. tmp = lista.getFirst();
  264. while(tmp!=null) {
  265. SLLNode<Integer> tmp2 = tmp.succ;
  266. while(tmp2!=null) {
  267. if(tmp.element<tmp2.element) {
  268. int smeniid = tmp.element;
  269. tmp.element = tmp2.element;
  270. tmp2.element = smeniid;
  271.  
  272. int smeniplata = tmp.plata;
  273. tmp.plata = tmp2.plata;
  274. tmp2.plata = smeniplata;
  275. System.out.println(lista);
  276. }
  277. tmp2 = tmp2.succ;
  278. }
  279. tmp = tmp.succ;
  280. }
  281. System.out.print(lista);
  282. // tmp = lista.getFirst();
  283. // if(tmp==null) {System.out.print("nema");}else {
  284. // while(tmp!=null) {
  285. // System.out.print(tmp.element + " " + tmp.plata + "\n");
  286. // tmp = tmp.succ;
  287. // }
  288. // }
  289.  
  290.  
  291. }
  292. public static void main(String[] args) {
  293. Scanner input = new Scanner(System.in);
  294. int n = input.nextInt();
  295. SLL<Integer> lista = new SLL<Integer>();
  296. for(int i=0;i<n;i++) {
  297. lista.insertLast(input.nextInt(), input.nextInt());
  298. }
  299. int iznos = input.nextInt();
  300. company(lista,iznos);
  301.  
  302. }
  303.  
  304. }
Add Comment
Please, Sign In to add comment