栈的链式表示

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
typedef struct Stack{
    int data;
    struct Stack *next;
}S1,*S2;
void Print(S2 s)
{
    S2 tmp = s;
    while(tmp->next)
    {
        tmp = tmp->next;
        printf("%d ",tmp->data);
    }
    printf("\n");
}
void Init(S2 *s,int n)
{
    (*s) = (S2)malloc(sizeof (S1));
    (*s)->next = NULL;
    S2 tmp = (*s);
    int i;
    for(i = 1;i<=n;++i)
    {
        S2 p = (S2) malloc(sizeof (S1));
        int t;
        scanf("%d", &t);
        p->data = t;
        p->next = tmp->next;
        tmp->next = p;
        tmp =  (*s);
    }
    Print(*s);
}
void pop(S2 s) 
{
    //printf("%d",s->data);
    S2 tmp = s->next;
    s->next = tmp->next;
//    printf("%d\n",s->next->data); 
//    free(tmp);
    Print(s); 
}
void Push(S2 s,int e)
{
    S2 tmp =  (S2)malloc(sizeof (S1));
    tmp->data = e;
    tmp->next = s->next;
    s->next = tmp;
    Print(s);
        
}

void Getlength(S2 s)
{
    S2 tmp = s;
    int sum = 0;
    while(tmp->next)
    {
        tmp = tmp->next;
        sum++;
    }
    printf("%d\n",sum);
}

S2 s;
void solve(){
    printf("1.Create a Linklist Stake\n");
    printf("2.pop the top element\n");
    printf("3.push the element\n");
    printf("4.Getlength\n");
} 
int main(){
    int pos = 1;
    solve();
    while(pos)
    {
        scanf("%d", &pos);
        if(pos ==  1)
        {
                printf("1.Create a Linklist Stake\n");
                printf("Please input the length\n");
                int n;
                scanf("%d",&n);
                Init(&s,n);
        }
        if(pos == 2)
        {
                printf("2.pop the top element\n");
                pop(s);
        }
        if(pos == 3)
        {
                printf("3.push the element\n");
                printf("Please input your number\n");
                int e;
                scanf("%d", &e);
                Push(s,e);
        }
        if(pos == 4)
        {
                //printf("4.Getlength\n");
                Getlength(s);
        }
         solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值