mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Merge pull request #68 from KylerSmith/stack-node-implementation
Stack node implementation
This commit is contained in:
@@ -2,27 +2,27 @@ import java.util.*;
|
||||
|
||||
/**
|
||||
* 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 class bfs{
|
||||
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<vertices;i++){
|
||||
if(a[pop][i]!=0 && b[i]!=(byte)0 && b[i]!=(byte)1 ){
|
||||
st.push(i);
|
||||
b[i]=(byte)0; //assigning waiting status
|
||||
}}}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The main method
|
||||
*
|
||||
* The main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
@@ -49,9 +49,9 @@ public class bfs{
|
||||
int vertices=in.nextInt(),source=in.nextInt();
|
||||
byte [][]a=new byte [vertices][vertices];
|
||||
//initially all elements of a are initialized with value zero
|
||||
|
||||
|
||||
for(int i=0;i<vertices;i++){
|
||||
int size =in.nextInt();
|
||||
int size =in.nextInt();
|
||||
for(int j=0;j<size;j++){
|
||||
a[i][in.nextInt()]=1; //taking adjacency entries by assigning 1
|
||||
}
|
||||
183
data_structures/Stacks/NodeStack.java
Normal file
183
data_structures/Stacks/NodeStack.java
Normal file
@@ -0,0 +1,183 @@
|
||||
/**
|
||||
* Implementation of a stack using nodes.
|
||||
* Unlimited size, no arraylist.
|
||||
*
|
||||
* @author Kyler Smith, 2017
|
||||
*/
|
||||
|
||||
|
||||
public class NodeStack<Item> {
|
||||
|
||||
/**
|
||||
* Entry point for the program.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
NodeStack<Integer> Stack = new NodeStack<Integer>();
|
||||
|
||||
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<Item> newNs = new NodeStack<Item>(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;
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,13 @@ import java.util.ArrayList;
|
||||
/**
|
||||
* 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 @@ class Stack{
|
||||
|
||||
/**
|
||||
* Adds an element to the top of the stack
|
||||
*
|
||||
*
|
||||
* @param value The element added
|
||||
*/
|
||||
public void push(int value){
|
||||
@@ -42,13 +42,13 @@ class Stack{
|
||||
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 @@ class Stack{
|
||||
|
||||
/**
|
||||
* Returns the element at the top of the stack
|
||||
*
|
||||
*
|
||||
* @return element at the top of the stack
|
||||
*/
|
||||
public int peek(){
|
||||
@@ -76,7 +76,7 @@ class Stack{
|
||||
|
||||
/**
|
||||
* Returns true if the stack is empty
|
||||
*
|
||||
*
|
||||
* @return true if the stack is empty
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
@@ -85,16 +85,16 @@ class Stack{
|
||||
|
||||
/**
|
||||
* 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 @@ class Stack{
|
||||
/**
|
||||
* 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<Integer> 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 @@ class Stack2{
|
||||
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 @@ class Stack2{
|
||||
|
||||
/**
|
||||
* 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 class Stacks{
|
||||
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
|
||||
Reference in New Issue
Block a user