版权声明: https://blog.csdn.net/qq_25233621/article/details/80993024
相关知识
1.函数封装
2.堆栈(先进后出,后进先出)
3.指针(内存申请,结构体运用)
头文件及其声明
#ifndef STACKLIST_H_INCLUDED
#define STACKLIST_H_INCLUDED
typedef int ElemType;
typedef struct STACK{
ElemType data;
struct STACK *next;
}Stack;
void CreateStack();
Stack *Push(Stack *head,ElemType val);//入栈
ElemType Pop(Stack *head);//出栈
#endif // STACKLIST_H_INCLUDED
主函数
#include <stdio.h>
#include <stdlib.h>
#include"StackList.h"
int main()
{
int i,x=0,val;
Stack *head;//节点头部初始化
head=(Stack *)malloc(sizeof(Stack));
head->next=NULL;
scanf("%d",&x);
for(i=0;i<x;i++){
scanf("%d",&val);
Push(head,val);
}
for(i=0;i<x;i++){
printf("%d ",Pop(head));
}
return 0;
}
StackList.c文件
#include <stdio.h>
#include <stdlib.h>
#include"StackList.h"
Stack *Push(Stack *head,ElemType val)
{
Stack *s;//声明节点指针
s=(Stack *)malloc(sizeof(Stack));//申请内存空间
s->data=val;
s->next=head->next;
head->next=s;
return head;
}
ElemType Pop(Stack *head)
{
Stack *fre;
ElemType TOPre;
if(head->next==NULL)return NULL;
else
{
fre=head->next;
head->next=fre->next;
TOPre=fre->data;
free(fre);
return TOPre;
}
}