-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1b.txt
92 lines (79 loc) · 1.67 KB
/
1b.txt
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
Write a Java program to implement the Stack using arrays. Write Push(), Pop(), and Display() methods to demonstrate its working.
import java.util.*;
class arrayStack
{
int arr[];
int top, max;
arrayStack( int n)
{
max = n;
arr = new int[max];
top = -1;
}
void push( int i )
{
if (top == max - 1)
System.out.println("Stack Overflow");
else
arr[++top] = i;
}
void pop()
{
if (top == -1)
{
System.out.println("Stack Underflow");
}
else
{
int element = arr[top--];
System.out.println("Popped Element: " + element);
}
}
void display()
{
System.out.print("\nStack = ");
if (top == -1)
{
System.out.print("Empty\n");
return;
}
for (int i = top; i >= 0; i--)
System.out.print(arr[i] + " ");
System.out.println();
}
}
class Stack
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter Size of Integer Stack ");
int n = scan.nextInt();
boolean done = false;
arrayStack stk = new arrayStack(n);
char ch;
do
{
System.out.println("\nStack Operations");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. display");
System.out.println("4. Exit");
int choice = scan.nextInt();
switch (choice)
{
case 1: System.out.println("Enter integer element to push");
stk.push(scan.nextInt());
break;
case 2: stk.pop();
break;
case 3: stk.display();
break;
case 4: done = true;
break;
default: System.out.println("Wrong Entry \n ");
break;
}
} while (!done);
}
}