algorithms

algorithms Commit Details


Date:2013-02-20 23:29:00 (11 years 10 months ago)
Author:Natalie Adams
Branch:default
Commit:7efa32a4dc11
Parents: 948d1bf17931
Message:Adding java stack

Changes:
Ajava/stack/Main.java (full)
Ajava/stack/Stack.java (full)

File differences

java/stack/Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author nathan
*/
public class Main {
public static void main(String[] args)
{
Stack stk = new Stack();
stk.push(5);
stk.push(10);
stk.push(1);
System.out.println(stk.max());
}
}
java/stack/Stack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.ArrayList;
import java.util.List;
/**
*
* @author nathan
*/
public class Stack {
private List<Integer> _container;
public Stack() {
this._container = new ArrayList<>();
}
public int pop() {
int ret = _container.get(0);
_container.remove(0);
return ret;
}
public boolean push(int obj) {
try {
return _container.add(obj);
} catch (Exception e) {
throw e;
}
}
public int peek() {
return _container.get(0);
}
public boolean offer(int obj) {
try {
return _container.add(obj);
} catch (Exception e) {
return false;
}
}
public int max() {
int max = _container.get(0);
for(int i = 1; i < _container.size(); i++) {
if (_container.get(i) > max) {
max = _container.get(i);
}
}
return max;
}
}

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.41944s using 14 queries.