Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fin bug in stack #1395

Open
AadityaThantharate opened this issue Nov 21, 2024 · 0 comments
Open

fin bug in stack #1395

AadityaThantharate opened this issue Nov 21, 2024 · 0 comments

Comments

@AadityaThantharate
Copy link

Image
import java.util.*;

public class Main {
// static class Node {
// int data;
// Node next;

//     public Node(int data) {
//         this.data = data;
//         next = null;
//     }

// }

// static class Stack {
//     public static Node head;

//     public static boolean isEmpty() {
//         return head == null;
//     }

//     public static void push(int data) {
//         Node newNode = new Node(data);
//         if (isEmpty()) {
//             head = newNode;
//             return;
//         }
//         newNode.next = head;
//         head = newNode;
//     }

//     public static int pop() {
//         if (isEmpty()) {
//             return-1;
//         }

//             int top = head.data;
//             head = head.next;
//             return top;
        
//     }

//     public static int peek() {
//         if (isEmpty()) {
//             return -1;
//         }
//         return head.data;
//     }
// }

// By Array List
static class Stack {
    static ArrayList<Integer> list = new ArrayList<>();
    public static boolean isEmpty(){
        return list.size() ==0;
    }
    //push
    public static void push(int data){
        list.add(data);
    }
    public static int pop(){
        if(isEmpty()){
            return -1;
        }
        int top = list.get(list.size()-1);
        list.remove(list.size()-1);
        return top;
    }
    public static int peek(){
        if(isEmpty()){
            return -1;
        }
        return  list.get(list.size()-1);
    }
}
public static void main(String[] args) {
    Stack s = new Stack();
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);

    while(!s.isEmpty()) {
        System.out.println(s.peek());
        s.pop();
    }
}

}

When i ever try to create stack it show me error continuosly you can see my code in i try to run the code in online copiler and its perfectly running properly see what happend and informed idk what kind of bug or error i am facing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant