Skip to content

Stack node implementation #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 14 additions & 14 deletions bfs.java → data_structures/Graphs/bfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@

/**
* 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()){
b[st.peek()]=(byte)0; //assigning waiting status
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[]){
Scanner in=new Scanner(System.in);
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
}
Expand Down
File renamed without changes.
File renamed without changes.
183 changes: 183 additions & 0 deletions data_structures/Stacks/NodeStack.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading