前言
动态数组栈的基本操作
一、头文件
#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);
}
总结
这个不难,栈满时可以扩容,参见动态数组队列。