栈子系统c语言,数据结构(栈子系统:c实现)

#include

#include

#define N sizeof(stacknode)//结点所占字节数 N

//定义结构体

typedef int datatype;

typedef struct stacknode

{

datatype data;

struct stacknode *next;

}stacknode;

//定义栈顶

typedef struct

{

stacknode *top;

int count;//计数用

}linkstack;

//进栈,元素一一进栈

void InsertStack(linkstack *s)

{

int x=0;

stacknode *p;

s->top=NULL;

s->count=0;

//printf("\n\t建立一个栈子系统");

p=(stacknode*)malloc(N);

printf("\n\t\t请逐个输入数字,结束标记位,做结束符的数字: 0 \n");

while(1)

{

/*printf("\t\t请输入:");

fflush(stdin);

if(!scanf("%d",&x))

{

printf("输入的元素种类错误!!!\n");

continue;

}

else if(x=='#') break;

else

{

p->data=x;

p->next=s->top;

s->top=p;

s->count++;

}

*/

printf("\t\t请输入:");

p=(stacknode*)malloc(N);

fflush(stdin);

if(!scanf("%d",&x))

{

printf("\t\t\t输入的“元素“种类错误!!!\n");

continue;

}

else if(x==0)

break;

else

{

p->data=x;

p->next=s->top;

s->top=p;

s->count++;

}

}

printf("\n");

}

//显示栈中元素

void ShowStack(linkstack *s)

{

stacknode *p;

int i=0;

p=s->top;

i=s->count;

//p->data=s->top->data;

//if(p->next==NULL)

if(i==0)

printf("\t\t栈是一个空栈!!!!\n");

else

{

printf("\t栈中各个元素为:\t");

while(i!=0)

{

printf("%8d",p->data);

p=p->next;

//s->top=s->top->next;

i--;

}

}

printf("\n");

}

//求栈中元素的个数

void LengthStack(linkstack *s)

{

printf("\t栈中元素的个数为:\t");

printf("%d",s->count);

}

//出栈,栈中各个元素的出栈

void PutStack(linkstack *s)

{

//int x;

stacknode *p;

//linkstack *i;

if(s->count==0)

{

printf("\t\t栈是一个空栈!!!!");

//return 0;

}

else

{

/*

p=s->top;

x=p->data;

s->top=p->next;

free(p);

//s->count-=1;

*/

p=s->top;

s->top=s->top->next;

printf("\n\t\t\t\t出栈元素为:%d\n",p->data);

free(p);

s->count--;

}

}

//数制转换,十进制转换为二进制

void ShiftStack(linkstack *s)

{

int z=0;

int m=0;

stacknode *p;

printf("请输入所要转换的 ”数字“ Z:\t");

scanf("%d",&z);

s->top=NULL;

while(z)

{

m=z%2;

z=z/2;

p=(stacknode*)malloc(N);

p->next=s->top;

s->top=p;

s->top->data=m;

}

printf("\n\t转化后的二进制为\t");

while(s->top)

{

p=s->top;

printf("%d",p->data);

s->top=s->top->next;

free(p);

}

printf("\n");

}

int main()

{

int a;

linkstack s;

s.count=0;

//linkstack *s;

while(1)

{

printf(" \n\t\t\t\t\t\t栈子系统\n");

printf(" \t\t***************************************************\n");

printf(" \t\t* 1------进 栈 *\n");

printf(" \t\t* 2------出 栈 *\n");

printf(" \t\t* 3------显示栈中元素 *\n");

printf(" \t\t* 4------求栈中元素个数 *\n");

printf(" \t\t* 5------数制转换 *\n");

printf(" \t\t* 0------返 回 *\n");

printf(" \t\t***************************************************\n");

printf(" 请输入(0-5)选项:\n");

printf("\n请输入所要达到第几号功能:\t");

fflush(stdin);

scanf("%d",&a);

if(a == 1)

InsertStack(&s);

else if(a == 2)

PutStack(&s);

else if(a == 3)

ShowStack(&s);

else if(a == 4)

LengthStack(&s);

else if(a == 5)

ShiftStack(&s);

else if(a == 0)

return 0;

else{

printf("!!!!!输入有误,请重新输入!!!!!\n");

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值