Skip to content

Commit bf79830

Browse files
Merge pull request #68 from KylerSmith/stack-node-implementation
Stack node implementation
2 parents 535add3 + 8dcd01e commit bf79830

22 files changed

+228
-45
lines changed

.DS_Store

0 Bytes
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.

ft.java renamed to Misc/ft.java

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

bfs.java renamed to data_structures/Graphs/bfs.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,56 @@
22

33
/**
44
* Implementation of a Breadth First Search
5-
*
5+
*
66
* @author Unknown
77
*
88
*/
99
public class bfs{
10-
10+
1111
/**
12-
* The BFS implemented in code to use.
13-
*
14-
* @param a Structure to perform the search on
12+
* The BFS implemented in code to use.
13+
*
14+
* @param a Structure to perform the search on a graph, adjacency matrix etc.
1515
* @param vertices The vertices to use
1616
* @param source The Source
1717
*/
1818
public static void bfsImplement(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices
1919
byte []b=new byte[vertices]; //flag container containing status of each vertices
2020
Arrays.fill(b,(byte)-1); //status initialization
21-
/* code status
21+
/* code status
2222
-1 = ready
2323
0 = waiting
2424
1 = processed */
25-
25+
2626
Stack st = new Stack(vertices); //operational stack
2727
st.push(source); //assigning source
2828
while(!st.isEmpty()){
2929
b[st.peek()]=(byte)0; //assigning waiting status
3030
System.out.println(st.peek());
3131
int pop=st.peek();
3232
b[pop]=(byte)1; //assigning processed status
33-
st.pop(); //removing head of the queue
33+
st.pop(); //removing head of the queue
3434
for(int i=0;i<vertices;i++){
3535
if(a[pop][i]!=0 && b[i]!=(byte)0 && b[i]!=(byte)1 ){
3636
st.push(i);
3737
b[i]=(byte)0; //assigning waiting status
3838
}}}
3939
}
40-
41-
40+
41+
4242
/**
43-
* The main method
44-
*
43+
* The main method
44+
*
4545
* @param args Command line arguments
4646
*/
4747
public static void main(String args[]){
4848
Scanner in=new Scanner(System.in);
4949
int vertices=in.nextInt(),source=in.nextInt();
5050
byte [][]a=new byte [vertices][vertices];
5151
//initially all elements of a are initialized with value zero
52-
52+
5353
for(int i=0;i<vertices;i++){
54-
int size =in.nextInt();
54+
int size =in.nextInt();
5555
for(int j=0;j<size;j++){
5656
a[i][in.nextInt()]=1; //taking adjacency entries by assigning 1
5757
}
File renamed without changes.
File renamed without changes.

data_structures/Stacks/NodeStack.java

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/**
2+
* Implementation of a stack using nodes.
3+
* Unlimited size, no arraylist.
4+
*
5+
* @author Kyler Smith, 2017
6+
*/
7+
8+
9+
public class NodeStack<Item> {
10+
11+
/**
12+
* Entry point for the program.
13+
*/
14+
public static void main(String[] args) {
15+
NodeStack<Integer> Stack = new NodeStack<Integer>();
16+
17+
Stack.push(3);
18+
Stack.push(4);
19+
Stack.push(5);
20+
System.out.println("Testing :");
21+
Stack.print(); // prints : 5 4 3
22+
23+
Integer x = Stack.pop(); // x = 5
24+
Stack.push(1);
25+
Stack.push(8);
26+
Integer y = Stack.peek(); // y = 8
27+
System.out.println("Testing :");
28+
Stack.print(); // prints : 8 1 4 3
29+
30+
System.out.println("Testing :");
31+
System.out.println("x : " + x);
32+
System.out.println("y : " + y);
33+
}
34+
35+
/**
36+
* Information each node should contain.
37+
* @value data : information of the value in the node
38+
* @value head : the head of the stack
39+
* @value next : the next value from this node
40+
* @value previous : the last value from this node
41+
* @value size : size of the stack
42+
*/
43+
private Item data;
44+
private static NodeStack<?> head;
45+
private NodeStack<?> next;
46+
private NodeStack<?> previous;
47+
private static int size = 0;
48+
49+
50+
/**
51+
* Constructors for the NodeStack.
52+
*/
53+
public NodeStack() {
54+
}
55+
56+
private NodeStack(Item item) {
57+
this.data = item;
58+
}
59+
60+
/**
61+
* Put a value onto the stack.
62+
*
63+
* @param item : value to be put on the stack.
64+
*/
65+
public void push(Item item) {
66+
67+
NodeStack<Item> newNs = new NodeStack<Item>(item);
68+
69+
if(this.isEmpty()) {
70+
NodeStack.setHead(new NodeStack<>(item));
71+
newNs.setNext(null);
72+
newNs.setPrevious(null);
73+
} else {
74+
newNs.setPrevious(NodeStack.head);
75+
NodeStack.head.setNext(newNs);
76+
NodeStack.head = newNs;
77+
}
78+
79+
NodeStack.setSize(NodeStack.getSize() + 1);
80+
}
81+
82+
/**
83+
* Value to be taken off the stack.
84+
*
85+
* @return item : value that is returned.
86+
*/
87+
public Item pop() {
88+
89+
Item item = (Item) NodeStack.head.getData();
90+
91+
NodeStack.head = NodeStack.head.getPrevious();
92+
NodeStack.head.setNext(null);
93+
94+
NodeStack.setSize(NodeStack.getSize() - 1);
95+
96+
return item;
97+
}
98+
99+
/**
100+
* Value that is next to be taken off the stack.
101+
*
102+
* @return item : the next value that would be popped off the stack.
103+
*/
104+
public Item peek() {
105+
return (Item) NodeStack.head.getData();
106+
}
107+
108+
/**
109+
* If the stack is empty or there is a value in.
110+
*
111+
* @return boolean : whether or not the stack has anything in it.
112+
*/
113+
public boolean isEmpty() {
114+
return NodeStack.getSize() == 0;
115+
}
116+
117+
/**
118+
* Returns the size of the stack.
119+
*
120+
* @return int : number of values in the stack.
121+
*/
122+
public int size() {
123+
return NodeStack.getSize();
124+
}
125+
126+
/**
127+
* Print the contents of the stack in the following format.
128+
*
129+
* x <- head (next out)
130+
* y
131+
* z <- tail (first in)
132+
* .
133+
* .
134+
* .
135+
*
136+
*/
137+
public void print() {
138+
for(NodeStack<?> n = NodeStack.head; n != null; n = n.previous) {
139+
System.out.println(n.getData().toString());
140+
}
141+
}
142+
143+
/** Getters and setters (private) */
144+
private NodeStack<?> getHead() {
145+
return NodeStack.head;
146+
}
147+
148+
private static void setHead(NodeStack<?> ns) {
149+
NodeStack.head = ns;
150+
}
151+
152+
private NodeStack<?> getNext() {
153+
return next;
154+
}
155+
156+
private void setNext(NodeStack<?> next) {
157+
this.next = next;
158+
}
159+
160+
private NodeStack<?> getPrevious() {
161+
return previous;
162+
}
163+
164+
private void setPrevious(NodeStack<?> previous) {
165+
this.previous = previous;
166+
}
167+
168+
private static int getSize() {
169+
return size;
170+
}
171+
172+
private static void setSize(int size) {
173+
NodeStack.size = size;
174+
}
175+
176+
private Item getData() {
177+
return this.data;
178+
}
179+
180+
private void setData(Item item) {
181+
this.data = item;
182+
}
183+
}

0 commit comments

Comments
 (0)