7-1 jmu-Java-05集合-01-ListIntegerStack(分数 15)

30 篇文章 0 订阅

定义IntegerStack接口,该接口描述了一个存放Integer的栈的常见方法:

 

public Integer push(Integer item); // 如item为null,则不入栈直接返回null。否则直接入栈,然后返回item。 public Integer pop(); // 出栈,如栈为空,则返回null。 public Integer peek(); // 获得栈顶元素,如栈顶为空,则返回null。注意:不要出栈 public boolean empty(); // 如果栈为空返回true public int size(); // 返回栈中元素数量

定义IntegerStack的实现类ArrayListIntegerStack,内部使用ArrayList存储。该类中包含:

构造方法:
在无参构造方法中新建ArrayList或者LinkedList,作为栈的内部存储。
思考:查询JDK文档,尝试说明本题到底使用哪个List实现类最好。

方法:
public String toString() //用于输出List中的内容,可直接调用List的toString()方法。可用System.out.println(list)进行输出。

提示:

  1. 不建议使用top指针。最好直接复用List实现类中已有的方法。
  2. pop时应将相应的元素从列表中移除。

main方法说明

  1. 建立ArrayListIntegerStack对象
  2. 输入m个值,均入栈。每次入栈均打印入栈返回结果。
  3. 输出: 栈顶元素,输出是否为空,然后输出size.
  4. 输出栈中所有元素(调用其toString()方法)
  5. 输入x,然后出栈x次,每次均打印出栈的对象。
  6. 输出:栈顶元素,输出是否为空,输出size。注意:这里的输出栈顶元素,仅输出、不出栈。
  7. 输出栈中所有元素(调用其toString()方法)。注意:返回null,也要输出。

思考:

如果使用LinkedList来实现IntegerStack,怎么实现?测试代码需要进行什么修改?

输入样例

5
1 3 5 7 -1
2

输出样例

1
3
5
7
-1
-1,false,5
[1, 3, 5, 7, -1]
-1
7
5,false,3
[1, 3, 5]
import com.sun.jdi.connect.spi.TransportService;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

interface GeneralStack {
    public Integer push(Integer item);            //如item为null,则不入栈直接返回null。
    public Integer pop();                 //出栈,如为栈为空,则返回null。
    public Integer peek();                //获得栈顶元素,如为空,则返回null.
    public boolean empty();//如为空返回true
    public Integer size();     //返回栈中元素数量

}

class ArrayListGeneralStack implements GeneralStack {
    private LinkedList<Integer> linkedList;
    public ArrayListGeneralStack() {
        this.linkedList = new LinkedList<Integer>();
    }

    @Override
    public Integer push(Integer item) {
        if (item == null) return null;
        this.linkedList.add(item);
        return item;
    }

    @Override
    public Integer pop() {
        if (linkedList.isEmpty()) {
            return null;
        }
        Integer a = linkedList.get(linkedList.size() - 1);
        linkedList.pollLast();
        return a;
    }

    @Override
    public Integer peek() {
        if (linkedList.isEmpty()) {
            return null;
        } else {
            return linkedList.get(linkedList.size() - 1);
        }
    }

    @Override
    public boolean empty() {
        return linkedList.isEmpty();
    }

    @Override
    public Integer size() {
        return linkedList.size();
    }

    @Override
    public String toString() {
        return linkedList + "";
    }
}

public class Main {
    public static void main(String[] args) {
        ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        for (int i = 0; i < n; i ++ ) {
            int a = cin.nextInt();
            System.out.println(arrayListGeneralStack.push(a));
        }
        System.out.println(arrayListGeneralStack.peek()
                + "," + arrayListGeneralStack.empty()
                + "," + arrayListGeneralStack.size());
        System.out.println(arrayListGeneralStack.toString());
        int m = cin.nextInt();
        for (int i = 0; i < m; i ++ ) {
            System.out.println(arrayListGeneralStack.pop());
        }
        System.out.println(arrayListGeneralStack.peek()
                + "," + arrayListGeneralStack.empty()
                + "," + arrayListGeneralStack.size());
        System.out.println(arrayListGeneralStack.toString());
    }
}

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值