Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Stack.

java Tuesday, 5 March, 2024, 11:53 am

1 package stack1;
2
3 public class Stack
4{
5 private Integer[] items;
6 private int top;
7 public Stack(int size)
8 {
9 items=new Integer[size];
10 top=-1;
11 }
12
13 public boolean isEmpty()
14 {
15 return top <0;
16 }
17
18 public boolean isFull()
19 {
20 return top == items.length-1;
21 }
22
23 public boolean push(int data)
24 {
25 if(isFull())
26 {
27 return false;
28 }
29
30 items[++top] = data;
31 return true;
32
33 }
34
35 public Integer pop()
36 {
37 if(isEmpty())
38 {
39 return null;
40 }
41 return items[top--];
42 }
43
44 public Integer peek()
45 {
46 if(isEmpty())
47 {
48 return null;
49 }
50 return items[top];
51 }

Page 1
Stack.java Tuesday, 5 March, 2024, 11:53 am

52 }
53
54
55
56
57 public class teststack1main {
58
59 public static void main(String[] args)
60 {
61 Stack st=new Stack(5);
62 st.push(10);
63 st.push(20);
64 st.push(30);
65 st.push(40);
66
67 System.out.println(st.push(60));
68 System.out.println(st.push(70));
69 System.out.println(st.peek());
70
71 }
72
73 }
74

Page 2

You might also like