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

Posted w-j-c

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);

 

以上是关于ALGORITHM1.2 Pushdown stack (linked-list implementation)(P149)的主要内容,如果未能解决你的问题,请参考以下文章

Index Condition Pushdown Optimization

Index Condition Pushdown(索引下推)

MySQL Index Condition Pushdown

MySQL 索引条件下推 Index Condition Pushdown

MySQL索引与Index Condition Pushdown

MySQL 5.6 Index Condition Pushdown