-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackArr.java
137 lines (125 loc) · 2.53 KB
/
StackArr.java
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
package stackArrayImplimentation;
import java.util.InputMismatchException;
public class StackArr
{
java.util.Scanner in = new java.util.Scanner(System.in);
int size ,top = -1;
int[] arr;
public void createStk()
{
System.out.print("Enter the size of the Stack : ");
size = getAnInteger();
arr= new int[size];
System.out.println("Stack Created.");
}
public boolean isEmpty()
{
if(top<0)
return true;
else
return false;
}
public boolean isFull()
{
if(top>=size-1)
return true;
else
return false;
}
public void pushAnItem()
{
if(isFull())
{
System.out.println("The stack is full .");
return;
}
else
{
top = top +1;
System.out.print("Enter the value you want to enter .");
arr[top]= getAnInteger();
}
}
public void pop()
{
if(isEmpty())
{
System.out.println("The stack is empty .");
return;
}
else
{
System.out.println("The popped item is "+arr[top]);
top=top-1;
}
}
public void traverse()
{
if(top<0)
{
System.out.println("empty stack .");
return;
}
else
{
System.out.println("\nThe elements in the stack are : \n");
for(int i=top ; i >= 0;i--)
System.out.print(" | "+arr[i]+" | ");
System.out.println("\n");
}
}
public static void main(String[] args)
{
StackArr obj = new StackArr();
System.out.println("Array implimentation of Stack .");
int choice=0;
do
{
System.out.println("\n\t||MENU||\n\n");
System.out.println("1.Creation .");
System.out.println("2.Traverse current info.");
System.out.println("3.Pop an elment.");
System.out.println("4.Push an element .");
System.out.println("5.Exit .");
System.out.print("Enter your choice : ");
choice = obj.getAnInteger();
switch(choice)
{
case 1:
obj.createStk();
break;
case 2:
obj.traverse();
break;
case 3:
obj.pop();
break;
case 4:
obj.pushAnItem();
break;
case 5:
System.out.println("You have sucessfully exit the program.");
System.exit(0);
default:
System.out.println("The information is not relevent .Enter again.");
}
}
while(true);
}
public int getAnInteger()
{
while(true)
{
try
{
return in.nextInt();
}
catch(InputMismatchException e)
{
in.next();
System.out.println();
System.out.println("Input type is not integer !!! give an integer .");
}
}
}
}