diff --git a/.DS_Store b/.DS_Store index f28585d78e4c..08cfaf755976 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/DecimalToOctal.java b/Conversions/DecimalToOctal.java similarity index 100% rename from DecimalToOctal.java rename to Conversions/DecimalToOctal.java diff --git a/CountChar.java b/Misc/CountChar.java similarity index 100% rename from CountChar.java rename to Misc/CountChar.java diff --git a/Dijkshtra.java b/Misc/Dijkshtra.java similarity index 100% rename from Dijkshtra.java rename to Misc/Dijkshtra.java diff --git a/Factorial.java b/Misc/Factorial.java similarity index 100% rename from Factorial.java rename to Misc/Factorial.java diff --git a/FindingPrimes.java b/Misc/FindingPrimes.java similarity index 100% rename from FindingPrimes.java rename to Misc/FindingPrimes.java diff --git a/ReverseString.java b/Misc/ReverseString.java similarity index 100% rename from ReverseString.java rename to Misc/ReverseString.java diff --git a/countwords.java b/Misc/countwords.java similarity index 100% rename from countwords.java rename to Misc/countwords.java diff --git a/ft.java b/Misc/ft.java similarity index 100% rename from ft.java rename to Misc/ft.java diff --git a/krishnamurthy.java b/Misc/krishnamurthy.java similarity index 100% rename from krishnamurthy.java rename to Misc/krishnamurthy.java diff --git a/removeDuplicateFromString.java b/Misc/removeDuplicateFromString.java similarity index 100% rename from removeDuplicateFromString.java rename to Misc/removeDuplicateFromString.java diff --git a/data_structures/Graphs.java b/data_structures/Graphs/Graphs.java similarity index 100% rename from data_structures/Graphs.java rename to data_structures/Graphs/Graphs.java diff --git a/bfs.java b/data_structures/Graphs/bfs.java similarity index 85% rename from bfs.java rename to data_structures/Graphs/bfs.java index 90ff4eca87ef..4f3bd62d339c 100644 --- a/bfs.java +++ b/data_structures/Graphs/bfs.java @@ -2,27 +2,27 @@ /** * Implementation of a Breadth First Search - * + * * @author Unknown * */ public class bfs{ - + /** - * The BFS implemented in code to use. - * - * @param a Structure to perform the search on + * The BFS implemented in code to use. + * + * @param a Structure to perform the search on a graph, adjacency matrix etc. * @param vertices The vertices to use * @param source The Source */ public static void bfsImplement(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices byte []b=new byte[vertices]; //flag container containing status of each vertices Arrays.fill(b,(byte)-1); //status initialization - /* code status + /* code status -1 = ready 0 = waiting 1 = processed */ - + Stack st = new Stack(vertices); //operational stack st.push(source); //assigning source while(!st.isEmpty()){ @@ -30,18 +30,18 @@ public static void bfsImplement(byte [][] a,int vertices,int source){ //passing System.out.println(st.peek()); int pop=st.peek(); b[pop]=(byte)1; //assigning processed status - st.pop(); //removing head of the queue + st.pop(); //removing head of the queue for(int i=0;i { + + /** + * Entry point for the program. + */ + public static void main(String[] args) { + NodeStack Stack = new NodeStack(); + + Stack.push(3); + Stack.push(4); + Stack.push(5); + System.out.println("Testing :"); + Stack.print(); // prints : 5 4 3 + + Integer x = Stack.pop(); // x = 5 + Stack.push(1); + Stack.push(8); + Integer y = Stack.peek(); // y = 8 + System.out.println("Testing :"); + Stack.print(); // prints : 8 1 4 3 + + System.out.println("Testing :"); + System.out.println("x : " + x); + System.out.println("y : " + y); + } + + /** + * Information each node should contain. + * @value data : information of the value in the node + * @value head : the head of the stack + * @value next : the next value from this node + * @value previous : the last value from this node + * @value size : size of the stack + */ + private Item data; + private static NodeStack head; + private NodeStack next; + private NodeStack previous; + private static int size = 0; + + + /** + * Constructors for the NodeStack. + */ + public NodeStack() { + } + + private NodeStack(Item item) { + this.data = item; + } + + /** + * Put a value onto the stack. + * + * @param item : value to be put on the stack. + */ + public void push(Item item) { + + NodeStack newNs = new NodeStack(item); + + if(this.isEmpty()) { + NodeStack.setHead(new NodeStack<>(item)); + newNs.setNext(null); + newNs.setPrevious(null); + } else { + newNs.setPrevious(NodeStack.head); + NodeStack.head.setNext(newNs); + NodeStack.head = newNs; + } + + NodeStack.setSize(NodeStack.getSize() + 1); + } + + /** + * Value to be taken off the stack. + * + * @return item : value that is returned. + */ + public Item pop() { + + Item item = (Item) NodeStack.head.getData(); + + NodeStack.head = NodeStack.head.getPrevious(); + NodeStack.head.setNext(null); + + NodeStack.setSize(NodeStack.getSize() - 1); + + return item; + } + + /** + * Value that is next to be taken off the stack. + * + * @return item : the next value that would be popped off the stack. + */ + public Item peek() { + return (Item) NodeStack.head.getData(); + } + + /** + * If the stack is empty or there is a value in. + * + * @return boolean : whether or not the stack has anything in it. + */ + public boolean isEmpty() { + return NodeStack.getSize() == 0; + } + + /** + * Returns the size of the stack. + * + * @return int : number of values in the stack. + */ + public int size() { + return NodeStack.getSize(); + } + + /** + * Print the contents of the stack in the following format. + * + * x <- head (next out) + * y + * z <- tail (first in) + * . + * . + * . + * + */ + public void print() { + for(NodeStack n = NodeStack.head; n != null; n = n.previous) { + System.out.println(n.getData().toString()); + } + } + + /** Getters and setters (private) */ + private NodeStack getHead() { + return NodeStack.head; + } + + private static void setHead(NodeStack ns) { + NodeStack.head = ns; + } + + private NodeStack getNext() { + return next; + } + + private void setNext(NodeStack next) { + this.next = next; + } + + private NodeStack getPrevious() { + return previous; + } + + private void setPrevious(NodeStack previous) { + this.previous = previous; + } + + private static int getSize() { + return size; + } + + private static void setSize(int size) { + NodeStack.size = size; + } + + private Item getData() { + return this.data; + } + + private void setData(Item item) { + this.data = item; + } +} diff --git a/data_structures/Stacks.java b/data_structures/Stacks/Stacks.java similarity index 96% rename from data_structures/Stacks.java rename to data_structures/Stacks/Stacks.java index 4e2b08545721..2158563173ba 100644 --- a/data_structures/Stacks.java +++ b/data_structures/Stacks/Stacks.java @@ -3,13 +3,13 @@ /** * This class implements a Stack using two different implementations. * Stack is used with a regular array and Stack2 uses an ArrayList. - * + * * A stack is exactly what it sounds like. An element gets added to the top of * the stack and only the element on the top may be removed. This is an example * of an array implementation of a Stack. So an element can only be added/removed * from the end of the array. In theory stack have no fixed size, but with an * array implementation it does. - * + * * @author Unknown * */ @@ -23,7 +23,7 @@ class Stack{ /** * Constructor - * + * * @param size Size of the Stack */ public Stack(int size){ @@ -34,7 +34,7 @@ public Stack(int size){ /** * Adds an element to the top of the stack - * + * * @param value The element added */ public void push(int value){ @@ -42,13 +42,13 @@ public void push(int value){ top++; stackArray[top] = value; }else{ - System.out.prinln("The stack is full, can't insert value"); + System.out.println("The stack is full, can't insert value"); } } /** * Removes the top element of the stack and returns the value you've removed - * + * * @return value popped off the Stack */ public int pop(){ @@ -62,7 +62,7 @@ public int pop(){ /** * Returns the element at the top of the stack - * + * * @return element at the top of the stack */ public int peek(){ @@ -76,7 +76,7 @@ public int peek(){ /** * Returns true if the stack is empty - * + * * @return true if the stack is empty */ public boolean isEmpty(){ @@ -85,16 +85,16 @@ public boolean isEmpty(){ /** * Returns true if the stack is full - * + * * @return true if the stack is full */ public boolean isFull(){ return(top+1 == maxSize); } - + /** * Deletes everything in the Stack - * + * * Doesn't delete elements in the array * but if you call push method after calling * makeEmpty it will overwrite previous @@ -108,41 +108,41 @@ public void makeEmpty(){ //Doesn't delete elements in the array but if you call /** * This is an ArrayList Implementation of stack, Where size is not * a problem we can extend the stack as much as we want. - * + * * @author Unknown * */ class Stack2{ /** ArrayList representation of the stack */ ArrayList stackList; - + /** * Constructor */ Stack2(){ stackList=new ArrayList<>(); } - + /** * Adds value to the end of list which * is the top for stack - * + * * @param value value to be added */ void push(int value){ stackList.add(value); } - + /** * Pops last element of list which is indeed * the top for Stack - * + * * @return Element popped */ int pop(){ - + if(!isEmpty()){ // checks for an empty Stack - + int popValue=stackList.get(stackList.size()-1); stackList.remove(stackList.size()-1); //removes the poped element from the list return popValue; @@ -151,25 +151,25 @@ int pop(){ System.out.print("The stack is already empty "); return -1; } - + } - + /** * Checks for empty Stack - * + * * @return true if stack is empty */ boolean isEmpty(){ if(stackList.isEmpty()) return true; - + else return false; - + } - + /** * Top element of stack - * + * * @return top element of stack */ int peek(){ @@ -179,14 +179,14 @@ int peek(){ /** * This class implements the Stack and Stack2 created above - * + * * @author Unknown * */ public class Stacks{ /** * Main method - * + * * @param args Command line arguments */ public static void main(String args[]){ @@ -196,21 +196,21 @@ public static void main(String args[]){ myStack.push(8); myStack.push(2); myStack.push(9); - + System.out.println("*********************Stack Array Implementation*********************"); System.out.println(myStack.isEmpty()); //will print false System.out.println(myStack.isFull()); //will print true System.out.println(myStack.peek()); //will print 9 System.out.println(myStack.pop()); //will print 9 System.out.println(myStack.peek()); // will print 2 - + Stack2 myStack2 = new Stack2(); //Declare a stack of maximum size 4 //Populate the stack myStack2.push(5); myStack2.push(8); myStack2.push(2); myStack2.push(9); - + System.out.println("*********************Stack List Implementation*********************"); System.out.println(myStack2.isEmpty()); //will print false System.out.println(myStack2.peek()); //will print 9 diff --git a/data_structures/AVLTree.java b/data_structures/Trees/AVLTree.java similarity index 100% rename from data_structures/AVLTree.java rename to data_structures/Trees/AVLTree.java