栈数组与栈链表C语言代码实现
内容摘要
栈数组#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 10000
//结点设计
typedef struct stack{
int data[maxn];
int top;
}stack;
//
#include<stdlib.h>
#include<string.h>
#define maxn 10000
//结点设计
typedef struct stack{
int data[maxn];
int top;
}stack;
//
文章正文
栈数组
#include<stdio.h> #include<stdlib.h> #include<string.h> #define maxn 10000 //结点设计 typedef struct stack{ int data[maxn]; int top; }stack; //创建 stack *init(){ stack *s=(stack *)malloc(sizeof(stack)); if(s==NULL){ printf("分配内存空间失败"); exit(0); } memset(s->data,0,sizeof(s->data)); //memset操作来自于库文件string.h,其表示将整个空间进行初始化 //不理解可以查阅百度百科https://baike.baidu.com/item/memset/4747579?fr=aladdin s->top=0; //栈的top和bottom均为0(表示为空) return s; } //入栈push void push(stack *s,int data){ s->data[s->top]=data; s->top++; } //出栈pop void pop(stack *s){ if(s->top!=0){ s->data[s->top]=0; //让其回归0模拟表示未初始化即可 s->top--; } } //模拟打印栈中元素 void print_stack(stack *s){ for(int n=s->top-1;n>=0;n--){ printf("%d\t",s->data[n]); } printf("\n"); //习惯性换行 } int main(){ stack *s=init(); int input[5]={11,22,33,44,55}; //模拟五个输入数据 for(int i=0;i<5;i++){ push(s,input[i]); } print_stack(s); ///////////// pop(s); print_stack(s); return 0; }
栈链表:
an#include <stdio.h> #include <stdlib.h> //栈的结点设计 //单个结点设计,数据和下一个指针 typedef struct node { int data; struct node *next; } Node; //利用上面的结点创建栈,分为指向头结点的top指针和计数用的count typedef struct stack { Node *top; int count; } Link_Stack; //创建栈 Link_Stack *Creat_stack() { Link_Stack *p; //p = new Link_Stack; p=(Link_Stack*)malloc(sizeof(Link_Stack)); if(p==NULL){ printf("创建失败,即将退出程序"); exit(0); } p->count = 0; p->top = NULL; return p; } //入栈 push Link_Stack *Push_stack(Link_Stack *p, int elem) { if (p == NULL) return NULL; Node *temp; temp=(Node*)malloc(sizeof(Node)); //temp = new Node; temp->data = elem; temp->next = p->top; p->top = temp; p->count++; return p; } //出栈 pop Link_Stack *Pop_stack(Link_Stack *p) { Node *temp; temp = p->top; if (p->top == NULL) { printf("错误:栈为空"); return p; } else { p->top = p->top->next; free(temp); //delete temp; p->count--; return p; } } //遍历栈:输出栈中所有元素 int show_stack(Link_Stack *p) { Node *temp; temp = p->top; if (p->top == NULL) { printf(""); printf("错误:栈为空"); return 0; } while (temp != NULL) { printf("%d\t", temp->data); temp = temp->next; } printf("\n"); return 0; } int main() { //用主函数测试一下功能 Link_Stack *p; p = Creat_stack(); int n = 5; int input[6] = {10,20,30,40,50,60}; /////////////以依次入栈的方式创建整个栈////////////// for(int i=0;i<n;i++){ Push_stack(p, input[i]); } show_stack(p); ////////////////////出栈/////////////////////// Pop_stack(p); show_stack(p); return 0; }
代码注释
[!--zhushi--]