链表来模拟栈

链表来模拟栈

public class LinkedStackDemo {
    public static void main(String[] args) {
        StackNodeList stackNodeList=new StackNodeList();
        int key=-1;
        boolean loop=true;//控制是否退出菜单
        Scanner scanner=new Scanner(System.in);
        while (loop){
            System.out.println("1:表示显示栈");
            System.out.println("2:退出程序");
            System.out.println("3:入栈");
            System.out.println("4:出栈");
            System.out.println("请输入你的选择");
            key=scanner.nextInt();
            switch (key){
                case 1:
                    stackNodeList.list();
                    break;
                case 2:
                    scanner.close();
                    loop=false;
                    break;
                case 3:
                    System.out.println("请输入你的要入栈的值");
                    int next = scanner.nextInt();
                    stackNodeList.push(next);
                    break;
                case 4:
                    try {
                        int pop = stackNodeList.pop();
                        System.out.printf("出栈的数据是%d \n",pop);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
    }

}
class StackNodeList{
    public StackNode head=new StackNode(0);
    public StackNode top=head;

    public boolean  isEmpty(){
        return  top==head;
    }
    public void push(int value){
        StackNode stackNode = new StackNode(value);
        top.next=stackNode;
        top=top.next;
    }
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈空无法取出数据");
        }
         StackNode temp=head;
        while (true){
            if(temp.next==top){
                break;
            }
            temp=temp.next;
        }

        int val=top.value;
        top=temp;
        top.next=null;
        return val;
    }
    public void list(){
        if (isEmpty()){
            System.out.println("栈空,无数据");
            return;
        }
         StackNode temp2=head.next;
        while (temp2!=null){
            System.out.println(temp2.value);
            temp2=temp2.next;
        }

    }
}
class StackNode{
    public int value;
    public StackNode next;

    public StackNode(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "StackNode{" +
                "value=" + value +
                '}';
    }
}
C++中链表实现可以使用链表模拟的结构。在这种实现中,我们可以使用std::list来作为底层数据结构。\[1\]的结构可以定义为一个类,其中包含一个std::list<int>类型的成员变量stk来存储中的元素。此外,我们可以使用两个迭代器bottom和top来表示底和顶。 入操作可以通过在链表的头部插入元素来实现。具体而言,我们可以使用std::list的push_front()函数将元素插入到链表的头部。在入操作中,我们需要将新元素插入到顶位置,并更新顶指针top。 下面是一个简单的C++代码示例,展示了如何使用链表实现的入操作: ```cpp void Stack::push(int value) { stk.push_front(value); top = stk.begin(); } ``` 在这个示例中,我们使用std::list的push_front()函数将新元素插入到链表的头部,然后将top指针更新为链表的第一个元素。 需要注意的是,由于链表没有固定的大小限制,因此在链表实现的中不会出现满的情况。\[1\]因此,我们可以根据需要随时向链表中添加新的元素。 希望这个回答能够帮助到你! #### 引用[.reference_title] - *1* [C++实现链表模拟)【每一步详细深入讲解,代码清晰、简单、易懂】](https://blog.csdn.net/weixin_55755506/article/details/128356314)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值