栈的数组实现
stack.h
#ifndef _STACK_ #define _STACK_ #define SIZE 100 typedef int data_t; typedef struct head{ data_t data[SIZE]; int top; }stack_t; stack_t *stack_create(); int stack_is_empty(stack_t *head); int stack_is_full(stack_t *head); void stack_clear(stack_t *head); int stack_push(stack_t *head, data_t data); data_t stack_pop(stack_t *head); data_t stack_get_top(stack_t *head); void stack_show(stack_t *head); void stack_destory(stack_t **head); #endif
stack.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <strings.h> #include "sqstack.h" stack_t *stack_create() { stack_t *head = (stack_t *)malloc(sizeof(stack_t)); if (head == NULL) { printf("malloc failed!\n"); return NULL; } bzero(head, sizeof(stack_t)); head->top = -1; return head; } int stack_is_empty(stack_t *head) { return head->top == -1; } int stack_is_full(stack_t *head) { return head->top == SIZE - 1; } void stack_clear(stack_t *head) { head->top = -1; } int stack_push(stack_t *head, data_t data) { if (stack_is_full(head)) { printf("stack is full!\n"); return -1; } head->data[head->top+1] = data; head->top++; return 0; } data_t stack_pop(stack_t *head) { if (stack_is_empty(head)) { printf("stack is empty!\n"); return -1; } data_t data = head->data[head->top]; head->top--; return data; } data_t stack_get_top(stack_t *head) { if (stack_is_empty(head)) { printf("stack is empty!\n"); return -1; } return head->data[head->top]; } void stack_show(stack_t *head) { int i; for (i = head->top; i >= 0; i--) { printf("%d, ", head->data[i]); } printf("\n"); } void stack_destory(stack_t **head) { free(*head); *head = NULL; }
main.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <strings.h> #include "sqstack.h" int main() { stack_t *head = stack_create(); int n = 10; while (n--) { stack_push(head, n+1); } stack_show(head); int i; for (i = 0; i < 10; i++) { printf("%d, ", stack_get_top(head)); printf("%d\n", stack_pop(head)); } stack_show(head); stack_destory(&head); return 0; }
栈的链表实现
lstack.h
#ifndef _STACK_ #define _STACK_ typedef int data_t; typedef struct node{ data_t data; struct node *next; }NODE; NODE *stack_create(); int stack_is_empty(NODE *head); int stack_is_full(NODE *head); int stack_length(NODE *head); void stack_clear(NODE *head); int stack_push(NODE *head, data_t data); data_t stack_pop(NODE *head); data_t stack_get_top(NODE *head); void stack_show(NODE *head); void stack_destory(NODE **head); #endif
lstack.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <strings.h> #include "lstack.h" NODE *stack_create() { NODE *head = (NODE *)malloc(sizeof(NODE)); if (NULL == head) { printf("malloc failed!\n"); return NULL; } bzero(head, sizeof(NODE)); head->data = -1; head->next = NULL; return head; } int stack_is_empty(NODE *head) { return head->next == NULL; } int stack_is_full(NODE *head) { return 0; } int stack_length(NODE *head) { NODE *p = head->next; int len = 0; while (NULL != p) { len++; p = p->next; } return len; } void stack_clear(NODE *head) { NODE *p = head->next; NODE *q = NULL; while (NULL != p) { q = p->next; free(p); p = q; } head->next = NULL; } int stack_push(NODE *head, data_t data) { NODE *p = head; NODE *q = (NODE *)malloc(sizeof(NODE)); if (NULL == q) { printf("malloc failed!\n"); return -1; } q->data = data; q->next = NULL; q->next = p->next; p->next = q; return 0; } data_t stack_pop(NODE *head) { if (stack_is_empty(head)) { printf("list is empty!\n"); return -1; } data_t data = head->next->data; NODE *p = head; NODE *q = head->next; p->next = q->next; free(q); q = NULL; return data; } data_t stack_get_top(NODE *head) { if (stack_is_empty(head)) { printf("list is empty!\n"); return -1; } return head->next->data; } void stack_show(NODE *head) { NODE *p = head->next; while (NULL != p) { printf("%d, ", p->data); p = p->next; } printf("\n"); } void stack_destory(NODE **head) { stack_clear(*head); free(*head); *head = NULL; }
main.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <strings.h> #include "lstack.h" int main() { NODE *head = stack_create(); if (NULL == head) { printf("create failed!\n"); return -1; } int n = 10; while (n--) { if (-1 == stack_push(head, n+1)) { printf("insert failed!\n"); break; } } stack_show(head); int i; for (i = 0; i < 10; i++) { printf("%d, ", stack_get_top(head)); printf("%d\n", stack_pop(head)); } stack_destory(&head); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理