Advertisement
JStefan

APS_LAB2_Task2

Oct 25th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class DLLKompanija {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int n = scanner.nextInt();
  9.  
  10.         Company company = new Company();
  11.  
  12.         for (int i = 0; i < n; i++) {
  13.             int id = scanner.nextInt();
  14.             int salary = scanner.nextInt();
  15.             company.insertLast(id, salary);
  16.         }
  17.  
  18.         int salary = scanner.nextInt();
  19.  
  20.         company.removeEmployeesWithLowerSalary(salary);
  21.         company.sortByIds();
  22.  
  23.         System.out.println(company);
  24.     }
  25.  
  26. }
  27.  
  28. class Employee {
  29.     protected int ID;
  30.     protected int salary;
  31.     protected Employee pred, succ;
  32.  
  33.     public Employee(int ID, int salary, Employee pred, Employee succ) {
  34.         this.ID = ID;
  35.         this.salary = salary;
  36.         this.pred = pred;
  37.         this.succ = succ;
  38.     }
  39.  
  40.     public int getID() {
  41.         return ID;
  42.     }
  43.  
  44.     public int getSalary() {
  45.         return salary;
  46.     }
  47.  
  48.     @Override
  49.     public String toString() {
  50.         return ID + " " + salary;
  51.     }
  52. }
  53.  
  54. class Company {
  55.     Employee first, last;
  56.  
  57.     public Company() { first = last = null; }
  58.  
  59.     public void deleteList() { first = last = null; }
  60.  
  61.     public int length() {
  62.         int ret;
  63.         if (first != null) {
  64.             Employee tmp = first;
  65.             ret = 1;
  66.             while (tmp.succ != null) {
  67.                 tmp = tmp.succ;
  68.                 ret++;
  69.             }
  70.             return ret;
  71.         } else
  72.             return 0;
  73.  
  74.     }
  75.  
  76.     public void insertFirst(int ID, int salary) {
  77.         Employee ins = new Employee(ID, salary, null, first);
  78.         if (first == null)
  79.             last = ins;
  80.         else
  81.             first.pred = ins;
  82.         first = ins;
  83.     }
  84.  
  85.     public void insertLast(int ID, int salary) {
  86.         if (first == null)
  87.             insertFirst(ID, salary);
  88.         else {
  89.             Employee ins = new Employee(ID, salary, last, null);
  90.             last.succ = ins;
  91.             last = ins;
  92.         }
  93.     }
  94.  
  95.     public void insertAfter(int ID, int salary, Employee after) {
  96.         if (after == last) {
  97.             insertLast(ID, salary);
  98.             return;
  99.         }
  100.         Employee ins = new Employee(ID, salary, after, after.succ);
  101.         after.succ.pred = ins;
  102.         after.succ = ins;
  103.     }
  104.  
  105.     public int deleteFirst() {
  106.         if (first != null) {
  107.             Employee tmp = first;
  108.             first = first.succ;
  109.             if(first != null) first.pred = null;
  110.             if (first == null)
  111.                 last = null;
  112.             return tmp.getID();
  113.         } else
  114.             return -1;
  115.     }
  116.  
  117.     public int deleteLast() {
  118.         if (first != null) {
  119.             if (first.succ == null)
  120.                 return deleteFirst();
  121.             else {
  122.                 Employee tmp = last;
  123.                 last = last.pred;
  124.                 last.succ = null;
  125.                 return tmp.getID();
  126.             }
  127.         }
  128.         // else throw Exception
  129.         return -1;
  130.     }
  131.  
  132.     public int delete(Employee node) {
  133.         if (node == first) {
  134.             deleteFirst();
  135.             return node.getID();
  136.         }
  137.         if (node == last) {
  138.             deleteLast();
  139.             return node.getID();
  140.         }
  141.         node.pred.succ = node.succ;
  142.         node.succ.pred = node.pred;
  143.         return node.getID();
  144.     }
  145.  
  146.     public void removeEmployeesWithLowerSalary(int salary) {
  147.         Employee current = first;
  148.  
  149.         while(current != null) {
  150.             if(current.getSalary() < salary) {
  151.                 if(current == last) deleteLast();
  152.                 else delete(current);
  153.             }
  154.             current = current.succ;
  155.         }
  156.     }
  157.  
  158.     public void sortByIds() {
  159.         Employee tmp1 = first;
  160.         Employee tmp2 = null;
  161.  
  162.         while (tmp1 != null) {
  163.             tmp2 = tmp1.succ;
  164.             while (tmp2 != null) {
  165.                 if(tmp1.getID() < tmp2.getID()) {
  166.                     int id = tmp1.getID();
  167.                     int sal = tmp1.getSalary();
  168.                     tmp1.ID = tmp2.ID;
  169.                     tmp1.salary = tmp2.salary;
  170.                     tmp2.ID = id;
  171.                     tmp2.salary = sal;
  172.                 }
  173.                 tmp2 = tmp2.succ;
  174.             }
  175.             tmp1 = tmp1.succ;
  176.         }
  177.     }
  178.  
  179.     @Override
  180.     public String toString() {
  181.         StringBuilder stringBuilder = new StringBuilder();
  182.  
  183.         Employee it = first;
  184.  
  185.         while (it != null) {
  186.             stringBuilder.append(it + "\n");
  187.             it = it.succ;
  188.         }
  189.  
  190.         if(stringBuilder.toString().length() == 0) return "nema";
  191.         else return stringBuilder.toString();
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement