码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-30 20:01:36      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:stack   end   close   span   iter   ++   client   图片   while   

//链表做的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);

 

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

标签:stack   end   close   span   iter   ++   client   图片   while   

原文地址:https://www.cnblogs.com/w-j-c/p/8974001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!