#include<stdio.h>
//随机存储的形式
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}Stack;
Stack *Createstack(void){
Stack *s;
s=malloc(sizeof(Stack));
s->next=NULL;
return s;
}
// Stack *create_l(void){
// Stack *top=NULL;
// Stack *pnew=NULL;
// while (1)
// {
// int a;
// scanf("%d",&a);
// if (a==0)
// {
// break;
// }
// pnew=malloc(sizeof(Stack));
// pnew->data=a;
// pnew->next=NULL;
// if (top==NULL)
// {
// top=pnew;
// }
// else
// {
// top->next=pnew;
// top=pnew;
// }
// }
// return top;
// }
int IsEmpty(Stack *s){
if (s==NULL)
{
return 1;
}
else
{
printf("链表不为空");
return 0;
}
}
void Push(int a,Stack *s){
Stack *node;
node=malloc(sizeof(Stack));
node->data=a;
node->next=s->next;//栈顶指针的更新
s->next=node;//栈顶指针指向的位置,s->next指向了新结点
}
/*删除并返回堆栈s的栈顶元素*/
Stack *Pop(Stack *S)
{
struct SNode *FirstCell;
int TopElem;
if( IsEmpty( S ) )
{
printf("堆栈空");
return NULL;
}
else
{
FirstCel1= S->Next;
S->Next = FirstCel1->Next;
TopElem = FirstCel1->Element;
free(FirstCell);
return TopElem;
}
}
//打印出链表的内容
void stack_print(Stack *s) {
Stack *p = s->next; // 从栈顶开始,即 s 的下一个节点
while (p != NULL) { // 当 p 不为 NULL 时继续循环
printf("%d ", p->data); // 打印节点数据
p = p->next; // 移动到下一个节点
}
printf("\n"); // 打印换行符以分隔输出
}
int main(){
Stack *m=Createstack();
for (int i = 0; i < 10; i++)
{
Push(i,m);
}
int a=IsEmpty(m);
printf("%d\n",a);
stack_print(m);
}
以上是我的思路