实现链式栈

#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);
}

以上是我的思路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想学习的朋友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值