Datasets:

ArXiv:
License:
File size: 1,458 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
public class StackArray{
  int Max = 100,top = -1;
  int a[] = new int[Max];


void push(int x){
    // check if it is full or not 
    if(isFull()){
      System.out.println("Stack is full");
    }
    else{
      top++;
      a[top] = x;
      System.out.println(x+" is pushed");
    }
  }
// Delete Operation 

boolean isEmpty(){

  if(top==-1){
    return true;
  }
  else{
    return false;
  }
}

boolean isFull(){
  if(top==Max-1){
    return true;
  }
  else{
    return false;
  }
}
int stackTop(){
  if(isEmpty()){
    return -1;
  }
  else{
    return a[top];
  }
}


void pop(){
  int x;
  if(isEmpty()){
    System.out.println("Stack Underflow!!");
  }
  else{
   x = a[top];
   top--;
   System.out.println(x+" is popped");
  }
}

void display(){
  if(top==-1){
    System.out.println("Stack is Empty");
  }
  else{
    for(int i=top;i>=0;i--){
      System.out.print(a[i]+" ");
    }
  }
}
/*
static boolean isEmpty(){

}
*/

void peak(int pos){
  int t = -1;

  if(top-pos+1 <0){
    System.out.println("Underflow");
  }
  else{
    t = a[top-pos+1];
    System.out.println(t+" is the required element");
  }
  }
  public static void main(String[] args){

  StackArray st = new StackArray();
   st.push(3);
   st.push(4);
   st.push(5);
   st.push(6);
   st.push(7);
   st.pop();
   
   st.display();

   System.out.println("\nTop element in an array is ");
   System.out.println(st.stackTop());
   System.out.println();
   st.peak(2);
  }
}