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

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


使用语言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 types


typedef 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 stack
bool 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 stack
bool SqStack::DestroyStack(){
    if (!base)
        return false;
    free(base);
    top = NULL;
    base = NULL;
    stacksize = 0;
    return true;
}


// Clear the content of the stack
bool SqStack::ClearStack(){
    if (!base)
        return false;
    top = base;
    stacksize = 0;
    return true;
}


// Judge if the stack is empty
bool SqStack::IsEmpty(){
    if (top == base)
        return true;
    return false;
}

// Add an element to the stack
bool 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 element
bool 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 point
SElemType* SqStack::Base(){
    return base;
}


// Get the top point
SElemType* SqStack::Top(){
    return top;
}


// Get the length of the stack
int SqStack::StackLen(){
    return stacksize;
}


// Show all the element of the stack
bool 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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值