Advertisement
Filip_Markoski

[ADSy] - Blockchain

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