#include <stdlib.h>
#include <string.h>
struct CHAP5
{
char * pName
bool Gender
int Age
struct CHAP5 * next
}
struct Node_int{
int i
struct Node_int * next
}
/*由于两种栈在结构的粗宴高度相似性,用宏代替重复的语句进行栈 *** 作函数声明*/
#define DECLARE_STACK_PUSH(function_name, node_type) void function_name(node_type ** top, node_type * data) {\
node_type * x = (node_type *)malloc(sizeof(node_type))\
memcpy(x, data, sizeof(node_type))\
x->next = *top*top = xreturn }
#define DECLARE_STACK_POP(function_name, node_type) int function_name(node_type **top, node_type * data) {\
node_type * x = *topif (!x) return 0\
*top = x->nextmemcpy(data, x, sizeof(node_type))\
data->next = 0free(x)return 1}
/*这里才是真正声明四个栈 *** 作函数*/
DECLARE_STACK_PUSH(stack_int_push, struct Node_int)
DECLARE_STACK_POP (stack_int_pop, struct Node_int)
DECLARE_STACK_PUSH(stack_chap5_push,struct CHAP5)
DECLARE_STACK_POP (stack_chap5_pop, struct CHAP5)
void test_stack_int(void)
{
int x = 0struct Node_int y, * top = 0
do {
scanf("%d", &x)
y.i = x
stack_int_push(&top, &y)
}while(x)
printf("退栈结果:\n")/*相当于栈清空 *** 作*/
do {
x = stack_int_pop(&top, &y)
if (x) printf("%d\t", y.i )
}while(x)
printf("\n")
}
void test_stack_chap5(void)
{
int x = 0struct CHAP5 y, * top = 0
printf("请输入几个姓名、年龄、性别用于检测堆栈,年龄输入0表示结束\n")
do {
y.pName = (char *)malloc(64)
scanf("%s %d %d", y.pName , &(y.Age), &x)
y.Gender = x==0?true:false
stack_chap5_push(&top, &y)
}while(y.Age )
printf("退栈结果:\n姓名\t年龄\t性别\n")/*相当于栈清空岩信银 *** 作*/
do {
x = stack_chap5_pop(&top, &y)
if (x) printf("%s\t%d\t%s\坦毕n", y.pName , y.Age, y.Gender?"男":"女")
}while(x)
}
int main(void)
{
test_stack_int()
test_stack_chap5()
system("pause")
return 0
}
//该销仔基程序简单并可正确运行,希望kutpbpb的回答能对你有所帮助!#include<stdio.h>
#define N 100
typedef struct
{
int value[N]
int base
int top
}Sta
void print()
{
printf("\n菜单:")
printf("\n1.入栈:")
printf("\n2.出栈:")
printf("\n3.退出:")
}
void printS(Sta S)
{
printf("亏谨\n请输出栈中元素:")
for(int i=S.topi!=S.basei--)
printf("%d ",S.value[i-1])
}
void pushS(Sta&S,int e)
{
if(S.top==N)
printf("\n栈满"戚键)
else
S.value[S.top++]=e
}
void popS(Sta&S,int&e)
{
if(S.top==S.base)
printf("\n栈空")
else
{
e=S.value[--S.top]
printf("\n请输出出栈元素: %d",e)
}
}
void main()
{
Sta S
int e,choose
S.base=S.top=0
do{
print()
printf("\n请输入你的选项:")
scanf("%d",&choose)
switch(choose)
{
case 1:
printf("\n请输入入栈元素:")
scanf("%d",&e)
pushS(S,e)
printS(S)
break
case 2:
popS(S,e)
printS(S)
break
case 3:
default:
break
}
if(choose==3)
break
}while(1)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)