Advertisement
Guest User

Untitled

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