Advertisement
Filip_Markoski

First Midterm.10 Company (Solved)

Oct 30th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.02 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class SLL {
  6.     private SLLNode first;
  7.  
  8.     class SLLNode implements Comparable<SLLNode> {
  9.         protected int id;
  10.         protected int salary;
  11.         protected SLLNode succ;
  12.  
  13.         public SLLNode(int id, int salary, SLLNode succ) {
  14.             this.id = id;
  15.             this.salary = salary;
  16.             this.succ = succ;
  17.         }
  18.  
  19.         @Override
  20.         public int compareTo(SLLNode node) {
  21.             if (this.id < node.id) return -1;
  22.             else if (this.id > node.id) return 1;
  23.             return 0;
  24.         }
  25.  
  26.         @Override
  27.         public String toString() {
  28.             return String.format("(%d,%d)", id, salary);
  29.         }
  30.     }
  31.  
  32.     public SLL() {
  33.         // Construct an empty SLL
  34.         this.first = null;
  35.     }
  36.  
  37.     public void deleteList() {
  38.         first = null;
  39.     }
  40.  
  41.     public int length() {
  42.         int ret;
  43.         if (first != null) {
  44.             SLLNode tmp = first;
  45.             ret = 1;
  46.             while (tmp.succ != null) {
  47.                 tmp = tmp.succ;
  48.                 ret++;
  49.             }
  50.             return ret;
  51.         } else
  52.             return 0;
  53.  
  54.     }
  55.  
  56.  
  57.     public void insertFirst(int id, int salary) {
  58.         SLLNode ins = new SLLNode(id, salary, first);
  59.         first = ins;
  60.     }
  61.  
  62.     public void insertLast(int id, int salary) {
  63.         if (first != null) {
  64.             SLLNode tmp = first;
  65.             while (tmp.succ != null)
  66.                 tmp = tmp.succ;
  67.             SLLNode ins = new SLLNode(id, salary, null);
  68.             tmp.succ = ins;
  69.         } else {
  70.             insertFirst(id, salary);
  71.         }
  72.     }
  73.  
  74.     public SLLNode getFirst() {
  75.         return first;
  76.     }
  77.  
  78.     public SLL copy() {
  79.         SLLNode first = getFirst();
  80.         SLL result = new SLL();
  81.         while (first != null) {
  82.             result.insertLast(first.id, first.salary);
  83.             first = first.succ;
  84.         }
  85.         return result;
  86.     }
  87.  
  88.     @Override
  89.     public String toString() {
  90.         if (first != null) {
  91.             SLLNode temp = first;
  92.             StringBuffer stringBuffer = new StringBuffer();
  93.             stringBuffer.append(temp + " -> ");
  94.             while (temp.succ != null) {
  95.                 temp = temp.succ;
  96.                 stringBuffer.append(temp + " -> ");
  97.             }
  98.             stringBuffer.append("END\n");
  99.             return stringBuffer.toString();
  100.         } else {
  101.             return "The linked list is empty.";
  102.         }
  103.     }
  104.  
  105.     public SLL delete_smaller_from(int value) {
  106.         SLL result = new SLL();
  107.         SLLNode first = this.getFirst();
  108.  
  109.         while (first != null) {
  110.             if (first.salary >= value) {
  111.                 result.insertLast(first.id, first.salary);
  112.             }
  113.             first = first.succ;
  114.         }
  115.         return result;
  116.     }
  117.  
  118.     public SLL sort_descending() {
  119.         SLLNode first = getFirst();
  120.         int temp;
  121.         SLLNode previous = null, current = null, following = null;
  122.         if (first == null) {
  123.             System.out.println("nema");
  124.             return this;
  125.         }
  126.  
  127.         for (int i = 0; i < 10; i++) {
  128.             first = getFirst();
  129.             while (first != null && first.succ != null) {
  130.                 current = first;
  131.                 following = first.succ;
  132.                 if (current.id < following.id) {
  133.                 /* SWAP DATA */
  134.                     temp = current.id;
  135.                     current.id = following.id;
  136.                     following.id = temp;
  137.  
  138.                     temp = current.salary;
  139.                     current.salary = following.salary;
  140.                     following.salary = temp;
  141.                 }
  142.                 //System.out.print(this.toString());
  143.                 first = first.succ;
  144.             }
  145.         }
  146.  
  147.         return this;
  148.     }
  149.  
  150.    /* public SLL sort_descending() {
  151.         SLLNode first = getFirst();
  152.  
  153.         if (first == null) {
  154.             System.out.println("nema");
  155.             return this;
  156.         }
  157.  
  158.         SLLNode previous = null, current = null, following = null, temp = null;
  159.         while (first.succ != null) {
  160.             *//*previous = first;
  161.             current.succ = first.succ;
  162.             following.succ = current;*//*
  163.             if (first.id < first.succ.id) {
  164.                 //keep a pointer to succ element of first
  165.                 current = first.succ;
  166.                 //make first point to succ element
  167.                 first.succ = current.succ;
  168.                 current.succ = first;
  169.                 first = current;
  170.                 //current has moved one step back it points to first.
  171.                 //so get it to the finished swap position
  172.                 current = current.succ;
  173.                 while (current.succ != null && current.succ.succ != null) {
  174.                     temp = current.succ.succ;
  175.                     current.succ.succ = temp.succ;
  176.                     temp.succ = current.succ;
  177.                     current.succ = temp;
  178.                     current = temp.succ;
  179.                 }
  180.             }
  181.             first = first.succ;
  182.         }
  183.         return this;
  184.     }*/
  185.  
  186.     /*public SLL sort_descending() {
  187.         if (first == null) {
  188.             System.out.print("nema");
  189.             return this;
  190.         }
  191.  
  192.         boolean flag = true;
  193.         while (flag) {
  194.             flag = false;
  195.             SLLNode current = first;
  196.             SLLNode following = first;
  197.             SLLNode temp;
  198.             while (current.succ != null)
  199.                 if (current.id < current.succ.id) {
  200.                     flag = true;
  201.                     temp = current.succ;
  202.                     current.succ = temp.succ;
  203.                     temp.succ = current;
  204.                     if (current == first) following = first = temp;
  205.                     else following = following.succ = temp;
  206.                 } else {
  207.                     if (current != first) following = following.succ;
  208.                     current = current.succ;
  209.                 }
  210.         }
  211.         return this;
  212.     }*/
  213.  
  214.     public void pecati(SLL lista) {
  215.         SLLNode p = lista.first;
  216.         while (p != null) {
  217.             System.out.println(p.id + " " + p.salary);
  218.             p = p.succ;
  219.         }
  220.     }
  221.  
  222.  
  223. }
  224.  
  225. public class SLLKompanija {
  226.     public static void main(String[] args) throws IOException {
  227.  
  228.         SLL lista1 = new SLL();
  229.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  230.                 System.in));
  231.         String s = stdin.readLine();
  232.         int N = Integer.parseInt(s);
  233.  
  234.         for (int i = 0; i < N; i++) {
  235.             s = stdin.readLine();
  236.             String s1 = stdin.readLine();
  237.             lista1.insertLast(Integer.parseInt(s), Integer.parseInt(s1));
  238.         }
  239.         s = stdin.readLine();
  240.  
  241.         lista1 = lista1.delete_smaller_from(Integer.parseInt(s));
  242.         if (lista1 != null) {
  243.             lista1 = lista1.sort_descending();
  244.             lista1.pecati(lista1);
  245.         }
  246.  
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement