-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListStack.java
More file actions
82 lines (61 loc) · 1.57 KB
/
ArrayListStack.java
File metadata and controls
82 lines (61 loc) · 1.57 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 5
// Use the ArrayBasedList class as a stack to reverse an input String.
import java.util.*;
public class ArrayListStack {
Object arr[];
int n;
public ArrayListStack() {
arr = new Object[10];
n = 0;
}
public void add(int i, Object x){
if(n == arr.length){
resize();
}
for(int j = n; j > i ;j--) {
arr[j] = arr[j-1];
}
arr[i] = x;
n++;
}
public Object remove(int i){
Object x = arr[i];
for(int j = i; j < n -1;j++) {
arr[j] = arr[j+1];
}
n--;
if(n < arr.length){
resize();
}
return x;
}
public void resize(){
Object arr2[] = new Object[n*2];
for(int i = 0; i < n;i++){
arr2[i] = arr[i];
}
arr = arr2;
}
public void push (Object x)
{
add (n,x);
}
public Object pop()
{
return remove (n - 1);
}
public void separating (String inputString){
String[] word = inputString.split(" ");
for (int i = 0 ; i < word.length ; i ++){
push(word[i]);
}
for (int i = 0 ; i < word.length ; i ++){
System.out.print(pop() + " ");
}
}
public static void main(String[] args) {
String inputString = "My name is Celine";
ArrayListStack arrayList = new ArrayListStack();
arrayList.separating(inputString);
}
}