顺序栈的实现(SqStack, C++版)

类别:编程语言 点击:0 评论:0 推荐:

/* stack.h */
#ifndef __SQSTACK_H__
#define __SQSTACK_H__ 1

#include <iostream.h>

extern "C" { void exit(int); }

const int nDefaultStackSize = 50; //缺省Stack元素个数

//以下为栈的顺序存储结构(顺序栈)的C++类定义
template <class T> //声明为模板类
class Stack {
private:
    T   *stacklist; //存放栈元素的指针
    int stacksize;  //存放栈(数组)大小
    int top;        //指示栈顶元素的位置(数组下标)
public:
    //构造函数
    Stack(int initSize = nDefaultStackSize) {
        if (initSize < 1) initSize = nDefaultStackSize;
        stacklist = new T[initSize]; //为stacklist分配存储空间
        if (!stacklist) {
            cerr << "为Stack分配存储空间失败!程序将终止。"
                 << endl;
            exit(1);
        }
        stacksize = initSize; //初始化栈大小
        top = -1; //初始化栈顶位置
    }
    //析构函数
    ~Stack() {
        if (stacklist) delete [] stacklist;
        stacksize = 0;
        top = -1;
    }
    //判断栈是否为空
    int StackEmpty() const {
        return top < 0; //top == -1;
    }
    //判断是否栈满
    int StackFull() const {
        return top == stacksize - 1;
    }
    //返回栈中元素个数
    int StackLength() const {
        return top + 1;
    }
    //压栈
    void Push(const T& item) {
        if (top >= stacksize - 1) {
            cerr << "栈已满,无法继续压栈操作!" << endl;
            return;
        }
        top ++; //修改栈顶位置(下标)
        stacklist[top] = item; //类型T需支持=运算符
    }
    //出栈
    T Pop() {
        T   temp;
        if (top >= 0) {
            temp = stacklist[top]; //转存栈顶元素取值
            top --; //修改栈顶位置
        }
        else
            cerr << "栈为空,无法继续出栈操作!" << endl;
        return temp;
    }
    //读取栈顶元素
    T Peek() const {
        T   returnValue;
        if (top >= 0)
            returnValue = stacklist[top];
        else
            cerr << "栈为空,无法读取栈顶元素!" << endl;
        return returnValue;
    }
    //清空栈
    void ClearStack() {
        top = -1; //栈顶位置复位
    }
};

//将十进制数num转换成toSys(2--36)进制的值(以字符串形式存入destNum并返回)
char *DecimalConversion(unsigned long num, unsigned char toSys, char destNum[])
{
    if (!destNum) return NULL;
    Stack<int>  S(80);
    unsigned char   i = 0;
    int item;
    if (toSys<2 || toSys>36) { destNum[0] = '\0'; return destNum; }
    while (num) {
        S.Push(num % toSys);
        num = num / toSys;
    }
    while (!S.StackEmpty()) {
        item = S.Pop();
        destNum[i] = (unsigned char)(item) +
            (item<10 ? '0' : ('a'-10));
        i++;       
    }
    destNum[i] = '\0';
    return destNum;
}

#endif /* !__SQSTACK_H__ */

////////////////////////////////////////////////////////////////////
// stacktest.cpp
#include <iomanip.h>
#include "stack.h"

void main()
{
    Stack<char *>   s1(10), s2(10);
    char    *weekday[] = {
        "Mon", "Tue", "Wed", "Thr", "Fri", "Sat", "Sun" };
    for (register int i = 0; i < 7; i++)
        s1.Push(weekday[i]);
    cout << "==========s1==========" << endl;
    for (i = 0; i < 7; i++) {
        cout << setw(5) << s1.Peek();
        s2.Push(s1.Pop());
    }
    cout << endl;
    cout << "==========s2==========" << endl;
    for (i = 0; i < 7; i++)
        cout << setw(5) << s2.Pop();
    cout << endl;

    char    s[80];
    cout << DecimalConversion(1024, 2, s) << endl;
}

本文地址:http://com.8s8s.com/it/it26332.htm