File size: 1,062 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 |
class stackLL{
static Node top;
static class Node{
int data;
Node next;
}
stackLL(){
this.top = null;
}
void push(int x){
Node temp = new Node();
if(temp==null){
System.out.println("Overflow");
return;
}
temp.data = x;
temp.next = top;
top = temp;
System.out.println(x+" is pushed");
}
int pop(){
Node temp = top;
int x = -1;
if(temp==null){
return -1;
}
else{
x = temp.data;
top = top.next;
}
return x;
}
void print(){
Node temp = top;
if(top==null){
return;
}
while(temp!=null){
System.out.print(temp.data+" ");
temp = temp.next;
}
}
int peek(){
if(top==null){
return -1;
}
else{
return top.data;
}
}
public static void main(String[] args)
{
stackLL st = new stackLL();
st.push(5);
st.push(4);
st.push(3);
st.push(2);
st.push(1);
st.pop();
st.print();
System.out.println("\n"+st.peek());
}
}
|