Home > Java > javaTutorial > Delete middle element of a stack in Java

Delete middle element of a stack in Java

DDD
Release: 2025-02-07 11:14:11
Original
334 people have browsed it

Delete middle element of a stack in Java

To delete the intermediate element of the stack, you first need to remove the element above the intermediate element. After removing the intermediate elements, the elements above them need to be restored to maintain the original order. This operation can be implemented using a recursive method, as described below.

Due to the characteristics of the stack, we can only perform stack pressing and stacking operations on the top of the stack, so we cannot directly delete the intermediate elements of the stack.

Step to delete the middle element of the stack

Step 1: Determine the position of the intermediate element First, you need to determine the intermediate element of the stack, the method is as follows:

  • If the stack contains 9 elements (indexes 0 to 8), the intermediate element is at position 4.
  • If the stack contains even numbers of elements (for example, 4 elements), delete the first of the two intermediate elements.
  • If the stack contains only one element, the element will be treated as an intermediate element and deleted.

Step 2: Recursively remove the element at the top of the stack until the intermediate element is reached.

Step 3: Once the intermediate element is reached, just pop it out of the stack and reassemble the stack to its original order.

Program

The following example deletes the intermediate element of the stack:

import java.util.Stack;

public class Example {
    public static void deleteMidElement(Stack<Integer> stack, int currentIndex) {
        if (currentIndex == 0) {
            stack.pop();
            return;
        }

        int temp = stack.pop();
        deleteMidElement(stack, currentIndex - 1);
        stack.push(temp);
    }

    public static void main(String args[]) {
        Stack<Integer> stack = new Stack<>();
        stack.push(9);
        stack.push(10);
        stack.push(0);
        stack.push(5);
        stack.push(7);
        int N = stack.size() / 2;

        System.out.println("删除中间元素之前的堆栈: " + stack);
        deleteMidElement(stack, N);
        System.out.println("删除中间元素之后的堆栈: " + stack);
    }
}
Copy after login

The output of the above program is as follows:

<code>删除中间元素之前的堆栈: [9, 10, 0, 5, 7]
删除中间元素之后的堆栈: [9, 10, 5, 7]</code>
Copy after login

Explanation

In the main method, we call the deleteMidElement() method, the parameters are the index of the stack and the intermediate elements.

In the deleteMidElement() method, we check whether currentIndex is equal to 0. If true, the top of the stack is an intermediate element; otherwise, store the top of the stack in a variable, call deleteMidElement() recursively, decrement currentIndex by one each time, and then push the stored element back on the stack.

The above is the detailed content of Delete middle element of a stack in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template