实现动态栈的基本操作——C语言


前言

动态数组栈的基本操作

一、头文件

#ifndef DYNAMIC_STACK_H
#define DYNAMIC_STACK_H
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int ElementType;
#define DEFAULT_CAPACITY 10
typedef struct {
	ElementType* data;//指向动态数组首元素的指针
	int size;
	int capacity;
}DynamicStack;

//创建
DynamicStack* stack_create();
//入栈
void stack_push(DynamicStack* s, ElementType val);
// 访问栈顶元素
ElementType  stack_peek(DynamicStack* s);
// 判空
bool is_empty(DynamicStack* s);
//打印
void print_stack(DynamicStack* s);
// 销毁动态数组栈
void stack_destroy(DynamicStack* s);

#endif//!DYNAMIC_STACK_H

二、基本操作

#include "DynamicStack.h"
//创建
DynamicStack* stack_create() {
	DynamicStack* stack = calloc(1, sizeof(DynamicStack));
    if (stack == NULL) {
        printf("error:calloc failed in stack_create.\n");
        return NULL;
    }
    stack->data = calloc(DEFAULT_CAPACITY, sizeof(ElementType));
    if (stack->data == NULL) {
        printf("error:calloc failed in stack_create.\n");
        return NULL;
    }
    stack->capacity = DEFAULT_CAPACITY;
    return stack;
}
//入栈
void stack_push(DynamicStack* s, ElementType val) {
    if (s->size == s->capacity) {
        printf("栈满\n");
        exit(1);
    }
    s->data[s->size] = val;
    s->size++;
}
//出栈
ElementType stack_pop(DynamicStack* s) {
    if (s->size == 0) {
        printf("栈空\n");
        exit(1);
    }
    int ret = s->data[s->size - 1];
    s->size--;
    return ret;
}
// 访问栈顶元素
ElementType  stack_peek(DynamicStack* s) {
    if (is_empty(s)) {
        printf("栈空,栈顶无元素\n");
        exit(1);
    }
    int ret = s->data[s->size - 1];
    return ret;
}
// 判空
bool is_empty(DynamicStack* s) {
    if (s->size == 0) {
        return true;
    }
    else {
        return false;
    }
}
//打印
void print_stack(DynamicStack* s) {
    for (int i =s->size-1;i>=0; i--) {
        printf("%d", s->data[i]);
        if (i !=0) {
            printf(" -> ");
        }
    }
    printf("\n");
}
// 销毁动态数组栈
void stack_destroy(DynamicStack* s) {
    free(s->data);
    s->data = NULL;
    free(s);
}

三、测试源

#include "DynamicStack.h"
//创建
DynamicStack* stack_create() {
	DynamicStack* stack = calloc(1, sizeof(DynamicStack));
    if (stack == NULL) {
        printf("error:calloc failed in stack_create.\n");
        return NULL;
    }
    stack->data = calloc(DEFAULT_CAPACITY, sizeof(ElementType));
    if (stack->data == NULL) {
        printf("error:calloc failed in stack_create.\n");
        return NULL;
    }
    stack->capacity = DEFAULT_CAPACITY;
    return stack;
}
//入栈
void stack_push(DynamicStack* s, ElementType val) {
    if (s->size == s->capacity) {
        printf("栈满\n");
        exit(1);
    }
    s->data[s->size] = val;
    s->size++;
}
//出栈
ElementType stack_pop(DynamicStack* s) {
    if (s->size == 0) {
        printf("栈空\n");
        exit(1);
    }
    int ret = s->data[s->size - 1];
    s->size--;
    return ret;
}
// 访问栈顶元素
ElementType  stack_peek(DynamicStack* s) {
    if (is_empty(s)) {
        printf("栈空,栈顶无元素\n");
        exit(1);
    }
    int ret = s->data[s->size - 1];
    return ret;
}
// 判空
bool is_empty(DynamicStack* s) {
    if (s->size == 0) {
        return true;
    }
    else {
        return false;
    }
}
//打印
void print_stack(DynamicStack* s) {
    for (int i =s->size-1;i>=0; i--) {
        printf("%d", s->data[i]);
        if (i !=0) {
            printf(" -> ");
        }
    }
    printf("\n");
}
// 销毁动态数组栈
void stack_destroy(DynamicStack* s) {
    free(s->data);
    s->data = NULL;
    free(s);
}


总结

这个不难,栈满时可以扩容,参见动态数组队列。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值