Advertisement
Guest User

Kompanija

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