Advertisement
StefiIOE

[ADS]Company

Nov 16th, 2020
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5.  
  6.  
  7. class SLLNode {
  8.     protected int id;
  9.     protected int salary;
  10.     protected SLLNode succ;
  11.  
  12.     public SLLNode(int id,int salary, SLLNode succ) {
  13.         this.id = id;
  14.         this.salary=salary;
  15.         this.succ = succ;
  16.     }
  17.  
  18.    
  19. }
  20.  
  21. class SLL {
  22.     private SLLNode first;
  23.  
  24.     public SLL() {
  25.         // Construct an empty SLL
  26.         this.first = null;
  27.     }
  28.  
  29.     public void deleteList() {
  30.         first = null;
  31.     }
  32.  
  33.     public int length() {
  34.         int ret;
  35.         if (first != null) {
  36.             SLLNode tmp = first;
  37.             ret = 1;
  38.             while (tmp.succ != null) {
  39.                 tmp = tmp.succ;
  40.                 ret++;
  41.             }
  42.             return ret;
  43.         } else
  44.             return 0;
  45.  
  46.     }
  47.  
  48.  
  49.     public void insertFirst(int id, int salary) {
  50.         SLLNode ins = new SLLNode(id,salary, first);
  51.         first = ins;
  52.     }
  53.  
  54.     public void insertLast(int id,int salary) {
  55.         if (first != null) {
  56.             SLLNode tmp = first;
  57.             while (tmp.succ != null)
  58.                 tmp = tmp.succ;
  59.             SLLNode ins = new SLLNode(id, salary, null);
  60.             tmp.succ = ins;
  61.         } else {
  62.             insertFirst(id,salary);
  63.         }
  64.     }
  65.  
  66.     public SLLNode getFirst() {
  67.         return first;
  68.     }
  69.    
  70.    
  71.     public SLL delete_smaller_from(int value) {
  72.         SLL result = new SLL ();
  73.         SLLNode first = this.getFirst();
  74.        
  75.         while(first != null){
  76.             if(first.salary>=value){
  77.                 result.insertLast(first.id, first.salary);
  78.             }
  79.             first = first.succ;
  80.         }
  81.         return result;
  82.     }
  83.    
  84.     public SLL sort_descending() {
  85.         SLLNode first = getFirst();
  86.         int tmp;
  87.         SLLNode current = null;
  88.         SLLNode following = null;
  89.         if(first == null){
  90.             System.out.println("empty");
  91.             return this;
  92.         }
  93.         for(int i = 0 ; i < 100; i++){
  94.             first =getFirst();
  95.             while(first!=null && first.succ != null){
  96.                 current = first;
  97.                 following = current.succ;
  98.                 if(current.id <following.id){
  99.  
  100.                     tmp=current.id;
  101.                     current.id = following.id;
  102.                     following.id = tmp;
  103.  
  104.                     tmp = current.salary;
  105.                     current.salary = following.salary;
  106.                     following.salary = tmp;
  107.  
  108.                 }
  109.                 first=first.succ;
  110.             }
  111.            
  112.         }
  113.         return this;
  114.     }
  115.     public void pecati (SLL lista)
  116.     {
  117.         SLLNode p=lista.first;
  118.         while(p!=null)
  119.         {
  120.             System.out.println(p.id+" "+p.salary);
  121.             p=p.succ;
  122.         }
  123.     }
  124.    
  125. }
  126. public class SLLKompanija {
  127.     public static void main(String[] args) throws IOException {
  128.  
  129.         SLL lista1 = new SLL();
  130.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  131.                 System.in));
  132.         String s = stdin.readLine();
  133.         int N = Integer.parseInt(s);
  134.        
  135.         for (int i = 0; i < N; i++) {
  136.             s=stdin.readLine();
  137.             String s1=stdin.readLine();
  138.             lista1.insertLast(Integer.parseInt(s),Integer.parseInt(s1));
  139.         }
  140.         s = stdin.readLine();
  141.        
  142.         lista1=lista1.delete_smaller_from(Integer.parseInt(s));
  143.         if(lista1!=null)
  144.         {
  145.             lista1=lista1.sort_descending();
  146.             lista1.pecati(lista1);
  147.         }
  148.        
  149.     }
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement