您的位置:首页 > 理论基础 > 数据结构算法

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

2016-04-15 17:09 405 查看
#include<stdio.h>
#include<stdlib.h>
#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");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: