数据结构复习笔记(三)——栈和队列之栈

栈的定义

仅在表尾进行插入删除操作的线性表

栈的抽象数据类型

栈的抽象数据类型

栈的线性存储结构

using namespace std;

typedef int SElemType;
#define INIT_SIZE 10

typedef struct
{
    SElemType *base;//栈底指针,在栈构造之前和之后销毁,base值为NULL
    SElemType *top;//栈顶指针
    int stacksize;
}SqStack;

void InitStack(SqStack &s, int init_size) {
    s.base = (SElemType *)malloc(sizeof(SElemType) * init_size);
    if (!s.base)
    {
        exit(0);
    }
    s.top = s.base;
    s.stacksize = init_size;
}

void DestroyStack(SqStack &s) {
    free(s.base);
    s.base = NULL;
    s.top = NULL;
    s.stacksize = 0;
}

void ClearStack(SqStack &s) {
    s.top = s.base;
}

int StackEmpty(SqStack s) {
    if (s.top == s.base) {
        return 1;
    }
    else
        return 0;

}

SElemType GetTop(SqStack s, SElemType &e) {
    if (s.top == s.base)
    {
        exit(0);
    }
    e = *(s.top - 1);
    return e;
}

SqStack Push(SqStack &s, SElemType e) {
    
    if (s.top - s.base >= s.stacksize) { //若栈满
        s.base = (SElemType *)realloc(s.base, (s.stacksize + 10) * sizeof(SElemType)); //重新分配空间
        if (!s.base)
            exit(0);
        s.top = s.base + s.stacksize;
        s.stacksize += 10;
    }
    
    *s.top++ = e;
    return s;
}

SqStack Pop(SqStack &s, SElemType &e) {
    if (s.top == s.base)
        exit(0);
    e = *--s.top;
    return s;
}

void StackTreaverse(SqStack s) {
    while (s.top > s.base)
    {
        cout << *s.base++ << "  ";
    }
    cout << endl;
}

两栈共享空间

typedef int SElemType;
#define MAXSIZE 20

using namespace std;

typedef struct{
    SElemType data[MAXSIZE];
    SElemType top1;
    SElemType top2;
}SqDoubleStack;

SqDoubleStack Push(SqDoubleStack *s, int e, int StackNumber) {
    if(s->top1 + 1 == s->top2)
    {
        printf("栈满!\n");
        exit(0);
    }
    if (StackNumber == 1)
    {
        s->data[++s->top1] = e;
    }
    else if (StackNumber == 2)
    {
        s->data[--s->top2] = e;
    }

    return *s;

}

SqDoubleStack Pop(SqDoubleStack *s, int *e, int StackNumber)
{
    if (StackNumber == 1)
    {
        if (s->top1 == -1)
        {
            printf("栈1是空栈!\n");
            exit(0);
        }

        *e = s->data[s->top1--]; 
    }
    else if (StackNumber == 2)
    {
        if (s->top2 == MAXSIZE)
        {
            printf("栈2是空栈!\n");
            exit(0);
        }

        *e = s->data[s->top2++];
    }

    return *s;
}

栈的链式存储结构

typedef int SElemType;


typedef struct StackNode {
    SElemType data;
    struct StackNode *next;
}StackNode, *LinkStackPtr;

typedef struct LinkStack {
    LinkStackPtr top;
    int count;
}LinkStack;

LinkStack Push(LinkStack *S, SElemType e) {
    LinkStackPtr s = (LinkStackPtr)malloc(sizeof(StackNode));
    s->data = e;
    s->next = S->top;
    S->top = s;
    S->count++;
    return *S;

}

LinkStack Pop(LinkStack *S, SElemType *e) {
    LinkStackPtr p;
    *e = S->top->data;
    p = S->top;
    S->top = S->top->next;
    free(p);
    S->count--;
    return *S;


}

你可能感兴趣的:(数据结构复习笔记(三)——栈和队列之栈)