c语言-链栈
作者:互联网
head
#ifndef __LINKSTACK_H__
#define __LINKSTACK_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define ElemType int
typedef struct StackNode{
ElemType data;
struct StackNode *next;
}StackNode;
typedef StackNode* LinkStack;
void IniStack(LinkStack *s);
void Push(LinkStack *s,ElemType x);
void Show(LinkStack *s);
void Pop(LinkStack *s);
#endif
实现代码
#include"LinkStack.h"
void IniStack(LinkStack *s){
*s=NULL;
}
void Push(LinkStack *s,ElemType x){
StackNode *t=(StackNode*)malloc(sizeof(StackNode));
assert(t!=NULL);
t->data=x;
if(*s==NULL){
*s=t;
t->next=NULL;
}
else{
t->next=*s;
*s=t;
}
}
void Show(LinkStack *s){
StackNode *p=*s;
while(p!=NULL){
printf("%6d",p->data);
p=p->next;
}
printf("\n");
}
void Pop(LinkStack *s){
StackNode *p=*s;
*s=p->next;
free(p);
}
main
#include"LinkStack.h"
void main(){
LinkStack st;
IniStack(&st);
for (int i = 1; i <=5; i++)
{
Push(&st,i);
}
Show(&st);
Pop(&st);
Show(&st);
}
标签:LinkStack,语言,void,st,链栈,next,StackNode,NULL 来源: https://blog.csdn.net/qq_43751489/article/details/110246434