算法(Algorithms)第4版 练习 1.3.7

本文介绍了一个使用Java实现的栈数据结构,包括基本操作如push、pop和peek等,并通过命令行交互方式演示了栈的操作过程。
package com.qiusongde;

import java.util.Iterator;
import java.util.NoSuchElementException;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class Stack<Item> implements Iterable<Item> {

    private Node first;
    private int n;
    
    private class Node {
        Item item;
        Node next;
    }
    
    public Stack() {
        first = null;
        n = 0;
    }
    
    public void push(Item item) {
        Node oldfirst = first;
        
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        
        n++;
    }
    
    public Item pop() {
        
        if(isEmpty()) 
            throw new NoSuchElementException("Stack is empty");
        
        Item item = first.item;
        first = first.next;
        n--;
        
        return item;
    }
    
    //1.3.7
    public Item peek() {
        if(isEmpty())
            throw new NoSuchElementException("Stack is empty");
        
        return first.item;
    }
    
    public boolean isEmpty() {
        return first == null;
    }
    
    public int size() {
        return n;
    }

    @Override
    public Iterator<Item> iterator() {
        
        return new StackIterator();
    }
    
    private class StackIterator implements Iterator<Item> {

        private Node current = first;
        
        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Item next() {
            if(!hasNext())
                throw new NoSuchElementException("Stack is empty");
            
            Item item = current.item;
            current = current.next;
            
            return item;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Stack don't support remove");
        }
        
    }
    
    public static void main(String[] args) {
        
        Stack<String> stack = new Stack<String>();
        StdOut.println("Initialized size:" + stack.size());
        
        while (!StdIn.isEmpty()) {
            
            String item = StdIn.readString();
            
            if (!item.equals("-")) {
                
                stack.push(item); 
                StdOut.println("push success:" + item + " size:" + stack.size());
                
                StdOut.print("Left on stack: ");
                for (String s : stack) {
                    StdOut.print(s + " ");
                }
                StdOut.println();
                
            } else {
                if(stack.isEmpty())
                    StdOut.println("pop error, stack empty");
                else {
                    StdOut.println("pop success:" + stack.pop() + " size:" + stack.size());
                    
                    StdOut.print("Left on stack: ");
                    for (String s : stack) {
                        StdOut.print(s + " ");
                    }
                    StdOut.println();
                }
            }
            
        }
        
    }
    
}

 

转载于:https://www.cnblogs.com/songdechiu/p/6513932.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值