Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
堆栈实现java_Java_Stack - Fatal编程技术网

堆栈实现java

堆栈实现java,java,stack,Java,Stack,无法通过此代码实现堆栈 UseStack.java class UseStack{ public static void main(String[] args) { Scanner obj = new Scanner(System.in); System.out.println("Enter the size of Stack...."); int

无法通过此代码实现堆栈

UseStack.java

     class UseStack{
            public static void main(String[] args) {
                Scanner obj = new Scanner(System.in);
                System.out.println("Enter the size of Stack....");
                int n = obj.nextInt();
                Push push = new Push(n);
                Pop pop = new Pop(n);
                while(true){
                    System.out.println("1: Push");
                    System.out.println("2: pop");
                    System.out.println("3: Show");
                    int choice = obj.nextInt();;
                    switch(choice){
                        case 1:
                        push.push();
                        break;
                        case 2:
                        pop.pop();
                        break;
                        case 3:
                        push.show();    
                        break;
                        default:
                        System.out.println("Invalid Option");
                        break;
                    }
                }
            }
        }
class Stack {
    public int arr[];
    public  int top;
    public  int capacity;

    Stack(int size){
        this.arr = new int[size];
        this.capacity = size;
        this.top = -1;
    }
}
class Push extends Stack {
    Push(int size) {
        super(size);
    }

    private static Scanner obj;
    public void push(){
        obj = new Scanner(System.in);
        System.out.println("Enter Value to push...");
        int value = obj.nextInt();
        System.out.println("Value : "+value);
        if(top==capacity-1){
            System.out.println("StackOverflow");
            return;
        }
        else{
            top++;
            System.out.println("Top : "+top);
            arr[top]=value;
            System.out.println("Pushed... "+arr[top]);
        }
    }

    public void show(){
        if(top==-1){
            System.out.println("StackUnderFlow");
            return;
        }
        else{
            System.out.println("Stack Elements : ");
            for(int i=top;i>=0;i--){
                System.out.println(arr[i]+" ");
            }
        }       
    }
}
public class Pop extends Stack {

    Pop(int size) {
        super(size);
    }
    
    public void pop(){
        if(top==-1){
            System.out.println("StackUnderflow-pop");
            return;
        }
        else{
            System.out.println("Top : "+top);
            System.out.println("Poped.. "+arr[top]);
            top--;
        }
    }
    
}
Stack.java

     class UseStack{
            public static void main(String[] args) {
                Scanner obj = new Scanner(System.in);
                System.out.println("Enter the size of Stack....");
                int n = obj.nextInt();
                Push push = new Push(n);
                Pop pop = new Pop(n);
                while(true){
                    System.out.println("1: Push");
                    System.out.println("2: pop");
                    System.out.println("3: Show");
                    int choice = obj.nextInt();;
                    switch(choice){
                        case 1:
                        push.push();
                        break;
                        case 2:
                        pop.pop();
                        break;
                        case 3:
                        push.show();    
                        break;
                        default:
                        System.out.println("Invalid Option");
                        break;
                    }
                }
            }
        }
class Stack {
    public int arr[];
    public  int top;
    public  int capacity;

    Stack(int size){
        this.arr = new int[size];
        this.capacity = size;
        this.top = -1;
    }
}
class Push extends Stack {
    Push(int size) {
        super(size);
    }

    private static Scanner obj;
    public void push(){
        obj = new Scanner(System.in);
        System.out.println("Enter Value to push...");
        int value = obj.nextInt();
        System.out.println("Value : "+value);
        if(top==capacity-1){
            System.out.println("StackOverflow");
            return;
        }
        else{
            top++;
            System.out.println("Top : "+top);
            arr[top]=value;
            System.out.println("Pushed... "+arr[top]);
        }
    }

    public void show(){
        if(top==-1){
            System.out.println("StackUnderFlow");
            return;
        }
        else{
            System.out.println("Stack Elements : ");
            for(int i=top;i>=0;i--){
                System.out.println(arr[i]+" ");
            }
        }       
    }
}
public class Pop extends Stack {

    Pop(int size) {
        super(size);
    }
    
    public void pop(){
        if(top==-1){
            System.out.println("StackUnderflow-pop");
            return;
        }
        else{
            System.out.println("Top : "+top);
            System.out.println("Poped.. "+arr[top]);
            top--;
        }
    }
    
}
Push.java

     class UseStack{
            public static void main(String[] args) {
                Scanner obj = new Scanner(System.in);
                System.out.println("Enter the size of Stack....");
                int n = obj.nextInt();
                Push push = new Push(n);
                Pop pop = new Pop(n);
                while(true){
                    System.out.println("1: Push");
                    System.out.println("2: pop");
                    System.out.println("3: Show");
                    int choice = obj.nextInt();;
                    switch(choice){
                        case 1:
                        push.push();
                        break;
                        case 2:
                        pop.pop();
                        break;
                        case 3:
                        push.show();    
                        break;
                        default:
                        System.out.println("Invalid Option");
                        break;
                    }
                }
            }
        }
class Stack {
    public int arr[];
    public  int top;
    public  int capacity;

    Stack(int size){
        this.arr = new int[size];
        this.capacity = size;
        this.top = -1;
    }
}
class Push extends Stack {
    Push(int size) {
        super(size);
    }

    private static Scanner obj;
    public void push(){
        obj = new Scanner(System.in);
        System.out.println("Enter Value to push...");
        int value = obj.nextInt();
        System.out.println("Value : "+value);
        if(top==capacity-1){
            System.out.println("StackOverflow");
            return;
        }
        else{
            top++;
            System.out.println("Top : "+top);
            arr[top]=value;
            System.out.println("Pushed... "+arr[top]);
        }
    }

    public void show(){
        if(top==-1){
            System.out.println("StackUnderFlow");
            return;
        }
        else{
            System.out.println("Stack Elements : ");
            for(int i=top;i>=0;i--){
                System.out.println(arr[i]+" ");
            }
        }       
    }
}
public class Pop extends Stack {

    Pop(int size) {
        super(size);
    }
    
    public void pop(){
        if(top==-1){
            System.out.println("StackUnderflow-pop");
            return;
        }
        else{
            System.out.println("Top : "+top);
            System.out.println("Poped.. "+arr[top]);
            top--;
        }
    }
    
}
Pop.java

     class UseStack{
            public static void main(String[] args) {
                Scanner obj = new Scanner(System.in);
                System.out.println("Enter the size of Stack....");
                int n = obj.nextInt();
                Push push = new Push(n);
                Pop pop = new Pop(n);
                while(true){
                    System.out.println("1: Push");
                    System.out.println("2: pop");
                    System.out.println("3: Show");
                    int choice = obj.nextInt();;
                    switch(choice){
                        case 1:
                        push.push();
                        break;
                        case 2:
                        pop.pop();
                        break;
                        case 3:
                        push.show();    
                        break;
                        default:
                        System.out.println("Invalid Option");
                        break;
                    }
                }
            }
        }
class Stack {
    public int arr[];
    public  int top;
    public  int capacity;

    Stack(int size){
        this.arr = new int[size];
        this.capacity = size;
        this.top = -1;
    }
}
class Push extends Stack {
    Push(int size) {
        super(size);
    }

    private static Scanner obj;
    public void push(){
        obj = new Scanner(System.in);
        System.out.println("Enter Value to push...");
        int value = obj.nextInt();
        System.out.println("Value : "+value);
        if(top==capacity-1){
            System.out.println("StackOverflow");
            return;
        }
        else{
            top++;
            System.out.println("Top : "+top);
            arr[top]=value;
            System.out.println("Pushed... "+arr[top]);
        }
    }

    public void show(){
        if(top==-1){
            System.out.println("StackUnderFlow");
            return;
        }
        else{
            System.out.println("Stack Elements : ");
            for(int i=top;i>=0;i--){
                System.out.println(arr[i]+" ");
            }
        }       
    }
}
public class Pop extends Stack {

    Pop(int size) {
        super(size);
    }
    
    public void pop(){
        if(top==-1){
            System.out.println("StackUnderflow-pop");
            return;
        }
        else{
            System.out.println("Top : "+top);
            System.out.println("Poped.. "+arr[top]);
            top--;
        }
    }
    
}
问题

在此实现中,pop()不起作用

我认为对于这个Pop类,需要同时扩展Stack和Push类,因为这在java中是不可能的,如果我错了,有人能帮我解决这个问题吗…

Pop()
不起作用,因为推送和Pop使用不同的对象

您不需要为push和pop定义另一个类,它们是在堆栈类中添加这些函数的操作

class Stack {
   ... // members and constructor
   public void push(){..}
   public void pop(){..}
   public void show(){..}
}
并创建一个Stack类的对象,用于push、pop和show

Stack s = new Stack(n);
while(true){
    ... 
    switch(choice){
        case 1:
        s.push();
        break;
        case 2:
        s.pop();
        break;
        case 3:
        s.show();    
        break;
        default:
        System.out.println("Invalid Option");
        break;
    }
}

为什么push和pop操作会扩展
堆栈
类?你不会写
狗延伸动物
吠叫延伸狗
,是吗?@Kayaman先生你能再纠正我一下吗?你不需要叫我先生。我只是问你为什么有
Push
Pop
类?为什么
Stack
类中不存在
push()
pop()
方法?您需要在问题中包含任何错误。但是代码在设计上是错误的,所以您应该首先修复您的设计(删除
Push
Pop
类),然后尝试让代码正常工作。问题是Push和popping在同一数据结构上不起作用。当用户发出“push”命令时,您在
push
堆栈上调用
push()
,从而将数据添加到
push
堆栈中;与此同时,“pop”堆栈仍然是空的。现在,当用户发出“pop”命令时,您调用
pop
堆栈的
pop()
方法,空的
pop
堆栈中没有任何东西可以弹出。@ItazakhStern知道什么?因为你完全错误的设计,你实际上使用了多个对象,但是你认为你可以把它们当作一个堆栈来对待?我建议你开始读这本书,因为你知道的并不像你想象的那么多。@ItazakhStern:“我知道这个”?如果你知道的话,为什么要问这个问题呢?我的意思是像push和pop这样的操作可以在stack类中定义