本文介绍了堆栈实现java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法通过此代码实现堆栈...

Unable to implement stack through this code...

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;
                    }
                }
            }
        }

Stack.java

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;
    }
}

Push.java

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]+" ");
            }
        }
    }
}

Pop.java

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() 不起作用......

In this implementation pop() is not working.....

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

I think for this Pop class needs to extends both Stack and Push classes as so this is not possible in java ,If I'm wrong can anyone help me with this how to resolved it...

推荐答案

pop() 不起作用,因为您使用不同的对象进行推送和弹出.

pop() is not working because you are using different objects for push and pop.

push 和 pop 不需要另外定义类,它们是在 Stack 类中添加这些函数的操作.

You don't need to define another class for push and pop, they are operation add those function inside Stack class.

class Stack {
   ... // members and constructor
   public void push(){..}
   public void pop(){..}
   public void show(){..}
}

并创建一个Stack类的对象,用于push、pop和show

And create an object of Stack class and use for push, pop and 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;
    }
}

这篇关于堆栈实现java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:42