ivana_andreevska

Zadaca 10 - Kompanija

Nov 14th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 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.         SLLNode current=first;
  73.         SLLNode previous=first;
  74.  
  75.         while(current!=null)
  76.         {
  77.             if(current.plata<iznos)
  78.             {
  79.                 if(current==first)
  80.                 {
  81.                     first=current.succ;
  82.                     previous=first;
  83.                     current=first;
  84.                 }else{
  85.                     previous.succ=current.succ;
  86.                     current=current.succ;
  87.                 }
  88.             }else{
  89.                 previous=current;
  90.                 current=current.succ;
  91.             }
  92.         }
  93.         return this; //this e objektot koj ja sodrzi listata SLL koja treba da ja vratima
  94.     }
  95.  
  96.     public SLL sortiraj_opagacki() {
  97.  
  98.         SLLNode node;
  99.         SLLNode prev1;
  100.         SLLNode prev2;
  101.         SLLNode temp;
  102.  
  103.         for(int i=0;i<length();i++)
  104.         {
  105.             node=first.succ;
  106.             prev1=first;
  107.             prev2=first;
  108.  
  109.             while(node!=null)
  110.             {
  111.                 if(prev1.id<node.id)
  112.                 {
  113.                  temp=node.succ;
  114.                  node.succ=prev1;
  115.                  prev1.succ=temp;
  116.  
  117.                  if(prev1==first)
  118.                  {
  119.                      first=node;
  120.                  }
  121.                  else
  122.                  {
  123.                      prev2.succ=node;
  124.                  }
  125.                  prev2=node;
  126.                  node=temp;
  127.                 }
  128.                 else{
  129.                     if(prev1!=first)
  130.                     {
  131.                         prev2=prev2.succ;
  132.                     }
  133.                     prev1= prev1.succ;
  134.                     node=node.succ;
  135.                 }
  136.             }
  137.         }
  138.         return this;
  139.     }
  140.     public void pecati (SLL lista)
  141.     {
  142.         SLLNode p=lista.first;
  143.         if(p==null)
  144.         {
  145.             System.out.println("nema");
  146.             return;
  147.         }
  148.         while(p!=null)
  149.         {
  150.             System.out.println(p.id+" "+p.plata);
  151.             p=p.succ;
  152.         }
  153.     }
  154.  
  155. }
  156. public class SLLKompanija {
  157.     public static void main(String[] args) throws IOException {
  158.  
  159.         SLL lista1 = new SLL();
  160.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  161.                 System.in));
  162.         String s = stdin.readLine();
  163.         int N = Integer.parseInt(s);
  164.  
  165.         for (int i = 0; i < N; i++) {
  166.             s=stdin.readLine();
  167.             String s1=stdin.readLine();
  168.             lista1.insertLast(Integer.parseInt(s),Integer.parseInt(s1));
  169.         }
  170.         s = stdin.readLine();
  171.  
  172.         lista1=lista1.brisi_pomali_od(Integer.parseInt(s));
  173.         if(lista1!=null)
  174.         {
  175.             lista1=lista1.sortiraj_opagacki();
  176.             lista1.pecati(lista1);
  177.         }
  178.  
  179.     }
  180. }
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment