数据结构实践(一)—— 栈

来源:互联网 发布:纪元1404 类似 知乎 编辑:程序博客网 时间:2024/04/30 17:00

数据结构实践(一)—— 栈


使用语言C/C++
关于栈的各种基本操作写了下,包括严蔚敏的《数据结构》中没有详细写出的。
C++用的不是很熟。


Stack.h

/**************************************// Claim of Function in Sequence Stack***************************************/#ifndef __STACK_H__ #define __STACK_H__typedef int SElemType;   // Change the definition of SElemType for various data typestypedef class SqStack{      private:        SElemType* top;        SElemType* base;        int stacksize;    public:        SqStack();        bool InitStack();        bool DestroyStack();        bool ClearStack();        bool IsEmpty();        bool Push(SElemType p);        bool Pop(SElemType &e);        bool show();        int StackLen();        SElemType* Base();        SElemType* Top();    protected:        void Resetbase(SElemType* sbase);        void Resettop(SElemType* stop);}SqStack;#endif 

Stack.cpp

/********************************************// Definition of Function in Sequence Stack********************************************/#include <stdio.h>#include <stdlib.h>#include "Stack.h"#define STACK_SIZE_INIT 100#define STACK_SIZE_EXTRA 10// Initiate a stackbool SqStack::InitStack(){    if (base)        DestroyStack();    base = (SElemType *)malloc(STACK_SIZE_INIT * sizeof(SElemType));    if (!base)         return false;       top = base;    stacksize = STACK_SIZE_INIT;    return true;}// Destroy an existed stackbool SqStack::DestroyStack(){    if (!base)        return false;    free(base);    top = NULL;    base = NULL;    stacksize = 0;    return true;}// Clear the content of the stackbool SqStack::ClearStack(){    if (!base)        return false;    top = base;    stacksize = 0;    return true;}// Judge if the stack is emptybool SqStack::IsEmpty(){    if (top == base)        return true;    return false;}// Add an element to the stackbool SqStack::Push(SElemType p){    if (!base)        return false;    while (top - base >= stacksize)        base = (SElemType *)realloc(base, (stacksize += STACK_SIZE_EXTRA) * sizeof(SElemType));    if (!base)        return false;    *top++ = p;    if (*top != p)        return false;    return true;}// Pop out an elementbool SqStack::Pop(SElemType &e){    if (!base)        return false;    if (top == base)        return false;    e = *--top;    return true;}// Reset the base point of the stack (Advanced Function) void SqStack::Resetbase(SElemType* sbase){    base = sbase;}// Reset the top point of the stack (Advanced Function)void SqStack::Resettop(SElemType* stop){    top = stop;}// Get the base pointSElemType* SqStack::Base(){    return base;}// Get the top pointSElemType* SqStack::Top(){    return top;}// Get the length of the stackint SqStack::StackLen(){    return stacksize;}// Show all the element of the stackbool SqStack::show(){    if (!base)        return false;    if (top == base){        printf("Empty!");        return true;    }    SElemType* p;    printf("The Stack contain element include:\n");    for (p = base; p < top; p++){        printf("%d\n",*p);    }    return true;}// Initiate the parameter using initiating list SqStack::SqStack():stacksize(0){    base = (SElemType *)malloc(STACK_SIZE_INIT * sizeof(SElemType));        top = base;    stacksize = STACK_SIZE_INIT;}

main.cpp(测试用)

#include <stdio.h>#include "Stack.h"// Change the definition of SElemType for various data types#define SElemType int    void main(){    SqStack S;    for (int i = 1; i <= 10; i++){        S.Push(i);    }    S.show();}
原创粉丝点击