Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unit4.collectionsLib.Node;
- public class Test {
- public static void main(String[] args) {
- Node<Pair> list =
- new Node<Pair>(new Pair('R', 1),
- new Node<Pair>(new Pair('L', 3),
- new Node<Pair>(new Pair('S', 4)
- )));
- printList(list);
- printList(uncompress(list));
- }
- public static <T> void printList(Node<T> list) {
- Node<T> p = list;
- while (p != null) {
- System.out.print(p.getValue() + " -> ");
- p = p.getNext();
- }
- System.out.println("null");
- }
- public static Node<Character> uncompress(Node<Pair> list) {
- Node<Character> letters = null;
- Node<Pair> pos = list;
- Node<Character> temp = null;
- Node<Character> add = null;
- while (pos != null) {
- char c = pos.getValue().getTav();
- int n = pos.getValue().getNum();
- add = new Node<>(c);
- if (letters == null) {
- letters = add;
- }
- if (temp != null && c != temp.getValue()) {
- temp.setNext(add);
- }
- temp = add;
- for (int i = 1; i < n; i++) {
- add = new Node<>(c);
- temp.setNext(add);
- temp = temp.getNext();
- }
- pos = pos.getNext();
- }
- return letters;
- }
- }
- public class Pair {
- private char tav;
- private int num;
- public Pair(char tav, int num){
- this.tav = tav;
- this.num = num;
- }
- public void setTav(char tav){
- this.tav = tav;
- }
- public void setNum(int num){
- this.num = num;
- }
- public char getTav(){
- return this.tav;
- }
- public int getNum(){
- return this.num;
- }
- public String toString() {
- return String.format("('%s',%d)", this.tav, this.num);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement