C++模版类实现顺序栈和单链栈并用栈实现Fibnacci的非递归算法

1、Stack.h

#ifndef Stack_h
#define Stack_h
const int maxSize = 50;
template <class T>
class Stack{
public:
    Stack(){}
    virtual ~Stack(){}
    virtual void Push(const T& x) = 0;
    virtual bool Pop(T& x) = 0;
    virtual bool getTop(T& x)const = 0;
    virtual bool IsEmpty()const = 0;
    virtual int getSize()const = 0;
};

#endif /* Stack_h */

2、SeqStack.h

#ifndef SeqStack_h
#define SeqStack_h
#include <iostream>
#include <assert.h>
#include "Stack.h"
using std::ostream;
const int stackIncreament = 20;
template <class T>
class SeqStack : public Stack<T>{
private:
    T* elements;
    int top;
    int maxSize;
    void overflowProcess();
public:
    SeqStack(int sz = 50);
    ~SeqStack(){delete []elements;}
    void Push(const T& x);
    bool Pop(T& x);
    bool getTop(T& x)const;
    bool IsEmpty()const{return (top == -1) ? true:false;}
    bool IsFull()const{return (top == maxSize-1) ? true:false;}
    int getSize()const{return top+1;}
    void MakeEmpty(){top = -1;}
    friend ostream& operator<<(ostream& os,SeqStack<T>& s);
};

#endif /* SeqStack_h */

3、SeqStack.cpp

#include <stdio.h>
#include <assert.h>
#include "SeqStack.h"
using std::ostream;
using std::endl;

template <class T>
SeqStack<T>::SeqStack(int sz) : top(-1), maxSize(sz){
    elements = new T[maxSize];
    assert(elements != NULL);
}

template <class T>
void SeqStack<T>::overflowProcess(){
    T* newArray = new T[maxSize + stackIncreament];
    assert(newArray != NULL);
    for(int i = 0; i <= top; i ++)
        newArray[i] = elements[i];
    maxSize = maxSize + stackIncreament;
    delete []elements;
    elements = newArray;
}

template <class T>
void SeqStack<T>::Push(const T& x){
    if(IsFull() == true) overflowProcess();
    elements[++ top] = x;
}

template <class T>
bool SeqStack<T>::Pop(T& x){
    if(IsEmpty() == true)
        return false;
    x = elements[top --];
    return true;
}

template <class T>
bool SeqStack<T>::getTop(T &x)const{
    if(IsEmpty() == true)
        return false;
    x = elements[top];
    return true;
}

template <class T>
ostream& operator<<(ostream& os, SeqStack<T>& s){
    os << "top = " << s.top << endl;
    for(int i = 0; i<= s.top; i ++)
        os << i+1 << ":" << s.elements[i] << endl;
    return os;
}

4、LinkedStack.h

#ifndef LinkedStack_h
#define LinkedStack_h
#include <iostream>
#include "Stack.h"
using std::ostream;
template <class T>
struct LinkNode{
    T data;
    LinkNode<T>* link;
    LinkNode(LinkNode<T>* ptr = NULL) : link(ptr){}
    LinkNode(const T& item, LinkNode<T> *ptr = NULL) : data(item), link(ptr){}
};

template <class T>
class LinkedStack : public Stack<T>{
private:
    LinkNode<T>* top;
public:
    LinkedStack() : top(NULL){}
    ~LinkedStack(){makeEmpty();}
    void Push(const T& x);
    bool Pop(T& x);
    bool getTop(T& x)const;
    bool IsEmpty()const{return (top == NULL) ? true:false;}
    int getSize()const;
    void makeEmpty();
    friend ostream& operator<<(ostream& os,LinkedStack<T>& s);
};

#endif /* LinkedStack_h */

5、LinkedStack.cpp

#include <stdio.h>
#include "LinkedStack.h"
using std::ostream;
using std::endl;

template <class T>
void LinkedStack<T>::makeEmpty(){
    LinkNode<T>* p;
    while(top != NULL){
        p = top;
        top = top->link;
        delete p;
    }
}

template <class T>
void LinkedStack<T>::Push(const T &x){
    top = new LinkNode<T>(x, top);
    assert(top != NULL);
}

template <class T>
bool LinkedStack<T>::Pop(T &x){
    if(IsEmpty() == true)
        return false;
    LinkNode<T>* p = top;
    top = top->link;
    x = p->data;
    delete p;
    return true;
}

template <class T>
bool LinkedStack<T>::getTop(T &x)const{
    if(IsEmpty() == true)
        return false;
    x = top->data;
    return true;
}

template <class T>
int LinkedStack<T>::getSize()const{
    LinkNode<T>* p = top;
    int count = 0;
    while(p != NULL){
        p = p->link;
        count ++;
    }
    return count;
}

template <class T>
ostream& operator<<(ostream& os, LinkedStack<T>& s){
    os << "栈中元素个数 =" << s.getSize() << endl;
    LinkNode<T>* p = s.top;
    int i = 0;
    while(p != NULL)
        os << ++i << ":" << p->data << endl;
    return os;
}

6、main.cpp

#include <iostream>
#include "LinkedStack.cpp"
#include "SeqStack.cpp"
using std::cout;
using std::endl;
struct Node{
    long n;
    int tag;
};

//Fibnacci用栈实现的非递归算法
long Fibnacci(long n){
    LinkedStack<Node> S;
    Node w;
    long sum = 0;
    do{
        while(n > 1){
            w.n = n;
            w.tag = 1;
            S.Push(w);
            n --;
        }
        sum += n;
        while(S.IsEmpty() == false){
            S.Pop(w);
            if(w.tag == 1){
                w.tag = 2;
                S.Push(w);
                n = w.n - 2;
                break;
            }
        }
    }while(S.IsEmpty() == false);
    return sum;
}

//Fibnacci非递归算法(迭代法)——单向递归都可转化为迭代法
long FibIter(long n){
    if(n <= 1)
        return n;
    long twoback = 0, oneback = 1, current = 0;
    for(int i = 2; i <= n; i++){
        current = twoback + oneback;
        twoback = oneback;
        oneback = current;
    }
    return current;
}

int main(){
    long b = 4;
    long a = Fibnacci(b);
    cout << a << endl;
    return 0;
}
发布了13 篇原创文章 · 获赞 0 · 访问量 446

猜你喜欢

转载自blog.csdn.net/Nemoosi/article/details/104388887