-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyStack.java
147 lines (129 loc) · 3.49 KB
/
MyStack.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
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
package com.company;
import java.util.*;
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.
* @return 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{
public class Node{
Object data;
Node next;
}
Node head = null;
public void add(Object element){
Node current=new Node();
current.data=element;
current.next=null;
Node temp = head;
if(head==null)
head=current;
else {
while(temp.next!=null){
temp=temp.next;
}
temp.next=current;
}
}
public void display(){
Node current = head;
System.out.print("[");
for(int i = 0; current!=null; ++i) {
System.out.print(current.data);
if(current.next!=null )
System.out.print(", ");
current = current.next;
}
System.out.print("]");
}
public void push(Object element){
Node current=new Node();
current.data=element;
current.next=head;
head=current;
}
public Object pop(){
if(head==null){
System.out.println("Error");
System.exit(0);
}
Object obj=head.data;
head=head.next;
return obj;
}
public Object peek(){
if(head==null){
System.out.println("Error");
System.exit(0);
}
return head.data;
}
public boolean isEmpty(){
if(head==null)
return true;
return false;
}
public int size(){
Node current=new Node();
int size=0;
if(head==null)
return size;
current=head;
while (current!=null){
size++;
current=current.next;
}
return size;
}
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
MyStack stack =new MyStack();
String str=in.nextLine().replaceAll("\\[|\\]", "");
String []s=str.split(", ");
Object[] arr = new Object[s.length];
if (s.length == 1 && s[0].isEmpty())
arr = new Object[]{};
else {
for(int i = 0; i < s.length; ++i){
arr[i] = s[i];
stack.add(arr[i]);
}
}
String op=in.next();
if(op.equals("push")){
Object obj=in.next();
stack.push(obj);
stack.display();
}
else if(op.equals("pop")){
stack.pop();
stack.display();
}
else if(op.equals("peek")){
System.out.println(stack.peek());
}
else if(op.equals("isEmpty")){
if(stack.isEmpty()) System.out.println("True");
else System.out.println("False");
}
else if (op.equals("size")){
System.out.println(stack.size());
}
else
System.out.println("Error");
}
}