【问题标题】:printing the elements by removing middle element of stack通过删除堆栈的中间元素来打印元素
【发布时间】:2018-11-09 09:28:58
【问题描述】:

输入:输入的第一行包含一个整数 T,表示测试用例的数量。接下来是 T 个测试用例,每个测试用例的第一行包含一个整数 n。第二行由 n 个间隔整数组成。

输出:按相反顺序打印删除中间元素后的栈元素。

输入:1

7

1 2 3 4 5 6 7

输出是:

7 6 5 3 2 1

实际上我可以反向打印,但我不知道如何从堆栈中删除中间元素。请帮助

import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
    Scanner s=new Scanner(System.in);
    int test=s.nextInt();
    for(int t=0;t<test;t++)
    {
        int n=s.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            a[i]=s.nextInt();
        Stack<Integer> stack=new Stack<Integer>();
        for(int i=0;i<n;i++)
        {
             stack.push(a[i]);
        }
       ListIterator<Integer> lstIterator=stack.listIterator(stack.size());
       while(lstIterator.hasPrevious())
       {
           Integer res=lstIterator.previous();
           //what condition should i give so that it would print all the 
           elements except middle one.
           System.out.print(res+" ");   
       }
       System.out.println();
    }
  }
}

【问题讨论】:

  • 如果元素个数为偶数,中间元素如何处理?
  • 对于这种情况,我将检查 n 是偶数还是奇数,如果是偶数,我将根据测试用例删除 (n/2)-1 位置。例如-n= 6 和元素是 1 2 3 4 5 6 那么我的输出将是 6 5 4 2 1
  • 请告诉我删除奇数个元素的中间元素的逻辑

标签: java stack


【解决方案1】:

您可以使用pop() 方法弹出返回并删除堆栈的顶部元素,这样您就可以创建并以相反的顺序填充新堆栈,不需要反转迭代器,看看在下面的代码中。

import java.util.ListIterator;
import java.util.Scanner;
import java.util.Stack;

class GFG {

  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    //Define stacks here
    Stack<Integer> stack = new Stack<Integer>();
    Stack<Integer> new_stack = new Stack<Integer>();
    int test = s.nextInt();
    for (int t = 0; t < test; t++) {
      int n = s.nextInt();
      int a[] = new int[n];
      double middle = Math.ceil((double) n / 2);
      System.out.println("Middle is : " + middle);
      for (int i = 0; i < n; i++) {
        a[i] = s.nextInt();
      }

      // add elements to stack

      for (int i = 0; i < n; i++) {
        stack.push(a[i]);
      }

      //popping the elements of stack

      for (int j = 0; j < n; j++) {
        Integer element = stack.pop();
        if (j != middle -1) {
          new_stack.push(element);
        }
      }

      ListIterator<Integer> lstIterator = new_stack.listIterator(stack.size());
      while (lstIterator.hasNext()) {
        Integer res = lstIterator.next();
//what condition should i give so that it would print all the elements except middle one.
        System.out.print(res + " ");
      }
      System.out.println();
    }
  }
}

【讨论】:

    【解决方案2】:

    不要插入输入数组的中间元素。

    获取中间元素索引:

     int middle = a.length/2;
    

    不要压栈中的中间元素:

    Stack<Integer> stack=new Stack<Integer>();
    for(int i=0;i<n;i++){
        if(i != middle)
          stack.push(a[i]);
    }
    

    其他一切看起来都还不错。只需确保为变量提供有意义的名称。

    【讨论】:

      【解决方案3】:

      您可以简单地使用递归来删除堆栈的中间元素。

      • 当前 = 0

      • mid = stack.size()/2

         static void delete(Stack<Integer> stack, int current, int mid){
        
            if(stack.isEmpty())
               return;
        
            if(current == mid){
               stack.pop();
               return;
            }
        
            int x = stack.pop();
            current++;
            delete(stack, current, mid);
            stack.push(x);
           }
        

      【讨论】:

        猜你喜欢
        • 2014-12-29
        • 1970-01-01
        • 2013-11-07
        • 1970-01-01
        • 1970-01-01
        • 2021-08-06
        • 2019-05-20
        • 1970-01-01
        • 2020-08-03
        相关资源
        最近更新 更多