【问题标题】:A method that insert an element at a certain position in a stack. (java)一种在堆栈中的特定位置插入元素的方法。 (爪哇)
【发布时间】:2021-08-01 05:15:20
【问题描述】:

我正在尝试在堆栈中编写代码,并在其中实现各种方法。我正在尝试一种名为insertAtPos(int value, int pos) 的方法,我在pos 的位置插入value。这甚至可能吗?

结果我得到了初始堆栈。我不明白错误在哪里。

这是方法(我的方法):

public void insertAtPos(int pos, int value) {  //////////?????
        Stack s1 = new Stack();
        int c = 0;
        
        while(!this.isEmpty() && c != pos) {
            s1.push(this.stackTop());
            this.pop();
            c++;
            
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
            
        }
        
    }

这是整个班级:



public class Stack {
    class Element{
        int data;
        Element next = null;
        
        Element(int value){
            data = value;
            next = null;
        }
    }
    
    private Element head = null;
    
    public Stack() {
        head = null;
    }
    
    
    public static void display(Stack s) {
        Stack s1 = new Stack();
        
        if(s.isEmpty()) {
            System.out.println("the stack is empty");
            return;
        }
        
        while(!s.isEmpty()) {
            
            s1.push(s.stackTop());
            s.pop();
            System.out.println(s1.stackTop());
        }
        
        while(!s1.isEmpty()) {
            s.push(s1.stackTop());
            s1.pop();
        }
        
    }


    public boolean isEmpty() {
        return this.head == null;
    }
    
    public void push(int value) {
        Element tmp = new Element(value);
        tmp.next = head;
        head = tmp;
        
    }
    
    public int pop() {
        if(isEmpty())
            System.exit(1);
        head = head.next;
    return 1;
    }
    
    public int stackTop() {
        if(isEmpty())
            System.exit(2);
        return head.data;
    }
    
    public void makeEmpty() {
        while(!this.isEmpty()) {
            this.pop();
        }
    }
    
    public int stackTopAndPop(){
        this.pop();
        return this.stackTop();
    }
    
    public int search(int value) { 
        Stack s1 = new Stack();
        int index = -1;
        int c = 0; //counter
        
        if(this.isEmpty()) {
            System.out.println("the stack is empty");
            return -1;
        }
        
        while(!this.isEmpty() && index == -1) {
            if(this.stackTop() == value)
                index = c;
            s1.push(this.stackTop());
            this.pop();
            c++;
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
        }
        return index;
    }
    
    
    public int count() {
        Stack s1 = new Stack();
        int c = 0; // counter
        
        while(!this.isEmpty()) {
            s1.push(this.stackTop());
            this.pop();
            c++;
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
        }
        
        return c;
    }
    
    
    public void insertNoRepetition(int value) {  //////////?????
        
    }
    
    
    public void insertAtPos(int pos, int value) {  //////////?????
        Stack s1 = new Stack();
        int c = 0;
        
        while(!this.isEmpty() && c != pos) {
            s1.push(this.stackTop());
            this.pop();
            c++;
            
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
            
        }
        
    }
    
    
    public void delete(int value) {
        Stack s1 = new Stack();
        
        while(!this.isEmpty()) {
            if(this.stackTop() != value)
                s1.push(this.stackTop());
            this.pop();
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
        }
        
    }
    
    
    
    public void deleteFirstOccurence(int value) {   //////////?????
        Stack s1 = new Stack();
        
        while(!this.isEmpty()) {
            if(this.stackTop() != value) {
                s1.push(this.stackTop());
                return;
            }
            this.pop();
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
        }
        
    }
    
    
    public void deleteEven() {
        Stack s1 = new Stack();
        
        while(!this.isEmpty()) {
            if(this.stackTop() % 2 != 0)
                s1.push(this.stackTop());
            this.pop();
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
        }
        
    }
    
    
    public void deleteOdd() {
        Stack s1 = new Stack();
        
        while(!this.isEmpty()) {
            if(this.stackTop() % 2 == 0)
                s1.push(this.stackTop());
            this.pop();
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
        }
        
    }
    
    
    public void deletePositive() {
        Stack s1 = new Stack();
        
        while(!this.isEmpty()) {
            if(this.stackTop() < 0)
                s1.push(this.stackTop());
            this.pop();
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
        }
        
    }
    
    
    public void deleteLessOrEqualThan(int value) {
        Stack s1 = new Stack();
        
        while(!this.isEmpty()) {
            if(this.stackTop() > value)
                s1.push(this.stackTop());
            this.pop();
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
        }
        
    }
    
    
    //delete all elements = to the elements stored in another stack
    public void deleteEqualToOther(Stack s) {
        Stack s1 = new Stack();
        Stack s2 = new Stack();
        
        while(!this.isEmpty() && this.stackTop() == s.stackTop()) {
            
                s1.push(this.stackTop());
                
            this.pop();
            
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
            
        }
        
    }
    
    
    
    
//  public Stack union(Stack s1, Stack s2) {
//      
//  }
//  
//  
//  public Stack intersection(Stack s1, Stack s2) {
//      
//  }
    
    
    public Stack ascendingOrder() {
        Stack s1 = new Stack();
        
        while(!this.isEmpty()) {
            int tmp = this.pop();
            
            while(!s1.isEmpty() && s1.stackTop() > tmp) {
                this.push(s1.stackTop());
                s1.pop();
            }
            
            s1.push(tmp);
        }
        return s1;
        
    }
    
    
    
    
    public static void main(String[] args) {
        Stack s1 = new Stack();
//      Stack s2 = new Stack();
        
        s1.push(1);
        s1.push(3);
        s1.push(3);
        s1.push(4);
        s1.push(-5);
        display(s1);
        System.out.println("______________________");
        
//      s2.push(1);
//      s2.push(3);
//      s2.push(4);
//      s2.push(6);
//      s2.push(7);
//      display(s2);
//      System.out.println("______________________");

        
//      System.out.println(s1.search(3));
//      System.out.println("______________________");
//
//      System.out.println(s1.count());
//      System.out.println("______________________");
//      
//      s1.delete(2);
//      display(s1);
//      System.out.println("______________________");

//      System.out.println(s1.stackTop());
//      System.out.println("______________________");
        
//      s1.makeEmpty();
//      display(s1);
//      System.out.println("______________________");
        
//      s1.deleteLessOrEqualThan(2);
//      display(s1);
//      System.out.println("______________________");
        
//      s1.deleteEqualToOther(s2);
//      display(s1);
//      System.out.println("______________________");

        
//      s1.ascendingOrder();
//      display(s1);
//      System.out.println("______________________");

        
//      s1.deleteFirstOccurence(3);
//      display(s1);
//      System.out.println("______________________");
        
        s1.insertAtPos(2, 2);
        display(s1);

        
    }

}

【问题讨论】:

    标签: java list stack


    【解决方案1】:

    因此,您将获得初始堆栈,因为您从未在方法“insertAtPos”中推送“值”。

    是的,在您的堆栈实现中是可能的。但是尝试将元素插入堆栈的内部位置对您来说没有任何意义。这打破了 LIFO“后进先出”的限制。如果您想出于编码学习目的这样做,那没关系,但堆栈的实际实现不应包含该功能。

    【讨论】:

    • 我知道 LIFO 限制,但是否可以将所有值弹出到某个位置,然后将弹出的值推回。这是否仍然违反 LIFO 限制?
    • 如果您将该方法放在堆栈实现中(作为堆栈的功能),它将打破限制。您可以从使用您的堆栈的客户端执行此操作。如果您希望您的代码有效,您只需添加: this.push(value); insertAtPos 函数的两个开关之间。
    猜你喜欢
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 2021-08-05
    • 2013-11-07
    • 2012-02-13
    • 2021-08-06
    相关资源
    最近更新 更多