【上机实验】04:栈相关操作

Stack栈


1.顺序栈操作:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

typedef struct SqStack {
    int *base;//栈底指针
    int top;//栈顶指针
    int stacksize;//栈可用最大容量
} SqStack;

//1.初始化栈操作
SqStack *InitStack(int n) {
    SqStack *s = (SqStack *)malloc(sizeof(SqStack));
    s->base = (int *)malloc(sizeof(int) * n);
    s->stacksize = n;
    s->top = -1;
    return s;
}

//2.栈的销毁操作
void DestroyStack(SqStack *s) {
    if (s == NULL) return;
    free(s->base);
    free(s);
    return;
}

//3.判断栈是否为空
int StackEmpty(SqStack *s) {
    return s->top == -1;
}

//4.获取栈顶元素
int GetTop(SqStack *s) {
    return s->base[s->top];
}

//5.入栈操作
int Push(SqStack *s, int val) {
    if (s == NULL) return 0;
    if (s->top == s->stacksize - 1) return 0;//栈已满
    s->top++;//注意:这里栈顶指针指向的是栈顶元素故需要先++再赋值
    s->base[s->top] = val;
    return 1;
}

//6.出栈操作
int Pop(SqStack *s) {
    if (s == NULL) return 0;
    if (StackEmpty(s)) return 0;
    s->top--;
    return 1;
}

//print操作
void print(SqStack *s) {
    if (s == NULL) return;
    printf("Stack : [");
    for (int i = 0; i <= s->top; ++i){
        i && printf(", ");
        printf("%d", s->base[i]);
    }
    printf("]\n");
    return;
}

int main(){
    srand(time(0));
    #define MAX_OP 20
    SqStack *s = InitStack(MAX_OP);
    for (int i = 0; i < MAX_OP; ++i) {
        int op = rand() % 4;
        int val = rand() % 100;
        switch (op) {
            case 0:
            case 1:
            case 2: {
                printf("push %d to the Stack = %d\n", val, Push(s, val));
            } break;
            case 3: {
                printf("pop %d from the stack = ", GetTop(s));
                printf("%d\n", Pop(s));
            } break;
        }
        print(s);
    }
    #undef MAX_OP
    DestroyStack(s);
    return 0;
}

在这里插入图片描述

2.顺序栈扩容:

关键代码

//SqStack扩容操作
int expand(SqStack *s) {
    int expand_size = s->stacksize;
    int *p;
    //顺序栈的循环扩容
    while (expand_size) {
        p = (int *)realloc(s->base, sizeof(int) * (s->stacksize + expand_size));
        if (p != NULL) break;//realloc扩容成功
        expand_size >>= 1;//realloc扩容失败则将扩容容量extend缩小,再次尝试扩容
    }
    if (p == NULL) return 0;
    s->stacksize += expand_size;
    s->base = p;
    return 1;
}

//5.入栈操作
int Push(SqStack *s, int val) {
    if (s == NULL) return 0;
    if (s->top == s->stacksize - 1) {
        if (!expand(s)) {
            printf(RED("fail to expand!\n"));
            return 0;
        }
        printf(GREEN("success to expand! the new size = %d\n"), s->stacksize);
    }
    s->top++;//注意:这里栈顶指针指向的是栈顶元素故需要先++再赋值
    s->base[s->top] = val;
    return 1;
}

完整代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define COLOR(a, b) "\033[" #b "m" a "\033[0m"
#define GREEN(a) COLOR(a, 32)
#define RED(a) COLOR(a, 31)

typedef struct SqStack {
    int *base;//栈底指针
    int top;//栈顶指针
    int stacksize;//栈可用最大容量
} SqStack;

//1.初始化栈操作
SqStack *InitStack(int n) {
    SqStack *s = (SqStack *)malloc(sizeof(SqStack));
    s->base = (int *)malloc(sizeof(int) * (n));
    s->stacksize = n;
    s->top = -1;
    return s;
}

//2.栈的销毁操作
void DestroyStack(SqStack *s) {
    if (s == NULL) return;
    free(s->base);
    free(s);
    return;
}

//3.判断栈是否为空
int StackEmpty(SqStack *s) {
    return s->top == -1;
}

//4.获取栈顶元素
int GetTop(SqStack *s) {
    return s->base[s->top];
}

//SqStack扩容操作
int expand(SqStack *s) {
    int expand_size = s->stacksize;
    int *p;
    //顺序栈的循环扩容
    while (expand_size) {
        p = (int *)realloc(s->base, sizeof(int) * (s->stacksize + expand_size));
        if (p != NULL) break;//realloc扩容成功
        expand_size >>= 1;//realloc扩容失败则将扩容容量extend缩小,再次尝试扩容
    }
    if (p == NULL) return 0;
    s->stacksize += expand_size;
    s->base = p;
    return 1;
}

//5.入栈操作
int Push(SqStack *s, int val) {
    if (s == NULL) return 0;
    if (s->top == s->stacksize - 1) {
        if (!expand(s)) {
            printf(RED("fail to expand!\n"));
            return 0;
        }
        printf(GREEN("success to expand! the new size = %d\n"), s->stacksize);
    }
    s->top++;//注意:这里栈顶指针指向的是栈顶元素故需要先++再赋值
    s->base[s->top] = val;
    return 1;
}

//6.出栈操作
int Pop(SqStack *s) {
    if (s == NULL) return 0;
    if (StackEmpty(s)) return 0;
    s->top--;
    return 1;
}

//print操作
void print(SqStack *s) {
    if (s == NULL) return;
    printf("Stack : [");
    for (int i = 0; i <= s->top; ++i){
        i && printf(", ");
        printf("%d", s->base[i]);
    }
    printf("]\n");
    return;
}

int main(){
    srand(time(0));
    #define MAX_OP 20
    SqStack *s = InitStack(1);
    for (int i = 0; i < MAX_OP; ++i) {
        int op = rand() % 4;
        int val = rand() % 100;
        switch (op) {
            case 0:
            case 1:
            case 2: {
                printf("push %d to the Stack = %d\n", val, Push(s, val));
            } break;
            case 3: {
                printf("pop %d from the stack = ", GetTop(s));
                printf("%d\n", Pop(s));
            } break;
        }
        print(s);
    }
    #undef MAX_OP
    DestroyStack(s);
    return 0;
}

在这里插入图片描述

栈的相关练习:Leetcode020有效的括号

3.链栈基本操作:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

typedef struct Stack {
    Node *top;
    int length;
} LinkStack;

//1.初始化结点
Node *InitNode(int val) {
    Node *p = (Node *)malloc(sizeof(Node));
    p->data = val;
    p->next = NULL;
    return p;
}

//2.初始化栈
LinkStack *InitStack() {
    LinkStack *s = (LinkStack *)malloc(sizeof(LinkStack));
    s->top = NULL;
    s->length = 0;
    return s;
}

//3.销毁结点
void DestroyNode(Node *node) {
    if (node == NULL) return;
    free(node);
    return;
}

//4.销毁栈操作
void DestroyStack(LinkStack *ls) {
    if (ls == NULL) return;
    Node *p = ls->top, *temp;
    while (p != NULL) {
        temp = p->next;
        free(p);
        p = temp;
    }
    free(ls);
    return;
}

//5.链栈判空
int Empty(LinkStack *ls) {
    return ls->top == NULL;
}

//6.获取栈顶元素
int GetTop(LinkStack *ls) {
    return ls->top->data;
}

//7.链栈入栈
int Push(LinkStack *ls, int val) {
    if (ls == NULL) return 0;
    Node *p = InitNode(val);
    p->next = ls->top;
    ls->top = p;
    ls->length += 1;
    return 1;
}

//8.链栈出栈
int Pop(LinkStack *ls) {
    if (ls == NULL) return 0;
    if (Empty(ls)) return 0;
    Node *temp = ls->top;
    ls->top = ls->top->next;
    free(temp);
    ls->length -= 1;
    return 1;
}

void print(LinkStack *ls) {
    if (ls == NULL) return;
    printf("Stack[%d] : ", ls->length);
    for (Node *p = ls->top; p; p = p->next) {
        p != ls->top && printf(" ");
        printf("%d", p->data);
    }
    printf("\n");
    return;
}

int main(){
    srand(time(0));
    #define MAX_OP 20
    LinkStack *ls = InitStack();
    for (int i = 0; i < MAX_OP; ++i) {
        int op = rand() % 4;
        int val = rand() % 100;
        switch (op) {
            case 0:
            case 1:
            case 2: {
                printf("push %d to the Stack = %d\n", val, Push(ls, val));
            } break;
            case 3: {
                if (!Empty(ls)) {
                    printf("pop %d form the Stack = ", GetTop(ls));
                    printf("%d\n", Pop(ls));
                }
            } break;
        }
        print(ls);
    }
    #undef MAX_OP
    DestroyStack(ls);
    return 0;
}

在这里插入图片描述

4.栈和队列的应用:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值