HristijanBosheski

Компанија [АПС Вежби за прв колоквиум]

Jan 19th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.30 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Iterator;
  5. import java.util.NoSuchElementException;
  6.  
  7. class SLL<E> {
  8.     private SLLNode<E> first;
  9.  
  10.     public SLL() {
  11.         // Construct an empty SLL
  12.         this.first = null;
  13.     }
  14.  
  15.     public void deleteList() {
  16.         first = null;
  17.     }
  18.  
  19.     public int length() {
  20.         int ret;
  21.         if (first != null) {
  22.             SLLNode<E> tmp = first;
  23.             ret = 1;
  24.             while (tmp.succ != null) {
  25.                 tmp = tmp.succ;
  26.                 ret++;
  27.             }
  28.             return ret;
  29.         } else
  30.             return 0;
  31.  
  32.     }
  33.  
  34.     @Override
  35.     public String toString() {
  36.         String ret = new String();
  37.         if (first != null) {
  38.             SLLNode<E> tmp = first;
  39.             ret += tmp + "->";
  40.             while (tmp.succ != null) {
  41.                 tmp = tmp.succ;
  42.                 ret += tmp + "->";
  43.             }
  44.         } else
  45.             ret = "Prazna lista!!!";
  46.         return ret;
  47.     }
  48.  
  49.     public void insertFirst(E o) {
  50.         SLLNode<E> ins = new SLLNode<E>(o, first);
  51.         first = ins;
  52.     }
  53.  
  54.     public void insertAfter(E o, SLLNode<E> node) {
  55.         if (node != null) {
  56.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  57.             node.succ = ins;
  58.         } else {
  59.             System.out.println("Dadenot jazol e null");
  60.         }
  61.     }
  62.  
  63.     public void insertBefore(E o, SLLNode<E> before) {
  64.        
  65.         if (first != null) {
  66.             SLLNode<E> tmp = first;
  67.             if(first==before){
  68.                 this.insertFirst(o);
  69.                 return;
  70.             }
  71.             //ako first!=before
  72.             while (tmp.succ != before)
  73.                 tmp = tmp.succ;
  74.             if (tmp.succ == before) {
  75.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  76.                 tmp.succ = ins;
  77.             } else {
  78.                 System.out.println("Elementot ne postoi vo listata");
  79.             }
  80.         } else {
  81.             System.out.println("Listata e prazna");
  82.         }
  83.     }
  84.  
  85.     public void insertLast(E o) {
  86.         if (first != null) {
  87.             SLLNode<E> tmp = first;
  88.             while (tmp.succ != null)
  89.                 tmp = tmp.succ;
  90.             SLLNode<E> ins = new SLLNode<E>(o, null);
  91.             tmp.succ = ins;
  92.         } else {
  93.             insertFirst(o);
  94.         }
  95.     }
  96.  
  97.     public E deleteFirst() {
  98.         if (first != null) {
  99.             SLLNode<E> tmp = first;
  100.             first = first.succ;
  101.             return tmp.element;
  102.         } else {
  103.             System.out.println("Listata e prazna");
  104.             return null;
  105.         }
  106.     }
  107.  
  108.     public E delete(SLLNode<E> node) {
  109.         if (first != null) {
  110.             SLLNode<E> tmp = first;
  111.             if(first ==node){
  112.                 return this.deleteFirst();
  113.             }
  114.             while (tmp.succ != node&&tmp.succ.succ != null)
  115.                 tmp = tmp.succ;
  116.             if (tmp.succ == node) {
  117.                 tmp.succ = tmp.succ.succ;
  118.                 return node.element;
  119.             } else {
  120.                 System.out.println("Elementot ne postoi vo listata");
  121.                 return null;
  122.             }
  123.         } else {
  124.             System.out.println("Listata e prazna");
  125.             return null;
  126.         }
  127.  
  128.     }
  129.  
  130.     public SLLNode<E> getFirst() {
  131.         return first;
  132.     }
  133.    
  134.     public SLLNode<E> find(E o) {
  135.         if (first != null) {
  136.             SLLNode<E> tmp = first;
  137.             while (tmp.element != o && tmp.succ != null)
  138.                 tmp = tmp.succ;
  139.             if (tmp.element == o) {
  140.                 return tmp;
  141.             } else {
  142.                 System.out.println("Elementot ne postoi vo listata");
  143.             }
  144.         } else {
  145.             System.out.println("Listata e prazna");
  146.         }
  147.         return first;
  148.     }
  149.    
  150.     public Iterator<E> iterator () {
  151.     // Return an iterator that visits all elements of this list, in left-to-right order.
  152.         return new LRIterator<E>();
  153.     }
  154.  
  155.     // //////////Inner class ////////////
  156.  
  157.     private class LRIterator<E> implements Iterator<E> {
  158.  
  159.         private SLLNode<E> place, curr;
  160.  
  161.         private LRIterator() {
  162.             place = (SLLNode<E>) first;
  163.             curr = null;
  164.         }
  165.  
  166.         public boolean hasNext() {
  167.             return (place != null);
  168.         }
  169.  
  170.         public E next() {
  171.             if (place == null)
  172.                 throw new NoSuchElementException();
  173.             E nextElem = place.element;
  174.             curr = place;
  175.             place = place.succ;
  176.             return nextElem;
  177.         }
  178.  
  179.         public void remove() {
  180.             //Not implemented
  181.         }
  182.     }
  183.    
  184.     public void mirror(){
  185.         if (first != null) {
  186.             //m=nextsucc, p=tmp,q=next
  187.             SLLNode<E> tmp = first;
  188.             SLLNode<E> newsucc = null;
  189.             SLLNode<E> next;
  190.            
  191.             while(tmp != null){
  192.                 next = tmp.succ;
  193.                 tmp.succ = newsucc;
  194.                 newsucc = tmp;
  195.                 tmp = next;
  196.             }
  197.             first = newsucc;
  198.         }
  199.        
  200.     }
  201.    
  202.     public void merge (SLL<E> in){
  203.         if (first != null) {
  204.             SLLNode<E> tmp = first;
  205.             while(tmp.succ != null)
  206.                 tmp = tmp.succ;
  207.             tmp.succ = in.getFirst();
  208.         }
  209.         else{
  210.             first = in.getFirst();
  211.         }
  212.     }
  213. }
  214.  
  215. class SLLNode<E> {
  216.     protected E element;
  217.     protected SLLNode<E> succ;
  218.  
  219.     public SLLNode(E elem, SLLNode<E> succ) {
  220.         this.element = elem;
  221.         this.succ = succ;
  222.     }
  223.  
  224.     @Override
  225.     public String toString() {
  226.         return element.toString();
  227.     }
  228. }
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235. class Vraboten{
  236.     int id;
  237.     int plata;
  238.    
  239.     public Vraboten(int id, int plata){
  240.         this.id=id;
  241.         this.plata=plata;
  242.     }
  243.    
  244.     public int getId(){
  245.         return this.id;
  246.     }
  247.     public int getPlata(){
  248.         return this.plata;
  249.     }
  250.    
  251.    
  252. }
  253.  
  254.  
  255.  
  256. public class SLLKompanija {
  257.    
  258.     public static void brisi(SLL<Vraboten> lista,int max){
  259.         SLLNode<Vraboten> tmp = lista.getFirst();
  260.         int br = 0;
  261.        
  262.         while (tmp != null){
  263.             if (tmp.element.plata < max){
  264.                 lista.delete(tmp);
  265.             }
  266.             else{
  267.                 br ++;
  268.             }
  269.             tmp = tmp.succ;
  270.         }
  271.         if (br ==0){
  272.             System.out.println("nema");
  273.         }
  274.         else{
  275.             sortiraj(lista);
  276.         }
  277.    
  278.     }
  279.    
  280.     public static void sortiraj (SLL<Vraboten> lista){
  281.         SLLNode<Vraboten> tmp = lista.getFirst();
  282.        
  283.         while (tmp !=null){
  284.             SLLNode<Vraboten> tmp1 = tmp;
  285.             while (tmp1 != null){
  286.                 if (tmp.element.id < tmp1.element.id){
  287.                     int pom = tmp.element.id;
  288.                     tmp.element.id=tmp1.element.id;
  289.                     tmp1.element.id=pom;
  290.                    
  291.                     int pom1 = tmp.element.plata;
  292.                     tmp.element.plata=tmp1.element.plata;
  293.                     tmp1.element.plata=pom1;
  294.                    
  295.                 }  
  296.                 tmp1 = tmp1.succ;
  297.             }
  298.             tmp = tmp.succ;
  299.         }
  300.        
  301.         pecati(lista);
  302.     }
  303.    
  304.     public static void pecati (SLL<Vraboten> lista){
  305.         SLLNode<Vraboten> tmp = lista.getFirst();
  306.        
  307.         while (tmp !=  null){
  308.             System.out.println(tmp.element.id + " " + tmp.element.plata );
  309.             tmp = tmp.succ;
  310.         }
  311.    
  312.        
  313.     }  
  314.        
  315.     public static void main(String[] args) throws IOException {
  316.        
  317.         SLL<Vraboten> lista = new SLL<Vraboten>();
  318.        
  319.         BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
  320.        
  321.         String s = br.readLine();
  322.         int n = Integer.parseInt(s);
  323.        
  324.         for (int i = 0 ; i<n;i++){
  325.             s=br.readLine();
  326.             String s1 = br.readLine();
  327.    
  328.             lista.insertLast(new Vraboten (Integer.parseInt(s),Integer.parseInt(s1))); 
  329.         }
  330.        
  331.         s = br.readLine();
  332.         int max = Integer.parseInt(s);
  333.        
  334.        
  335.         brisi(lista,max);
  336.     }
  337.  
  338. }
Add Comment
Please, Sign In to add comment