-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStack.java
More file actions
156 lines (135 loc) · 3.91 KB
/
Copy pathMyStack.java
File metadata and controls
156 lines (135 loc) · 3.91 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
interface IStack {
/*** Removes the element at the top of stack and returnsthat element.
* @return top of stack element, or through exception if empty
*/
public Object pop();
/*** Get the element at the top of stack without removing it from stack.
* @return top of stack element, or through exception if empty
*/
public Object peek();
/*** Pushes an item onto the top of this stack.
* @param object to insert*
*/
public void push(Object element);
/*** Tests if this stack is empty
* @return true if stack empty
*/
public boolean isEmpty();
public int size();
}
public class MyStack implements IStack {
class Node {
Object data;
Node next;
}
Node top;
public Object pop() {
if(top == null)
throw new RuntimeException("Error");
else {
Node x = top;
top = x.next;
return x.data;
}
}
public Object peek() {
if(top==null)
throw new RuntimeException("Error");
else
return top.data;
}
public void push(Object element) {
Node node=new Node();
node.data=element;
if (top==null) {
top=node;
}
else {
node.next=top;
top=node;
}
}
public boolean isEmpty() {
if(top==null)
return true;
else
return false;
}
public int size() {
int count=0;
Node x=top;
while(x!=null) {
count++;
x=x.next;
}
return count;
}
public void display() {
Node x=top;
if(top==null) {
System.out.printf("[]");
}
else {
System.out.printf("[");
while(x.next!= null) {
System.out.printf("%d, ",x.data);
x=x.next;
}
System.out.printf("%d",x.data);
System.out.printf("]");
}
}
//@SuppressWarnings("unchecked")
public static void main(String[] args) {
MyStack stack=new MyStack();
try {
Scanner input=new Scanner(System.in);
String s = input.nextLine().replaceAll("\\[|\\]", "");
String[] sin=s.split(", ");
if(sin.length==1&&sin[0].isEmpty()) {
}
else {
for(int i=sin.length-1;i>=0;i--) {
stack.push(Integer.parseInt(sin[i]));
}
}
String instruction=input.nextLine();
if(instruction.equals("push")) {
int x = input.nextInt();
stack.push(x);
stack.display();
}
else if(instruction.equals("peek")) {
int x = (int) stack.peek();
System.out.print(x );
}
else if(instruction.equals("pop")) {
stack.pop();
stack.display();
}
else if(instruction.equals("size")) {
int x = stack.size();
System.out.print(x);
}
else if(instruction.equals("isEmpty")) {
boolean x = stack.isEmpty();
if(x)
System.out.print("True");
else
System.out.print("False");
}
else {
System.out.print("Error");
}
}
catch(Exception e) {
System.out.print("Error");
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
}
}