标签:出栈 out str std amp tac sizeof typedef ext
//链栈操作(带头节点)
#include <stdio.h>
#include <malloc.h>
typedef struct LinkStack{
int data;
struct LinkStack* next;
}Stack,*LStack;
Stack * InitStack(LStack *); //初始化
void Push(LStack , int); //入栈
void Pop(LStack); //出栈
void output(LStack); //遍历
int main(void){
LStack S;
InitStack(&S);
if(S->next == NULL){
printf("初始化成功!\n");
}else{
printf("初始化失败!\n");
}
Push(S,1);
Push(S,2);
Push(S,3);
Push(S,4);
Push(S,5);
printf("当前链栈中的数据是:");
output(S);
printf("\n");
Pop(S);
Pop(S);
Pop(S);
printf("当前链栈中的数据是:");
output(S);
return 0;
}
//初始化
Stack * InitStack(LStack *S){
(*S) = (Stack *)malloc(sizeof(Stack));
(*S)->next = NULL;
return (*S);
}
//入栈
void Push(LStack S , int e){
Stack *p = (Stack *)malloc(sizeof(Stack));
p->data = e;
p->next = S->next;
S->next = p;
}
//出栈
void Pop(LStack S){
Stack *p = S->next;
int e;
if(S->next != NULL){
e = p->data;
printf("当前弹出数据是:%d\n",e);
S->next = p->next;
printf("当前栈顶数据是:%d\n",S->next->data);
free(p);
}else{
printf("栈为空!\n");
}
}
//遍历
void output(LStack S){
Stack *p = S->next;
if(p == NULL){
printf("栈为空!");
}else{
while(p != NULL){
printf("%d ",p->data);
p = p->next;
}
}
}
标签:出栈 out str std amp tac sizeof typedef ext
原文地址:https://www.cnblogs.com/Timesi/p/12448091.html