user-0 提问: 小萌新求教啊这个用C语言怎么写啊顺序表和栈没学太懂啊

回复:
user: user-1
content: cseqstack.h ------------------------------------- #ifndef C_SEQ_STACK_H #define C_SEQ_STACK_H  /* 结构体：基于顺序表的栈 */ typedef struct c_sequence_stack {     // 数据容器     void** arr;     // 数据大小     int dsize;     // 数据容量     int cap;     // 当前栈顶     int top; }cseqstack;  /* 顺序表栈初始化 */ void cseqstack_init(cseqstack*, int, int); /* 顺序表栈是否为空 */ int cseqstack_empty(cseqstack*); /* 顺序表栈是否已满 */ int cseqstack_full(cseqstack*); /* 顺序表栈栈顶访问 */ int cseqstack_top(cseqstack*, void*); /* 顺序表栈压入数据 */ int cseqstack_push(cseqstack*, void*); /* 顺序表栈弹出数据 */ int cseqstack_pop(cseqstack*, void*); /* 顺序表栈清空 */ void cseqstack_clear(cseqstack*); /* 顺序表栈数据量 */ int cseqstack_length(cseqstack*); /* 顺序表栈销毁 */ void cseqstack_deinit(cseqstack*);  #endif /* C_SEQ_STACK_H */  cseqstack.c ------------------------------------------- #include "cseqstack.h" #include <stdio.h> #include <string.h> #include <stdlib.h>  /* 顺序表栈初始化 */ void cseqstack_init(cseqstack* css, int dsize, int cap) { 	css->dsize = dsize;     css->cap = cap;     css->top = 0;     css->arr = (void**)malloc(sizeof(void*) * cap);     memset(css->arr, 0 ,sizeof(void*) * cap); }  /* 顺序表栈是否为空 */ int cseqstack_empty(cseqstack* css) {     return !css->top; }  /* 顺序表栈是否已满 */ int cseqstack_full(cseqstack* css) {     return (css->top == css->cap); }  /* 顺序表栈栈顶访问 */ int cseqstack_top(cseqstack* css, void* data) {     if (!css->top)         return 0;     memcpy(data, css->arr[css->top - 1], css->dsize);     return 1; }  /* 顺序表栈压入数据 */ int cseqstack_push(cseqstack* css, void* data) {     if (css->top == css->cap)         return 0;     css->arr[css->top] = malloc(css->dsize);     memcpy(css->arr[css->top++], data, css->dsize);     return 1; }   /* 顺序表栈弹出数据 */ int cseqstack_pop(cseqstack* css, void* data) {     if (!css->top)         return 0; 	if (data)     	memcpy(data, css->arr[--css->top], css->dsize);     free(css->arr[css->top]);     return 1; }  /* 顺序表栈清空 */ void cseqstack_clear(cseqstack* css) { 	while (css->top)     	free(css->arr[--css->top]); }  /* 顺序表栈数据量 */ int cseqstack_length(cseqstack* css) {     return css->top; }  /* 顺序表栈销毁 */ void cseqstack_deinit(cseqstack* css) { 	while (css->top)     	free(css->arr[--css->top]);     free(css->arr);     css->dsize = 0;     css->cap = 0; }  main.c 测试程序 ---------------------------------------- #include <stdio.h> #include "cseqstack.h"  #define STACK_CAP 5  int main(void) {     int i = 0;     /* 创建顺序表栈对象 */     cseqstack css; 	/* 初始化顺序表栈对象，设置数据大小，数据容量 */     cseqstack_init(&css, sizeof(int), STACK_CAP);     if (cseqstack_empty(&css))     { 		printf("current stack is empty!\n");     }     printf("push data into stack...\n");     for (i = 0; i < STACK_CAP; ++i)     { 		cseqstack_push(&css, &i);         int data = 0;         cseqstack_top(&css, &data);         printf("current top: %d\n", data);     } 	if (cseqstack_full(&css))     { 		printf("current stack is full!\n");     }     printf("pop data from stack...\n");     for (i = 0; i < STACK_CAP; ++i)     {         int data = 0;         cseqstack_pop(&css, &data);         printf("current data: %d\n", data);     } 	if (cseqstack_empty(&css))     { 		printf("current stack is empty!\n");     } 	printf("push data into stack...\n");     for (i = 0; i < STACK_CAP; ++i)     { 		cseqstack_push(&css, &i);         int data = 0;         cseqstack_top(&css, &data);         printf("current top: %d\n", data);     } 	int length = cseqstack_length(&css);     printf("stack have %d data.\n", length);     printf("stack clear...\n");     cseqstack_clear(&css);     length = cseqstack_length(&css);     printf("stack have %d data.\n", length);     if (cseqstack_empty(&css))     { 		printf("current stack is empty!\n");     }     /* 销毁顺序表栈 */     cseqstack_deinit(&css);     return 0; }

user: user-2
content: https://bbs.csdn.net/topics/392520476