-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSc2720_Stack.java
More file actions
86 lines (63 loc) · 1.47 KB
/
CSc2720_Stack.java
File metadata and controls
86 lines (63 loc) · 1.47 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
/*
* Program Name: Lab 7 / Stack
* Name: Stephanie L. Wyckoff
* Date: 2/24/2017
* Course: Data Structures Lab
* Professor: Duan
* TA: Pelin Icer, Shubin Wu
*
* Description: Constructor class used to create new Stack objects with char array. Program contains methods for pop, push, peek, isEmpty and printStack.
*
* Input: character array.
*
* Output: Statement of each step.
*/
public class Stack {
private int top;
private int size;
private char[] stack;
public Stack(int arraySize) {
size = arraySize;
stack = new char[size];
top = -1;
}
public boolean pop() {
System.out.println("Pop opperation done!\n" + (top) + " is popped");
if(isEmpty() == false) {
top = top-1;
}
else {
System.out.println("Stack is empty. Can't pop.");
}
return false ;
}
public boolean push(char a) {
System.out.println("Element " + a + " is pushed to Stack!");
if(top == (size-1)) {
System.out.println("Stack is full");
}
else {
top = top+1;
stack[top] = a;
}
return false;
}
public char peek() {
System.out.println("Peek is: " + stack[top]);
return stack[top];
}
public boolean isEmpty() {
System.out.print("Is the stack empty? ");
if(top == -1) {
return true;
}
return false;
}
public void printStack() {
System.out.println("Elements in the stack are (top -> bottom):");
for(int i = 0; i <= top; i++) {
System.out.print(stack[i] + " ");
}
System.out.println("\n");
}
}