Advertisement
Josif_tepe

Untitled

May 25th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 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 plata;
  10.     protected SLLNode succ;
  11.  
  12.     public SLLNode(int id,int plata, SLLNode succ) {
  13.         this.id = id;
  14.         this.plata=plata;
  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 plata) {
  50.         SLLNode ins = new SLLNode(id,plata, first);
  51.         first = ins;
  52.     }
  53.  
  54.     public void insertLast(int id,int plata) {
  55.         if (first != null) {
  56.             SLLNode tmp = first;
  57.             while (tmp.succ != null)
  58.                 tmp = tmp.succ;
  59.             SLLNode ins = new SLLNode(id, plata, null);
  60.             tmp.succ = ins;
  61.         } else {
  62.             insertFirst(id,plata);
  63.         }
  64.     }
  65.  
  66.     public SLLNode getFirst() {
  67.         return first;
  68.     }
  69.  
  70.  
  71.     public SLL brisi_pomali_od(int iznos) {
  72.         SLL nova_lista = new SLL();
  73.         SLLNode head = getFirst();
  74.         while(head != null) {
  75.             if(head.plata > iznos) {
  76.                 nova_lista.insertLast(head.id, head.plata);
  77.             }
  78.             head = head.succ;
  79.         }
  80.         return nova_lista;
  81.     }
  82.  
  83.     public SLL sortiraj_opagacki() {
  84.         SLL nova_lista = new SLL();
  85.         SLLNode head = getFirst();
  86.         if(head == null) {
  87.             System.out.println("nema");
  88.             return new SLL();
  89.         }
  90.         while(head != null) {
  91.             SLLNode sosed = head.succ;
  92.             while(sosed != null) {
  93.                 if(head.id < sosed.id) {
  94.                     int tmp_id = head.id;
  95.                     int tmp_plata = head.plata;
  96.                     head.id = sosed.id;
  97.                     head.plata = sosed.plata;
  98.  
  99.                     sosed.id = tmp_id;
  100.                     sosed.plata = tmp_plata;
  101.                 }
  102.                 sosed = sosed.succ;
  103.             }
  104.             nova_lista.insertLast(head.id, head.plata);
  105.             head = head.succ;
  106.         }
  107.         return nova_lista;
  108.     }
  109.     public void pecati (SLL lista)
  110.     {
  111.         SLLNode p=lista.first;
  112.         while(p!=null)
  113.         {
  114.             System.out.println(p.id+","+p.plata);
  115.             p=p.succ;
  116.         }
  117.     }
  118.  
  119. }
  120. public class SLLKompanija {
  121.     public static void main(String[] args) throws IOException {
  122.  
  123.         SLL lista1 = new SLL();
  124.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  125.                 System.in));
  126.         String s = stdin.readLine();
  127.         int N = Integer.parseInt(s);
  128.  
  129.         for (int i = 0; i < N; i++) {
  130.             s=stdin.readLine();
  131.             String s1=stdin.readLine();
  132.             lista1.insertLast(Integer.parseInt(s),Integer.parseInt(s1));
  133.         }
  134.         s = stdin.readLine();
  135.  
  136.         lista1=lista1.brisi_pomali_od(Integer.parseInt(s));
  137.         if(lista1!=null)
  138.         {
  139.             lista1=lista1.sortiraj_opagacki();
  140.             lista1.pecati(lista1);
  141.         }
  142.  
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement