Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 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.    /* public E  deleteFirst() {
  70.         if (first != null) {
  71.             SLLNode tmp = first;
  72.             first = first.succ;
  73.             return tmp.element;
  74.         } else {
  75.             System.out.println("Listata e prazna");
  76.             return null;
  77.         }
  78.     }
  79.  
  80.    
  81.     public E delete(SLLNode node) {
  82.         if (first != null) {
  83.             SLLNode tmp = first;
  84.             if(first ==node){
  85.                 return this.deleteFirst();
  86.             }
  87.             while (tmp.succ != node&&tmp.succ.succ != null)
  88.                 tmp = tmp.succ;
  89.             if (tmp.succ == node) {
  90.                 tmp.succ = tmp.succ.succ;
  91.                 return node.element;
  92.             } else {
  93.                 System.out.println("Elementot ne postoi vo listata");
  94.                 return null;
  95.             }
  96.         } else {
  97.             System.out.println("Listata e prazna");
  98.             return null;
  99.         }
  100.  
  101.     }*/
  102.  
  103.    
  104.    
  105.     public SLL brisi_pomali_od(int iznos) {
  106.         SLLNode jazol1=this.getFirst();
  107.         SLL rezultat=new SLL();
  108.          while(jazol1!=null)
  109.         {
  110.             if(jazol1.plata>=iznos)
  111.             {
  112.                
  113.                 rezultat.insertLast(jazol1.id,jazol1.plata);
  114.                 jazol1=jazol1.succ;
  115.  
  116.             }
  117.              else {
  118.              
  119.                  jazol1=jazol1.succ;
  120.              }
  121.            
  122.         }
  123.         return rezultat;
  124.     }
  125.    
  126.     public SLL sortiraj_opagacki() {
  127.         SLL list=new SLL();
  128.         SLLNode jazol1=this.getFirst();
  129.         SLLNode jazol2=null;
  130.         SLLNode pom=this.getFirst();
  131.         int tmp;
  132.         int tmp1;
  133.         while(jazol1!=null)
  134.         {
  135.             if(jazol1.succ!=null)
  136.             {
  137.                 jazol2=jazol1.succ;
  138.                
  139.             }
  140.             else break;
  141.             while(jazol2!=null)
  142.             {
  143.                
  144.                 if(jazol1.id<jazol2.id)
  145.                 {
  146.                     tmp=jazol1.id;
  147.                     tmp1=jazol1.plata;
  148.                     //pom.id=jazol1.id;
  149.                     //pom.plata=jazol1.plata;
  150.                     jazol1.id=jazol2.id;
  151.                     jazol1.plata=jazol2.plata;
  152.                     jazol2.id=tmp;
  153.                     jazol2.plata=tmp1;
  154.                     jazol2=jazol2.succ;
  155.                    
  156.                 }
  157.                 else
  158.                 {
  159.                     jazol2=jazol2.succ;
  160.                    
  161.                 }
  162.             }
  163.             jazol1=jazol1.succ;
  164.          
  165.         }
  166.         return this;
  167.     }
  168.     public void pecati (SLL lista)
  169.     {
  170.         SLLNode p=lista.first;
  171.         while(p!=null)
  172.         {
  173.             System.out.println(p.id+" "+p.plata);
  174.             p=p.succ;
  175.         }
  176.     }
  177.     public void nema()
  178.     {
  179.         System.out.println("nema");
  180.     }
  181.    
  182. }
  183. public class SLLKompanija {
  184.     public static void main(String[] args) throws IOException {
  185.  
  186.         SLL lista1 = new SLL();
  187.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  188.                 System.in));
  189.         String s = stdin.readLine();
  190.         int N = Integer.parseInt(s);
  191.        
  192.         for (int i = 0; i < N; i++) {
  193.             s=stdin.readLine();
  194.             String s1=stdin.readLine();
  195.             lista1.insertLast(Integer.parseInt(s),Integer.parseInt(s1));
  196.         }
  197.         s = stdin.readLine();
  198.        
  199.         lista1=lista1.brisi_pomali_od(Integer.parseInt(s));
  200.         if(lista1!=null)
  201.         {
  202.             lista1=lista1.sortiraj_opagacki();
  203.             lista1.pecati(lista1);
  204.            
  205.         }
  206.          
  207.      
  208.            
  209.        
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement