Skip to content

Commit 954fb3f

Browse files
SpreehaRazdeep
authored andcommitted
day 38 (#245)
1 parent fbed801 commit 954fb3f

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

day38/Java/Stack.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package javaapplication3;
7+
8+
/**
9+
* @date 11/02/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class Stack {
14+
static int st[]=new int[5];
15+
static int top=0;
16+
public static void push(int n)
17+
{
18+
if(top<5)
19+
{
20+
st[top]=n;
21+
top++;
22+
}
23+
else
24+
isFull();
25+
}
26+
public static void pop()
27+
{
28+
if(top<=5 && top>0)
29+
{
30+
top--;
31+
System.out.println("Popped element is "+st[top]);
32+
}
33+
else
34+
isEmpty();
35+
}
36+
public static void isEmpty()
37+
{
38+
if(top<=0)
39+
System.out.println("Stack is empty");
40+
}
41+
public static void isFull()
42+
{
43+
if(top>=5)
44+
System.out.println("Stack is full");
45+
}
46+
public static void peek()
47+
{
48+
if(top-1>=0 && top-1<5)
49+
System.out.println("The topmost element of the stack is "+st[top-1]);
50+
else
51+
System.out.println("Stack is empty");
52+
}
53+
public static void display()
54+
{
55+
System.out.println("All the elements present in the stack are:");
56+
for(int i=0;i<top;i++)
57+
System.out.print(st[i]+" ");
58+
System.out.println();
59+
}
60+
public static void main(String []args)
61+
{
62+
Scanner sc=new Scanner(System.in);
63+
int ch;int c=0;
64+
do
65+
{
66+
c=0;
67+
System.out.println("Enter choice ");
68+
System.out.println("1. Add new element"
69+
+ "\n"+"2. Remove topmost eleemnt"
70+
+"\n3. To view the topmost element"+"\n4. To view all the elements");
71+
ch=sc.nextInt();
72+
switch(ch)
73+
{
74+
case 1:
75+
System.out.println("Enter element to be added to stack ");
76+
int n=sc.nextInt();
77+
push(n);
78+
break;
79+
case 2:
80+
pop();
81+
break;
82+
case 3:
83+
peek();
84+
break;
85+
case 4:
86+
display();
87+
break;
88+
default:
89+
System.out.println("Invalid choice :");
90+
}
91+
System.out.println("Enter 1 if you wish to continue");
92+
c=sc.nextInt();
93+
}
94+
while(c==1);
95+
}
96+
}

day38/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,100 @@ stk.push (1);
182182
stk.push (2);
183183
stk.pop ();
184184
stk.peek ();
185+
```
186+
187+
## Java Implementation
188+
189+
### [Solution](./Java/Stack.java)
190+
191+
```java
192+
/**
193+
* @date 11/02/19
194+
* @author SPREEHA DUTTA
195+
*/
196+
import java.util.*;
197+
public class Stack {
198+
static int st[]=new int[5];
199+
static int top=0;
200+
public static void push(int n)
201+
{
202+
if(top<5)
203+
{
204+
st[top]=n;
205+
top++;
206+
}
207+
else
208+
isFull();
209+
}
210+
public static void pop()
211+
{
212+
if(top<=5 && top>0)
213+
{
214+
top--;
215+
System.out.println("Popped element is "+st[top]);
216+
}
217+
else
218+
isEmpty();
219+
}
220+
public static void isEmpty()
221+
{
222+
if(top<=0)
223+
System.out.println("Stack is empty");
224+
}
225+
public static void isFull()
226+
{
227+
if(top>=5)
228+
System.out.println("Stack is full");
229+
}
230+
public static void peek()
231+
{
232+
if(top-1>=0 && top-1<5)
233+
System.out.println("The topmost element of the stack is "+st[top-1]);
234+
else
235+
System.out.println("Stack is empty");
236+
}
237+
public static void display()
238+
{
239+
System.out.println("All the elements present in the stack are:");
240+
for(int i=0;i<top;i++)
241+
System.out.print(st[i]+" ");
242+
System.out.println();
243+
}
244+
public static void main(String []args)
245+
{
246+
Scanner sc=new Scanner(System.in);
247+
int ch;int c=0;
248+
do
249+
{
250+
c=0;
251+
System.out.println("Enter choice ");
252+
System.out.println("1. Add new element"
253+
+ "\n"+"2. Remove topmost eleemnt"
254+
+"\n3. To view the topmost element"+"\n4. To view all the elements");
255+
ch=sc.nextInt();
256+
switch(ch)
257+
{
258+
case 1:
259+
System.out.println("Enter element to be added to stack ");
260+
int n=sc.nextInt();
261+
push(n);
262+
break;
263+
case 2:
264+
pop();
265+
break;
266+
case 3:
267+
peek();
268+
break;
269+
case 4:
270+
display();
271+
break;
272+
default:
273+
System.out.println("Invalid choice :");
274+
}
275+
System.out.println("Enter 1 if you wish to continue");
276+
c=sc.nextInt();
277+
}
278+
while(c==1);
279+
}
280+
}
185281
```

0 commit comments

Comments
 (0)