Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- public class Blockchain {
- public static void main(String args[]) {
- String text = "3\n" +
- "I'm the Genesis Block\n" +
- "1\n" +
- "I'm the first block\n" +
- "2\n" +
- "I'm the second block\n" +
- "1\n" +
- "3\n" +
- "I'm the third block\n" +
- "2\n" +
- "-1328248291\n" +
- "3\n" +
- "KRAJ";
- Scanner scanner = new Scanner(text);
- //Scanner scanner = new Scanner(System.in);
- SLL<Block> list = new SLL<>();
- int numOfBlocks = scanner.nextInt();
- scanner.nextLine();
- Block rootBlock = new Block(0, scanner.nextLine(), 0);
- list.insertFirst(rootBlock);
- for (int i = 1; i < numOfBlocks; i++) {
- int index = Integer.parseInt(scanner.nextLine());
- String data = scanner.nextLine();
- list.insertLastBlock(index, data);
- }
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- System.out.println("read line: "+line);
- if (line.equals("KRAJ")) {
- break;
- }
- int cmd = Integer.parseInt(line);
- switch (cmd) {
- case 1:
- int index = Integer.parseInt(scanner.nextLine());
- String data = scanner.nextLine();
- list.insertLastBlock(index, data);
- break;
- case 2: //delete
- int hash = Integer.parseInt(scanner.nextLine());
- list.delete(hash);
- break;
- case 3:
- list.checkValidity();
- break;
- }
- }
- System.out.println(list);
- }
- }
- class SLL<E> {
- private SLLNode<E> first;
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- SLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- @Override
- public String toString() {
- StringBuilder ret = new StringBuilder();
- if (first != null) {
- SLLNode<E> tmp = first;
- ret.append(tmp).append("\n->");
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret.append(tmp).append("\n->");
- }
- } else
- ret = new StringBuilder("Prazna lista!!!");
- return ret.toString();
- }
- public void insertFirst(E o) {
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;
- }
- public void insertAfter(E o, SLLNode<E> node) {
- if (node != null) {
- SLLNode<E> ins = new SLLNode<E>(o, node.succ);
- node.succ = ins;
- } else {
- System.out.println("Dadenot jazol e null");
- }
- }
- public void insertBefore(E o, SLLNode<E> before) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if (first == before) {
- this.insertFirst(o);
- return;
- }
- //ako first!=before
- while (tmp.succ != before)
- tmp = tmp.succ;
- if (tmp.succ == before) {
- SLLNode<E> ins = new SLLNode<E>(o, before);
- tmp.succ = ins;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- }
- public void insertLast(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode<E> ins = new SLLNode<E>(o, null);
- tmp.succ = ins;
- } else {
- insertFirst(o);
- }
- }
- public void insertLastBlock(int index, String data) {
- if (first != null) {
- SLLNode<Block> tmp = (SLLNode<Block>) first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- Block block = new Block(index, data, tmp.element.hash);
- SLLNode<Block> ins = new SLLNode<Block>(block, null);
- tmp.succ = ins;
- }
- }
- public E deleteFirst() {
- if (first != null) {
- SLLNode<E> tmp = first;
- first = first.succ;
- return tmp.element;
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public E delete(SLLNode<E> node) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if (first == node) {
- return this.deleteFirst();
- }
- while (tmp.succ != node && tmp.succ.succ != null)
- tmp = tmp.succ;
- if (tmp.succ == node) {
- tmp.succ = tmp.succ.succ;
- return node.element;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- return null;
- }
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public E delete(int hash) {
- if (first != null) {
- SLLNode<Block> f = (SLLNode<Block>) first;
- SLLNode<Block> tmp = f;
- if (f.element.hash == hash) {
- System.out.print("Can't delete the rootBlock");
- return null;
- }
- while (tmp.succ.element.hash != hash && tmp.succ.succ != null) {
- tmp = tmp.succ;
- }
- if (tmp.succ.element.hash == hash) {
- tmp.succ = tmp.succ.succ;
- while(tmp.succ != null){
- tmp.succ.element.prevHash = tmp.element.hash;
- tmp.succ.element.hash = tmp.succ.element.hashCode();
- tmp = tmp.succ;
- }
- return null;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- return null;
- }
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public void checkValidity(){
- SLLNode<Block> tmp = (SLLNode<Block>)first;
- int prev = 0;
- while (tmp.succ != null) {
- if(tmp.element.prevHash != prev){
- System.out.println("Nevalidna");
- return;
- }
- prev = tmp.element.hash;
- tmp = tmp.succ;
- }
- System.out.println("Validna");
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public SLLNode<E> find(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.element != o && tmp.succ != null)
- tmp = tmp.succ;
- if (tmp.element == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return first;
- }
- public Iterator<E> iterator() {
- // Return an iterator that visits all elements of this list, in left-to-right order.
- return new LRIterator<E>();
- }
- // //////////Inner class ////////////
- private class LRIterator<E> implements Iterator<E> {
- private SLLNode<E> place, curr;
- private LRIterator() {
- place = (SLLNode<E>) first;
- curr = null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null)
- throw new NoSuchElementException();
- E nextElem = place.element;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- public void remove() {
- //Not implemented
- }
- }
- public void mirror() {
- if (first != null) {
- //m=nextsucc, p=tmp,q=next
- SLLNode<E> tmp = first;
- SLLNode<E> newsucc = null;
- SLLNode<E> next;
- while (tmp != null) {
- next = tmp.succ;
- tmp.succ = newsucc;
- newsucc = tmp;
- tmp = next;
- }
- first = newsucc;
- }
- }
- public void merge(SLL<E> in) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- tmp.succ = in.getFirst();
- } else {
- first = in.getFirst();
- }
- }
- }
- class Block {
- public int index;
- public String data;
- public int hash;
- public int prevHash;
- public Block(int index, String data, int prevHash) {
- this.index = index;
- this.data = data;
- this.prevHash = prevHash;
- this.hash = this.hashCode();
- }
- @Override
- public int hashCode() {
- return (Integer.valueOf(index) + data + Integer.valueOf(prevHash)).hashCode();
- }
- @Override
- public String toString() {
- return "Block{" +
- "index=" + index +
- ", data='" + data + '\'' +
- ", hash=" + hash +
- ", prevHash=" + prevHash +
- '}';
- }
- }
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement