Crazy

Компанија

Oct 29th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class DLLNode<E>{
  4.     DLLNode<E> prev;
  5.     DLLNode<E> next;
  6.     E ID;
  7.     E plata;
  8.     DLLNode( E ID, E plata, DLLNode<E> prev, DLLNode<E> next){
  9.         this.prev = prev;
  10.         this.next = next;
  11.         this.ID = ID;
  12.         this.plata = plata;
  13.     }
  14. }
  15.  
  16. class DLL<E>{
  17.     DLLNode<E> first;
  18.     DLLNode<E> last;
  19.    
  20.     DLL(){
  21.         first = null;
  22.         last = null;
  23.     }
  24.     public void insertFirst(E o, E o1) {
  25.         DLLNode<E> ins = new DLLNode<E>(o,o1, null, first);
  26.         if (first == null)
  27.             last = ins;
  28.         else
  29.             first.prev = ins;
  30.         first = ins;
  31.     }
  32.     public void insertLast(E o, E o1) {
  33.         if (first == null)
  34.             insertFirst(o, o1);
  35.         else {
  36.             DLLNode<E> ins = new DLLNode<E>(o, o1, last, null);
  37.             last.next = ins;
  38.             last = ins;
  39.         }
  40.     }
  41.     public void izbrisi(E plata){
  42.     DLLNode<E> tmp = first;
  43.         while(tmp != null){
  44.             if((Integer)tmp.plata < (Integer)plata)delete(tmp);
  45.             tmp = tmp.next;
  46.         }
  47.    
  48.     }
  49.     public E deleteFirst() {
  50.         if (first != null) {
  51.             DLLNode<E> tmp = first;
  52.             first = first.next;
  53.             if (first != null) first.prev = null;
  54.             if (first == null)
  55.                 last = null;
  56.             return tmp.ID;
  57.         } else
  58.             return null;
  59.     }
  60.     public E deleteLast() {
  61.         if (first != null) {
  62.             if (first.next == null)
  63.                 return deleteFirst();
  64.             else {
  65.                 DLLNode<E> tmp = last;
  66.                 last = last.prev;
  67.                 last.next = null;
  68.                 return tmp.ID;
  69.             }
  70.         }
  71.         // else throw Exception
  72.         return null;
  73.     }
  74.     public E delete(DLLNode<E> node) {
  75.         if(node==first){
  76.             deleteFirst();
  77.             return node.ID;
  78.         }
  79.         if(node==last){
  80.             deleteLast();
  81.             return node.ID;
  82.         }
  83.         node.prev.next = node.next;
  84.         node.next.prev = node.prev;
  85.         return node.ID;
  86.        
  87.     }
  88.     public void podredipoId(){
  89.     DLLNode<E> tmp = first;
  90.         while(tmp !=null){
  91.             DLLNode<E> tmp1 = tmp.next;
  92.             while(tmp1 != null){
  93.                 if((Integer)tmp1.ID > (Integer)tmp.ID){
  94.                     E idd = tmp.ID;
  95.                     E plataa = tmp.plata;
  96.                     tmp.ID  = tmp1.ID;
  97.                     tmp.plata = tmp1.plata;
  98.                     tmp1.ID = idd;
  99.                     tmp1.plata = plataa;
  100.                 }
  101.                 tmp1 = tmp1.next;
  102.             }
  103.             tmp = tmp.next;
  104.         }
  105.        
  106.     }
  107.    
  108.     public void pecati(){
  109.     DLLNode<E> tmp = first;
  110.      int gol=0;
  111.         while(tmp != null){
  112.             System.out.println(tmp.ID+" "+tmp.plata);
  113.             tmp = tmp.next;
  114.             gol++;
  115.         }
  116.         if(gol == 0)System.out.println("nema");
  117.     }
  118. }
  119.  
  120. public class DLLKompanija{
  121.  
  122.     public static void main(String [] args){
  123.         Scanner m = new Scanner(System.in);    
  124.         int cit = m.nextInt();
  125.         DLL<Integer> vraboteni = new DLL<>();
  126.         for(int i=0; i<cit; i++){
  127.             int id = m.nextInt();
  128.             int plata = m.nextInt();
  129.             vraboteni.insertLast(id, plata);
  130.         }
  131.        
  132.         int proveri = m.nextInt();
  133.         vraboteni.izbrisi(proveri);
  134.         vraboteni.podredipoId();
  135.         vraboteni.pecati();    
  136.     }
  137.  
  138. }
Add Comment
Please, Sign In to add comment