Advertisement
Guest User

עמוד 99 - תרגיל 49

a guest
Apr 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1.  
  2. import unit4.collectionsLib.Node;
  3.  
  4. public class Test {
  5.  
  6.     public static void main(String[] args) {
  7.         Node<Pair> list =
  8.                 new Node<Pair>(new Pair('R', 1),
  9.                         new Node<Pair>(new Pair('L', 3),
  10.                                 new Node<Pair>(new Pair('S', 4)
  11.                                 )));
  12.         printList(list);
  13.         printList(uncompress(list));
  14.     }
  15.  
  16.     public static <T> void printList(Node<T> list) {
  17.         Node<T> p = list;
  18.         while (p != null) {
  19.             System.out.print(p.getValue() + " -> ");
  20.  
  21.             p = p.getNext();
  22.         }
  23.         System.out.println("null");
  24.     }
  25.  
  26.     public static Node<Character> uncompress(Node<Pair> list) {
  27.         Node<Character> letters = null;
  28.         Node<Pair> pos = list;
  29.         Node<Character> temp = null;
  30.         Node<Character> add = null;
  31.         while (pos != null) {
  32.             char c = pos.getValue().getTav();
  33.             int n = pos.getValue().getNum();
  34.             add = new Node<>(c);
  35.             if (letters == null) {
  36.                 letters = add;
  37.             }
  38.             if (temp != null && c != temp.getValue()) {
  39.                 temp.setNext(add);
  40.             }
  41.  
  42.             temp = add;
  43.  
  44.             for (int i = 1; i < n; i++) {
  45.                 add = new Node<>(c);
  46.                 temp.setNext(add);
  47.                 temp = temp.getNext();
  48.             }
  49.             pos = pos.getNext();
  50.         }
  51.         return letters;
  52.     }
  53.  
  54. }
  55.  
  56.  
  57. public class Pair {
  58.     private char tav;
  59.     private int num;
  60.    
  61.     public Pair(char tav, int num){
  62.         this.tav = tav;
  63.         this.num = num;
  64.     }
  65.    
  66.     public void setTav(char tav){
  67.         this.tav = tav;
  68.     }
  69.    
  70.     public void setNum(int num){
  71.         this.num = num;
  72.     }  
  73.    
  74.     public char getTav(){
  75.         return this.tav;
  76.     }
  77.    
  78.     public int getNum(){
  79.         return this.num;
  80.     }
  81.    
  82.     public String toString() {
  83.         return String.format("('%s',%d)", this.tav, this.num);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement