Skip to content

Data Structures

Examples

1. Stack

class Sample{
    public int[] itsStack = new int[];
    public boolean isEmpty(){

    }

    public boolean isFull(){

    }

    public boolean push(){

    }

    public boolean pop(){

    }

    public int top(){

    }

    public void display(){

    }


}

class Stack{
    public static void main(String[] args){

    }
}

2. Tower Of Hanoi

// TOH refers to Tower Of Hanoi.
public class TOH{
    // Defining a method which accepts parameters as: no of disks, name of rods (should match according to name of formal arguments passed).
    public void tower(int noOfDisks, String sourceRod, String destinationRod, String auxiliaryRod){
        if(noOfDisks == 1){
            System.out.format("\nRod %d moved from %s to %s",1,sourceRod,destinationRod);
            return ;
        }else{
            // Key Algorithms :
            tower(noOfDisks - 1, "Source Rod","Auxiliary Rod","Destination Rod");
            System.out.format("\nDisk %d moved from %s to %s",noOfDisks,sourceRod,destinationRod);
            tower(noOfDisks - 1, "Auxiliary Rod","Destination Rod","Source Rod");
        }        
    }

}
Driver Code
// TOHDemo refers to Tower Of Hanoi Demo.
class TOHDemo{
    public static void main(String[] args){
        // This accepts the no of disks
        int noOfDisks = 4;
        TOH objectOne = new TOH();
        // Passing the no of disks and rod names to recursive function.
        objectOne.tower(noOfDisks,"Source Rod","Destination Rod","Auxilary Rod");
    }
}

2. Block Chain

import java.util.Arrays;

public class Block{
    private int previousHash;
    private String[] transaction;
    private int blockHash;

    public Block(int previousHash, String[] transaction){
        this.previousHash = previousHash;
        this.transaction = transaction;

        Object[] contents = {Arrays.hashCode(transaction),previousHash};
        this.blockHash = Arrays.hashCode(contents);
    }

    public int getPreviousHash(){
        return previousHash;
    }

    public String[] getTransaction(){
        return transaction;
    }
}
Driver Code
import java.lang.*;
import java.util.*;
import java.text.*;

public class Main{
    ArrayList<Block> blockchain = new ArrayList<>();
    public static void main(String[] args){
        Block genesisBlock = new Block(previousHash)
    }
}