ALGORITHM1.2 Pushdown stack (linked-list implementation)(P149)

//链表做的stack

java版:

import edu.princeton.cs.algs4.*;
import java.util.Iterator;

public class Stack<Item> implements Iterable<Item>
{
    private Node first;
    private int N;
    
    private class Node 
    {
        Item item;
        Node next;
    }
    
    public boolean isEmpty()
    {
        return first == null;//or: N == 0;
    }
    
    public int size()
    {
        return N;
    }
    
    public void push(Item item)
    {
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        N++;
    }
    
    public Item pop()
    {
        Item item = first.item;
        first = first.next;
        N--;
        return item;
    }
    
    public Iterator<Item> iterator()
    {
        return new ListIterator();
    }
    
    //see page 155 for iterator() implementation.
    private class ListIterator implements Iterator<Item>
    {
        private Node current = first;
        public boolean hasNext()
        {
            return current != null;
        }
        
        public void remove()
        {
        }
        
        public Item next()
        {
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
    
    //see page 147 for test client main()
    public static void main(String[] args)
    {
        Stack<String> s = new Stack<String>();
        while(!StdIn.isEmpty())
        {
            String item = StdIn.readString();
            if(!item.equals("-"))
                s.push(item);
            else if(!s.isEmpty())
                StdOut.print(s.pop() + " ");
        }
        StdOut.println("(" + s.size() + " left on stack");
    }
}
java版

c++版:

#include <iostream>
#include <string>

template<class T>
class Stack
{
    private:
        int N;
        class Node
        {
             public:
                T item;
                Node * next;
        };
        Node * first;
    public:
        Stack();
        ~Stack();
        bool isEmpty();
        int size();
        void push(T item);
        T pop();
};

template<class T>
Stack<T>::Stack() : N(0)
{
}

template<class T>
Stack<T>::~Stack()
{
    for(int i = 0; i < N; i++)
    {
        Node * oldfirst = first;
        first = first->next;
        delete oldfirst;
    }
}

template<class T>
bool Stack<T>::isEmpty()
{
    return N == 0;
}

template<class T>
int Stack<T>::size()
{
    return N;
}

template<class T>
void Stack<T>::push(T item)
{
    N++;
    Node  * oldfirst = first;
    first = new Node();
    first->item = item;
    first->next = oldfirst;
}

template<class T>
T Stack<T>::pop()
{
    N--;
    T item = first->item;
    Node * oldfirst = first;
    first = first->next;
    delete oldfirst;
    return item;
}

int main()
{
    using namespace std;
    Stack<string> * p = new Stack<string>();
    string str;
        while(cin >> str)//(CTRL + d)
    {
        if(str != "-")
        {
            p->push(str);
            cout << "push " << str << endl;
        }
        else if(!p->isEmpty())
        {
            cout << "pop ?" << endl;
            cout << "pop " << p->pop() << endl;
        }
    }
    cout << p->size() << " left on stack" << endl;
    delete p;
    return 0;
}
c++版

//c++学的真心不咋样,等等暑假再去刷一遍书(QAQ);

猜你喜欢

转载自www.cnblogs.com/w-j-c/p/8974001.html
今日推荐