Advertisement
fensa08

#APS Lab 2/2

Oct 25th, 2019
2,627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.00 KB | None | 0 0
  1. Компанија Problem 2 (1 / 4)
  2.  
  3. Податоците за плати на вработените во една компанија привремено се чуваат во двострано поврзана листа. Во секој јазол од листата се чува единствен ID на вработениот и неговата плата. Потребно е да се отстранат сите вработени со помали плати од даден износ, а остатокот да се прикажат во опаѓачки редослед во однос на ID-то.
  4.  
  5. Во првиот ред од влезот е даден бројот на вработени, потоа наизменично се дадени ID-та и платата за секој од вработените и во последниот ред е износот во однос на кој ќе се отстрануваат вработените. На излез се печати листа (ID, плата) во опаѓачки редослед според ID-то на секој од вработените.
  6.  
  7. Доколку нема вработени со плата поголема од дадената да се испечати: nema
  8.  
  9. Име на класата: DLLKompanija
  10.  
  11. ========================================================================================================================================
  12.  
  13. import java.io.BufferedReader;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16.  
  17. public class DLLKompanija {
  18.  
  19.     public static class DLLNode<E> {
  20.         protected int ID;
  21.         protected int plata;
  22.         protected DLLNode<E> pred, succ;
  23.  
  24.         public DLLNode(int ID, int plata, DLLNode<E> pred, DLLNode<E> succ) {
  25.             this.ID = ID;
  26.             this.plata = plata;
  27.             this.pred = pred;
  28.             this.succ = succ;
  29.         }
  30.  
  31.         @Override
  32.         public String toString() {
  33.             return ID + " " + plata;
  34.         }
  35.     }
  36.  
  37.  
  38.     public static class DLL<E> {
  39.         private DLLNode<E> first, last;
  40.  
  41.         public DLL() {
  42.             // Construct an empty SLL
  43.             this.first = null;
  44.             this.last = null;
  45.         }
  46.  
  47.         public void deleteList() {
  48.             first = null;
  49.             last = null;
  50.         }
  51.  
  52.         public int length() {
  53.             int ret;
  54.             if (first != null) {
  55.                 DLLNode<E> tmp = first;
  56.                 ret = 1;
  57.                 while (tmp.succ != null) {
  58.                     tmp = tmp.succ;
  59.                     ret++;
  60.                 }
  61.                 return ret;
  62.             } else
  63.                 return 0;
  64.  
  65.         }
  66.  
  67.       /*  public DLLNode<E> find(E o) {
  68.             if (first != null) {
  69.                 DLLNode<E> tmp = first;
  70.                 while (tmp.element != o&&tmp.succ != null)
  71.                     tmp = tmp.succ;
  72.                 if (tmp.element == o) {
  73.                     return tmp;
  74.                 } else {
  75.                     System.out.println("Elementot ne postoi vo listata");
  76.                 }
  77.             } else {
  78.                 System.out.println("Listata e prazna");
  79.             }
  80.             return first;
  81.         }*/
  82.  
  83.         public void insertFirst(int id, int plata) {
  84.             DLLNode<E> ins = new DLLNode<E>(id,plata, null, first);
  85.             if (first == null)
  86.                 last = ins;
  87.             else
  88.                 first.pred = ins;
  89.             first = ins;
  90.         }
  91.  
  92.         public void insertLast(int id, int plata) {
  93.             if (first == null)
  94.                 insertFirst(id,plata);
  95.             else {
  96.                 DLLNode<E> ins = new DLLNode<E>(id, plata , last, null);
  97.                 last.succ = ins;
  98.                 last = ins;
  99.             }
  100.         }
  101.  
  102.         public void insertAfter(int id, int plata, DLLNode<E> after) {
  103.             if(after==last){
  104.                 insertLast(id, plata);
  105.                 return;
  106.             }
  107.             DLLNode<E> ins = new DLLNode<E>(id, plata , after, after.succ);
  108.             after.succ.pred = ins;
  109.             after.succ = ins;
  110.         }
  111.  
  112.         public void insertBefore(int id, int plata, DLLNode<E> before) {
  113.             if(before == first){
  114.                 insertFirst(id, plata);
  115.                 return;
  116.             }
  117.             DLLNode<E> ins = new DLLNode<E>(id,plata, before.pred, before);
  118.             before.pred.succ = ins;
  119.             before.pred = ins;
  120.         }
  121.  
  122.         public void deleteFirst() {
  123.             if (first != null) {
  124.                 DLLNode<E> tmp = first;
  125.                 first = first.succ;
  126.                 if (first != null) first.pred = null;
  127.                 if (first == null)
  128.                     last = null;
  129.                 //return tmp.element;
  130.             }
  131.                 //return null;
  132.         }
  133.  
  134.         public void deleteLast() {
  135.             if (first != null) {
  136.                 if (first.succ == null)
  137.                      deleteFirst();
  138.                 else {
  139.                     DLLNode<E> tmp = last;
  140.                     last = last.pred;
  141.                     last.succ = null;
  142.                     //return tmp.element;
  143.                 }
  144.             }
  145.             // else throw Exception
  146.             //return null;
  147.         }
  148.  
  149.         public void delete(DLLNode<E> node) {
  150.             if(node==first){
  151.                 deleteFirst();
  152.                 return;
  153.                 //return node.element;
  154.             }
  155.             if(node==last){
  156.                 deleteLast();
  157.                 return;
  158.                 //return node.element;
  159.             }
  160.             node.pred.succ = node.succ;
  161.             node.succ.pred = node.pred;
  162.             //return node.element;
  163.  
  164.         }
  165.  
  166.         @Override
  167.         public String toString() {
  168.             String ret = new String();
  169.             if (first != null) {
  170.                 DLLNode<E> tmp = first;
  171.                 ret += tmp + "<->";
  172.                 while (tmp.succ != null) {
  173.                     tmp = tmp.succ;
  174.                     ret += tmp + "<->";
  175.                 }
  176.             } else
  177.                 ret = "Prazna lista!!!";
  178.             return ret;
  179.         }
  180.  
  181.         public String toStringR() {
  182.             String ret = new String();
  183.             if (last != null) {
  184.                 DLLNode<E> tmp = last;
  185.                 ret += tmp + "<->";
  186.                 while (tmp.pred != null) {
  187.                     tmp = tmp.pred;
  188.                     ret += tmp + "<->";
  189.                 }
  190.             } else
  191.                 ret = "Prazna lista!!!";
  192.             return ret;
  193.         }
  194.  
  195.         public DLLNode<E> getFirst() {
  196.             return first;
  197.         }
  198.  
  199.         public DLLNode<E> getLast() {
  200.  
  201.             return last;
  202.         }
  203.  
  204.         public void izvadiDupliIPrebroj(){
  205.  
  206.         }
  207.     }
  208.  
  209.     public static void deleteElements(DLL<Integer> lista, int plata){
  210.  
  211.         // todo Proba da rabotam so pointeri
  212.  
  213.         DLLNode<Integer> p = lista.getFirst();
  214.  
  215.         while(p != null){
  216.  
  217.             if(p.plata < plata){
  218.                 DLLNode<Integer> temp = p;
  219.                 p = p.succ;
  220.                 lista.delete(temp);
  221.             }else{
  222.                 p = p.succ;
  223.             }
  224.         }
  225.  
  226.         // sort the list
  227.         if(lista.length() == 0){
  228.             System.out.println("nema");
  229.             return;
  230.         }
  231.         sortList(lista);
  232.         DLLNode<Integer> pok = lista.getFirst();
  233.         while(pok.succ != null){
  234.             System.out.println( pok.ID + " " + pok.plata);
  235.             pok = pok.succ;
  236.         }
  237.         System.out.println(pok.ID + " " + pok.plata);
  238.  
  239.     }
  240.  
  241.     public static void sortList(DLL<Integer> list){
  242.  
  243.         DLLNode<Integer> sortedFirst = null;
  244.         DLLNode<Integer> sorted = null;
  245.         DLLNode<Integer> max = null;
  246.         DLLNode<Integer> pok = list.getFirst();
  247.  
  248.         while(list.length() != 0){
  249.             max = list.getFirst();
  250.             pok = list.getFirst();
  251.             while(pok != null){
  252.  
  253.                 if(pok.ID > max.ID){
  254.                     max = pok;
  255.                 }
  256.                 pok = pok.succ;
  257.             }
  258.          // go ima max;
  259.            if(sortedFirst == null){
  260.                sortedFirst = max;
  261.                sorted = max;
  262.            }else{
  263.                sorted.succ = max;
  264.                sorted = sorted.succ;
  265.            }
  266.            list.delete(max);
  267.         }
  268.  
  269.         list.first =  sortedFirst;
  270.     }
  271.  
  272.     public static void main(String []args) throws IOException {
  273.  
  274.         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  275.         int n = Integer.parseInt(input.readLine());
  276.  
  277.         DLL<Integer> lista = new DLL<Integer>();
  278.         for(int i = 0; i < n; i++){
  279.             Integer id = Integer.parseInt(input.readLine());
  280.             Integer plata = Integer.parseInt(input.readLine());
  281.             lista.insertLast(id,plata);
  282.         }
  283.  
  284.         Integer plata = Integer.parseInt(input.readLine());
  285.         deleteElements(lista,plata);
  286.  
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement