栈子系统c语言,数据结构(栈子系统:c实现)
#include
#include
#define N sizeof(stacknode)//結(jié)點(diǎn)所占字節(jié)數(shù) N
//定義結(jié)構(gòu)體
typedef int datatype;
typedef struct stacknode
{
datatype data;
struct stacknode *next;
}stacknode;
//定義棧頂
typedef struct
{
stacknode *top;
int count;//計(jì)數(shù)用
}linkstack;
//進(jìn)棧,元素一一進(jìn)棧
void InsertStack(linkstack *s)
{
int x=0;
stacknode *p;
s->top=NULL;
s->count=0;
//printf("\n\t建立一個(gè)棧子系統(tǒng)");
p=(stacknode*)malloc(N);
printf("\n\t\t請(qǐng)逐個(gè)輸入數(shù)字,結(jié)束標(biāo)記位,做結(jié)束符的數(shù)字: 0 \n");
while(1)
{
/*printf("\t\t請(qǐng)輸入:");
fflush(stdin);
if(!scanf("%d",&x))
{
printf("輸入的元素種類錯(cuò)誤!!!\n");
continue;
}
else if(x=='#') break;
else
{
p->data=x;
p->next=s->top;
s->top=p;
s->count++;
}
*/
printf("\t\t請(qǐng)輸入:");
p=(stacknode*)malloc(N);
fflush(stdin);
if(!scanf("%d",&x))
{
printf("\t\t\t輸入的“元素“種類錯(cuò)誤!!!\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棧是一個(gè)空棧!!!!\n");
else
{
printf("\t棧中各個(gè)元素為:\t");
while(i!=0)
{
printf("%8d",p->data);
p=p->next;
//s->top=s->top->next;
i--;
}
}
printf("\n");
}
//求棧中元素的個(gè)數(shù)
void LengthStack(linkstack *s)
{
printf("\t棧中元素的個(gè)數(shù)為:\t");
printf("%d",s->count);
}
//出棧,棧中各個(gè)元素的出棧
void PutStack(linkstack *s)
{
//int x;
stacknode *p;
//linkstack *i;
if(s->count==0)
{
printf("\t\t棧是一個(gè)空棧!!!!");
//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--;
}
}
//數(shù)制轉(zhuǎn)換,十進(jìn)制轉(zhuǎn)換為二進(jìn)制
void ShiftStack(linkstack *s)
{
int z=0;
int m=0;
stacknode *p;
printf("請(qǐng)輸入所要轉(zhuǎn)換的 ”數(shù)字“ 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轉(zhuǎn)化后的二進(jìn)制為\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棧子系統(tǒng)\n");
printf(" \t\t***************************************************\n");
printf(" \t\t* 1------進(jìn) 棧 *\n");
printf(" \t\t* 2------出 棧 *\n");
printf(" \t\t* 3------顯示棧中元素 *\n");
printf(" \t\t* 4------求棧中元素個(gè)數(shù) *\n");
printf(" \t\t* 5------數(shù)制轉(zhuǎn)換 *\n");
printf(" \t\t* 0------返 回 *\n");
printf(" \t\t***************************************************\n");
printf(" 請(qǐng)輸入(0-5)選項(xiàng):\n");
printf("\n請(qǐng)輸入所要達(dá)到第幾號(hào)功能:\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("!!!!!輸入有誤,請(qǐng)重新輸入!!!!!\n");
}
}
}
總結(jié)
以上是生活随笔為你收集整理的栈子系统c语言,数据结构(栈子系统:c实现)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 自定义多边形,Andro
- 下一篇: java信息管理系统总结_java实现科