-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSC.java
More file actions
93 lines (83 loc) · 1.87 KB
/
SC.java
File metadata and controls
93 lines (83 loc) · 1.87 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
/**
* This class demonstrates the methods of the SC class.
* @author Ioannis Tzeneralis
* @version 1.0
*/
class SC {
private char q[]; // this array holds the queue
private int top, capacity; // the put and get indices
/**
* Default constructor.
* @param size The capacity
*/
SC(int size) {
q = new char[size];
capacity = size;
top = -1;
}
/**
* This method adds x to the q[], if q[] not full.
* @param x The char q[]
*/
public void push(char x)
{
if (full())
{
System.out.println("Overflow\nProgram Terminated\n");
System.exit(1);
}
q[++top] = x;
}
/**
* This method removes and returns the last data from q[], if q[] not empty.
* @return q[top--];
*/
public char pop()
{
// check for stack underflow
if (empty())
{
System.out.println("Underflow\nProgram Terminated");
System.exit(1);
}
// decrease stack size by 1 and (optionally) return the popped element
return q[top--];
}
/**
* This method returns the last data from q[], if q[] not empty.
* @return 1;
* @return q[top];
*/
public char peek()
{
if (!empty()) {
return q[top];
}
else {
System.exit(1);
}
return 1;
}
/**
* This method returns the size of q[].
* @return top + 1;
*/
public int size() {
return top + 1;
}
/**
* This method returns if q[] is empty.
* @return top == -1;
*/
public Boolean empty()
{
return top == -1; // or return size() == 0;
}
/**
* This method returns if q[] is full.
* @return top == capacity - 1;
*/
public Boolean full() {
return top == capacity - 1; // or return size() == capacity;
}
}