forked from scottnakada/HackerRank-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
204 lines (165 loc) · 5.5 KB
/
Main.java
File metadata and controls
204 lines (165 loc) · 5.5 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package stackQueue;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Main {
/* Java Stack Class practice */
public static void stackPractice() {
/* Create an instance of a Stack object */
Stack stack = new Stack();
/* Print out a header */
System.out.println("****************************");
System.out.println("* Testing Java Stack Class *");
System.out.println("****************************");
/* See if the stack is empty */
if (stack.isEmpty()) {
System.out.println("The stack is empty");
}
/* Print the contents of the stack */
System.out.println(stack);
/* Push some data on the stack */
stack.push("One");
System.out.println(stack);
stack.push("Two");
System.out.println(stack);
stack.push("Three");
System.out.println(stack);
/* Peek at the top of the stack */
System.out.println("The top of the stack contains=" + stack.peek());
/* Search the stack for Two */
System.out.println("The index of the string Two is=" + stack.search("Two"));
/* Pop data off the stack */
String getData;
getData = (String) stack.pop();
System.out.println("Popped data=" + getData);
System.out.println(stack);
getData = (String) stack.pop();
System.out.println("Popped data=" + getData);
System.out.println(stack);
getData = (String) stack.pop();
System.out.println("Popped data=" + getData);
System.out.println(stack);
}
/* Practice with the Java Queue Class */
public static void queuePractice() {
/* Create an instance of the Queue class */
Queue queue = new LinkedList();
/* Print out a header */
System.out.println("****************************");
System.out.println("* Testing Java Queue Class *");
System.out.println("****************************");
/* Test to see if the queue is empty */
if (queue.isEmpty()) {
System.out.println("The queue is empty");
}
/* Print out the queue */
System.out.println(queue);
/* Add elements to the queue */
queue.add(2);
System.out.println(queue);
queue.add(4);
System.out.println(queue);
queue.add(6);
System.out.println(queue);
/* Peek at the head of the queue */
System.out.println("The head of the queue contains=" + queue.element());
System.out.println(queue);
/* Print the queue size */
System.out.println("The size of the queue is=" + queue.size());
/* Remove elements from the queue */
int getData;
getData = (int) queue.remove();
System.out.println("Removed " + getData + " from the queue.");
System.out.println(queue);
getData = (int) queue.remove();
System.out.println("Removed " + getData + " from the queue.");
System.out.println(queue);
getData = (int) queue.remove();
System.out.println("Removed " + getData + " from the queue.");
System.out.println(queue);
}
public static void myStackPractice() {
/* Create an instance of a StackInt object */
StackInt myStack = new StackInt();
/* Print out a header */
System.out.println("**************************");
System.out.println("* Testing my Stack Class *");
System.out.println("**************************");
/* See if the stack is empty */
if (myStack.isEmpty()) {
System.out.println("The stack is empty");
}
/* Print the contents of the stack */
myStack.print();
/* Push some data on the stack */
myStack.push(1);
myStack.print();
myStack.push(3);
myStack.print();
myStack.push(25);
myStack.print();
/* Peek at the top of the stack */
System.out.println("The top of the stack contains=" + myStack.peek());
///* Search the stack for Two */
System.out.println("The index of 3 is=" + myStack.search(3));
/* Pop data off the stack */
int getData;
getData = myStack.pop();
System.out.println("Popped data=" + getData);
myStack.print();
getData = myStack.pop();
System.out.println("Popped data=" + getData);
myStack.print();
getData = myStack.pop();
System.out.println("Popped data=" + getData);
myStack.print();
}
/* Practice with the my QueueInt Class */
public static void myQueuePractice() {
/* Create an instance of the Queue class */
QueueInt myQueue = new QueueInt();
/* Print out a header */
System.out.println("**************************");
System.out.println("* Testing my Queue Class *");
System.out.println("**************************");
/* Test to see if the queue is empty */
if (myQueue.isEmpty()) {
System.out.println("The queue is empty");
}
/* Print out the queue */
myQueue.print();
/* Add elements to the queue */
myQueue.add(2);
myQueue.print();
myQueue.add(4);
myQueue.print();
myQueue.add(6);
myQueue.print();
/* Peek at the head of the queue */
System.out.println("The head of the queue contains=" + myQueue.peek());
myQueue.print();
/* Print the queue size */
System.out.println("The size of the queue is=" + myQueue.size());
/* Remove elements from the queue */
int getData;
getData = myQueue.remove();
System.out.println("Removed " + getData + " from the queue.");
myQueue.print();
getData = myQueue.remove();
System.out.println("Removed " + getData + " from the queue.");
myQueue.print();
getData = myQueue.remove();
System.out.println("Removed " + getData + " from the queue.");
myQueue.print();
}
public static void main(String[] args) {
/* Practice with Java Stack Class */
stackPractice();
/* Practice with Java Queue Class */
queuePractice();
/* Practice with my StackInt Class */
myStackPractice();
/* Practice with my QueueInt Class */
myQueuePractice();
}
}