Advertisement
Mitrezzz

[АПС] SLLKompanija

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