1
+
2
+ //Importing Scanner class for input
3
+ import java .util .Scanner ;
4
+ import java .util .InputMismatchException ;
5
+
6
+ // Stack class
7
+ class Stack {
8
+ private int top ;
9
+ private int maxSize ;
10
+ private int stack [];
11
+
12
+ // Constructor for Stack class intialize stack with given maximum size
13
+ public Stack (int maxSize ) {
14
+ this .maxSize = maxSize ;
15
+ this .stack = new int [maxSize ];
16
+ this .top = -1 ; // stack is empty
17
+ }
18
+
19
+ // adding element to stack
20
+ public void push (int element ) {
21
+ if (top == maxSize - 1 ) { // to check stack is full
22
+ System .out .println ("-1" );
23
+ return ;
24
+ }
25
+ stack [++top ] = element ; // increasing the top and adding element
26
+ }
27
+
28
+ // removing element from stack
29
+ public void pop () {
30
+ if (top == -1 ) { // to check stack is empty
31
+ System .out .println ("-1" );
32
+ return ;
33
+ }
34
+ top --; // decrement top and get element at top of stack
35
+ }
36
+
37
+ // to get top of stack
38
+ public int peek () {
39
+ if (top == -1 ) { // to check stack is empty
40
+ return -1 ;
41
+ }
42
+ return stack [top ]; // get top element of stack
43
+ }
44
+
45
+ // method to check empty stack
46
+ public boolean isEmpty () {
47
+ return top == -1 ;
48
+ }
49
+
50
+ // method to check stack is Full
51
+ public boolean isFull () {
52
+ return top == maxSize - 1 ;
53
+ }
54
+
55
+ // to display the stack
56
+ public void display () {
57
+ if (top == -1 ) { // to check stack is empty
58
+ return ;
59
+ }
60
+ for (int index = top ; index >= 0 ; index --) {
61
+ System .out .print (stack [index ] + " " );
62
+ }
63
+ }
64
+
65
+ public static void main (String [] args ) {
66
+ try (Scanner input = new Scanner (System .in )) {
67
+ System .out .println ("Enter size of stack you want: " );
68
+ int maxSize = input .nextInt (); // user input for max size
69
+ Stack stack = new Stack (maxSize );
70
+ int choice ;
71
+ do {
72
+ System .out .println ("1. Push" );
73
+ System .out .println ("2. Pop" );
74
+ System .out .println ("3. Peek" );
75
+ System .out .println ("4. Is empty" );
76
+ System .out .println ("5. Display" );
77
+ System .out .println ("6. Exit" );
78
+ System .out .println ("Enter your choice: " );
79
+ choice = input .nextInt (); // getting user's choice
80
+ switch (choice ) {
81
+ case 1 :
82
+ System .out .println ("Enter element to push: " );
83
+ int element = input .nextInt ();
84
+ if (stack .isFull ()) {
85
+ System .out .println ("stack is full" );
86
+ break ;
87
+ }
88
+ stack .push (element );
89
+ break ;
90
+ case 2 :
91
+ if (stack .isEmpty ()) {
92
+ System .out .println ("Stack is empty" );
93
+ break ;
94
+ }
95
+ int top = stack .peek ();
96
+ stack .pop ();
97
+ System .out .println ("Deleted element is " + top );
98
+ break ;
99
+ case 3 :
100
+ if (stack .isEmpty ()) {
101
+ System .out .println ("Stack is empty" );
102
+ break ;
103
+ }
104
+ System .out .println ("Top element is " + stack .peek ());
105
+ break ;
106
+ case 4 :
107
+ if (stack .isEmpty ()) {
108
+ System .out .println ("Stack is empty" );
109
+ } else {
110
+ System .out .println ("Stack is not empty" );
111
+ }
112
+ break ;
113
+ case 5 :
114
+ if (stack .isEmpty ()) {
115
+ System .out .println ("stack is empty" );
116
+ break ;
117
+ }
118
+ stack .display ();
119
+ System .out .println ();
120
+ break ;
121
+ case 6 :
122
+ System .out .println ("Exit done" );
123
+ break ;
124
+ default :
125
+ System .out .println ("Invalid choice" );
126
+ }
127
+ } while (choice != 6 );
128
+ } catch (InputMismatchException exception ) {
129
+ System .out .println (exception );
130
+ } catch (Exception exception ) {
131
+ System .out .println (exception );
132
+ }
133
+ }
134
+ }
0 commit comments