顺序栈与链栈

首先是顺序栈

#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 100
typedef struct{
    int data[MAXSIZE];//数值 
    int top;//顶 
}shunxu;
shunxu *chuangjian()//创建顺序表 
{
    shunxu*s;
    s=(shunxu*)malloc(sizeof(shunxu));
    s->top=-1;
}
int Empty(shunxu * s)
{
    if(s->top==-1) return 1;//返回1 表示空 
    else return 0;//返回0  表示表不空 
} 
/*入*/
int ru(shunxu *s,int n)//插入操作,执行成功返回1,不成功返回0 
{
    if(s->top==MAXSIZE-1) return 0; 
    else {
        s->top++;
        s->data[s->top]=n;
        return 1;
    }   
} 
int chu(shunxu *s,int *n)//出栈 
{
    if(Empty(s)) return 0;//表示空
        else{
            *n=s->data[s->top];//把最上面的数返回
            s->top--;//指向下方 
            return 1;//执行成功返回1 
        } 
}
int biaoding(shunxu *s){//返回表顶元素 
    if(Empty(s)) return 0;
        else return s->data[s->top];
}
void show(shunxu *s)
{
    for(int i=0;i<=s->top;i++)
    {
        printf("%d\n",s->data[i]);
    }
}
/*void show2(shunxu *s)
{
    for(int i=0;i<=s->top+1;i++)
    {
        printf("%d\n",s->data[i]);
    }
}*/
main()
{
    int n=0;
    int *m=(int *)malloc(sizeof(int));
    shunxu *s=chuangjian();
    printf("这是顺序栈,现在可以开始入栈\n");
    do
    {
        printf("请输入整形元素,以0为结束>");
        scanf("%d",&n);
        if(n==0) break;
        ru(s,n);
    }while(1);
    show(s);
    printf("现在可以出栈了\n");
    do{
        printf("如果出栈请按1,如果不想继续出栈请按2>");
        scanf("%d",&n);
        if(n==1){
            if(chu(s,m)) printf("出栈成功,%d被出栈\n",*m);
                else printf("空栈不可出!\n");
        }
        else break;
    }while(1);
    printf("您现在的表顶元素是>>>>%d\n",biaoding(s));
    show(s);
    //show2(s);
} 

然后是链栈

#include<stdio.h>
#include<malloc.h>
typedef struct node
{
    int data;
    struct node *next;
}Node,* Link;

//创建
Link chuangjian()
{
    return NULL;    
} 
//判断空
int Empty(Link top)
{
    if(top==NULL) return 1;
        else return 0;
} 
//入栈
Link ru(Link top,int data)
{
    Link p=(Link)malloc(sizeof(Node));
    p->data=data;
    p->next=top;
    top=p;
    return top;
} 
//出栈
Link chu(Link top,int *n)
{
    Node *s;
    if(top==NULL) return NULL;
    else
    {
        *n=top->data;
         s=top;
         top=top->next;
         free(s);
         return top;
    }
}
void show(Link top)
{
    while(top!=NULL)
    {
        printf("%d\n",top->data);
        top=top->next;
    }
} 
main()
{
    int n=0;
    int *s=(int *)malloc(sizeof(int));
    Link top;
    top=chuangjian();
    printf("这是链栈现在可以入栈了\n");
    do
    {
            printf("请输入一个整数,以0为结束>");
            scanf("%d",&n);
            if(n==0) break;
            top=ru(top,n);
    }while(1);
    show(top);
    printf("现在我们来出栈\n");
    do 
    {
        printf("如果你要出栈就输入1,否则输入2>");
        scanf("%d",&n);
        if(n==1)
        {
            top=chu(top,s);
            if(top==NULL) printf("已经空了,不能出栈啦\n"); 
                else printf("%d已被出栈\n",*s);
        }else break;
    }while(1);
    show(top);  
} 

下面是一个进制转换器

#include<stdio.h>
#include<malloc.h>
typedef struct node{
    int data;
    struct node*next;
} Node,*Link;
Link chuangjian()
{
    return NULL;
}

Link ru(int shu,Link top)
{
    Link p=(Link)malloc(sizeof(Node));
    p->data=shu;
    p->next=top;
    top=p;
    return top;
}
Link zhuan(int shu,int zhuan,Link top)
{
    int n=shu;
    int s=0;
    do
    {
        s=n-8*(n/zhuan);
        n=n/zhuan;
        top=ru(s,top);
        if(n==0) break;
    }while(1);
    return top;
}
void show(Link top)
{
    while(top!=NULL)
    {
        printf("%d\n",top->data);
        top=top->next;
    }
} 
main()
{
    int a,b=0;
    Link top=chuangjian();
     printf("请输入要被转换的十进制数:");
     scanf("%d",&a); 
     printf("请输入要转换的进制数:");
     scanf("%d",&b);
    top=zhuan(a,b,top);
    show(top);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值