要求编写功能函数实现堆栈的定义,分别基于顺序栈和链栈:按用户要求输入元素,编写进栈函数push();编写出栈函数pop();编写判断栈满和栈空的函数 ;遍历栈,并输出元素;输入数据实现十进制到二进制的转换。c语言实现完整代码
时间: 2024-05-19 14:13:47 浏览: 212
//使用顺序栈实现
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int top;
} SqStack;
void InitStack(SqStack *s) //初始化栈
{
s->top = -1;
}
int IsEmpty(SqStack s) //判断栈是否为空
{
return (s.top == -1);
}
int IsFull(SqStack s) //判断栈是否已满
{
return (s.top == MAXSIZE-1);
}
int Push(SqStack *s, int e) //进栈操作
{
if (IsFull(*s))
return 0;
s->data[++s->top] = e;
return 1;
}
int Pop(SqStack *s, int *e) //出栈操作
{
if (IsEmpty(*s))
return 0;
*e = s->data[s->top--];
return 1;
}
void TraverseStack(SqStack s) //遍历栈并输出元素
{
while (!IsEmpty(s))
{
int e;
Pop(&s, &e);
printf("%d ", e);
}
printf("\n");
}
void DecToBin() //十进制到二进制的转换
{
SqStack s;
InitStack(&s);
int n;
printf("请输入一个十进制整数:");
scanf("%d", &n);
while (n)
{
Push(&s, n%2);
n /= 2;
}
printf("转换后的二进制数为:");
TraverseStack(s);
}
int main()
{
SqStack s;
InitStack(&s);
int e, choice;
while (1)
{
printf("请选择要进行的操作:\n");
printf("1. 进栈\n");
printf("2. 出栈\n");
printf("3. 判断栈是否为空\n");
printf("4. 判断栈是否已满\n");
printf("5. 遍历栈并输出元素\n");
printf("6. 十进制到二进制的转换\n");
printf("0. 退出\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("请输入要进栈的元素:");
scanf("%d", &e);
if (Push(&s, e))
printf("进栈成功!\n");
else
printf("进栈失败,栈已满!\n");
break;
case 2:
if (Pop(&s, &e))
printf("出栈成功,出栈元素为%d!\n", e);
else
printf("出栈失败,栈已空!\n");
break;
case 3:
if (IsEmpty(s))
printf("栈为空!\n");
else
printf("栈不为空!\n");
break;
case 4:
if (IsFull(s))
printf("栈已满!\n");
else
printf("栈未满!\n");
break;
case 5:
printf("栈中的元素为:");
TraverseStack(s);
break;
case 6:
DecToBin();
break;
case 0:
exit(0);
default:
printf("输入有误,请重新输入!\n");
}
}
return 0;
}
//使用链栈实现
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode {
int data;
struct LNode *next;
} LinkNode, *LinkStack;
void InitStack(LinkStack *s) //初始化栈
{
*s = NULL;
}
int IsEmpty(LinkStack s) //判断栈是否为空
{
return (s == NULL);
}
int Push(LinkStack *s, int e) //进栈操作
{
LinkNode *p = (LinkNode*)malloc(sizeof(LinkNode));
p->data = e;
p->next = *s;
*s = p;
return 1;
}
int Pop(LinkStack *s, int *e) //出栈操作
{
if (IsEmpty(*s))
return 0;
LinkNode *p = *s;
*e = p->data;
*s = p->next;
free(p);
return 1;
}
void TraverseStack(LinkStack s) //遍历栈并输出元素
{
while (!IsEmpty(s))
{
int e;
Pop(&s, &e);
printf("%d ", e);
}
printf("\n");
}
void DecToBin() //十进制到二进制的转换
{
LinkStack s;
InitStack(&s);
int n;
printf("请输入一个十进制整数:");
scanf("%d", &n);
while (n)
{
Push(&s, n%2);
n /= 2;
}
printf("转换后的二进制数为:");
TraverseStack(s);
}
int main()
{
LinkStack s;
InitStack(&s);
int e, choice;
while (1)
{
printf("请选择要进行的操作:\n");
printf("1. 进栈\n");
printf("2. 出栈\n");
printf("3. 判断栈是否为空\n");
printf("4. 遍历栈并输出元素\n");
printf("5. 十进制到二进制的转换\n");
printf("0. 退出\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("请输入要进栈的元素:");
scanf("%d", &e);
if (Push(&s, e))
printf("进栈成功!\n");
else
printf("进栈失败!\n");
break;
case 2:
if (Pop(&s, &e))
printf("出栈成功,出栈元素为%d!\n", e);
else
printf("出栈失败,栈已空!\n");
break;
case 3:
if (IsEmpty(s))
printf("栈为空!\n");
else
printf("栈不为空!\n");
break;
case 4:
printf("栈中的元素为:");
TraverseStack(s);
break;
case 5:
DecToBin();
break;
case 0:
exit(0);
default:
printf("输入有误,请重新输入!\n");
}
}
return 0;
}
阅读全文