顺序栈:
#include<stdio.h>
#include<stdlib.h>
#define Stsck_Size 50
typedef int StackElementType;
typedef struct
{
StackElementType elem[Stsck_Size];
int top;
}SeqStack,*LinkStackptr;
void InitStack(SeqStack &S)//初始化
{
S.top = -1;
}
void menu()
{
printf(
" 线性表子系统 \n"
"**********************************\n"
"* 1-----进 栈 *\n"
"* 2-----出 栈 *\n"
"* 3-----显 示 *\n"
"* 4-----数制转换 *\n"
"* 0-----退 出 *\n"
"**********************************\n"
"请输入菜单数(1-7)\n"
);
}
void Push(SeqStack &S,int x)//进栈
{
if (S.top >= Stsck_Size)
printf("栈已满\n");
S.top++;
S.elem[S.top] = x;
}
int Pop(SeqStack &S, int &B) {//出栈&B
if (S.top == -1)
return 0;
B = S.elem[S.top];
S.top--;
return 1;
}
int ShowStack(SeqStack& S)//显示
{
int z=1,c=S.top;//c结束标志
int* p = &S.elem[S.top];//P指向top输出时防止top移动
if (S.top <= -1) {
printf("栈为空\n");
return 0;
}
while (z) {
printf("%d", *p);
p--;
c--;
if (c == -1)
break;
}
return 1;
}
void Change(int ten, SeqStack& S)//进制转换
{
int n, m = ten;
while (m != 0) {
n = m % 2;
m = m / 2;
Push(S, n);
}
printf("转换后的2进制数\n");
ShowStack(S);
}
int main()
{
menu();
SeqStack S;
InitStack(S);
int z=1,c,d=1,x,B;
int ten;
while (z) {
scanf("%d", &c);
getchar();
switch (c) {
case 1:printf("输入整数以0结束\n");
while (d) {
scanf("%d", &x);
Push(S, x);
if (x == 0) {
printf("请重新选择\n"); break;
}
}break;
case 2:Pop(S, B); break;
case 3:ShowStack(S); break;
case 4:printf("10进制转化为2进制\n输入一个10进制数\n");
scanf("%d", &ten);
Change(ten, S);
break;
case 0:z = 0; break;
default:printf("请重新输入:(0-5)"); break;
}
}
return 0;
}
链栈:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#define Stsck_Size 50
typedef struct StackNode
{
struct StackNode* next;
int data;
}StackNode,*LinkStackPtr;
typedef struct
{
LinkStackPtr top;
}LinkStack;
void InitStack(LinkStack &S)
{
S.top=(LinkStackPtr)malloc(sizeof (StackNode));
S.top->next = NULL;
}
int Push(LinkStack &S, int d)
{
LinkStackPtr temp;
temp= (LinkStackPtr)malloc(sizeof(StackNode));
if (temp ==NULL)
return 0;
temp->data = d;
temp->next = S.top->next;
S.top->next = temp;
return 1;
}
int Pop(LinkStack& S, int &x)
{
LinkStackPtr temp;
temp = S.top->next;
if (temp == NULL)
return 0;
S.top->next = temp->next;
x = temp->data;
free(temp);
printf("出栈完毕\n");
return 1;
}
void Show(LinkStack& S)
{
LinkStackPtr p = S.top;
if (S.top->next == NULL)
printf("栈为空\n");
else
{
while (p->next != NULL)
{
printf("%d ",p->next->data);//top顶没有存数
p = p->next;
}
}
}
void Change(int ten, LinkStack& S)//进制转换
{
InitStack(S);
int n, m = ten;
while (m != 0) {
n = m % 2;
m = m / 2;
Push(S, n);
}
printf("转换后的2进制数\n");
Show(S);
}
void menu()
{
printf(
" 线性表子系统 \n"
"**********************************\n"
"* 1-----进 栈 *\n"
"* 2-----出 栈 *\n"
"* 3-----显 示 *\n"
"* 4-----数制转换 *\n"
"* 0-----退 出 *\n"
"**********************************\n"
"请输入菜单数(1-7)\n"
);
}
int main()
{
LinkStack S;
InitStack(S);
menu();
int choice, d, x, z = 1, ten;
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
printf("输入进栈的数\n");
while (z) {
scanf("%d", &d);
if (d != 0)
Push(S, d);
else {
printf("进栈完毕\n");
break;
}
} break;
case 2:
Pop(S, x);
break;
case 3:
Show(S);
break;
case 4:
printf("输入一个10进制数\n");
scanf("%d", &ten);
Change(ten, S);
break;
case 0:exit(1); break;
default:printf("请重新输入\n");
}
printf("\n请选择:(0-5)\n");
}
return 0;
}