实现栈的基本操作,
该程序的功能是实现顺序栈的定义和操作。该程序包括定义的栈结构类型以及对每一种栈操作的具体的函数定义和主函数。
利用栈的基本操作解决实际应用
例如进制转换,阶乘,行编辑,汉诺塔等。
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef int SElemType;
typedef int Status;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW 0
int Count = 0;//全局变量,汉诺塔计数器
//author: 小柳学渣
void move(char x, int n, char z);//汉诺塔移动
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
/*创建*/
Status InitStack(SqStack &S)
{
S.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!S.base)
exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
/*栈顶元素*/
Status GetTop(SqStack S, SElemType &e)
{
if (S.top == S.base)
return ERROR;
e = *(S.top - 1);
return OK;
}
/*插入*/
Status Push(SqStack &S, SElemType e)
{
if (S.top - S.base >= S.stacksize)
{
S.base = (SElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
if (!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}
/*删除*/
Status Pop(SqStack &S, SElemType &e)
{
if (S.top == S.base)
return ERROR;
e = *--S.top;
return OK;
}
/*判断是否为空*/
Status StackEmpty(SqStack &S)
{
if (S.top != S.base)
return ERROR;
return OK;
}
/*重置为空栈*/
Status ClearStack(SqStack &S)
{
S.top = S.base;
return OK;
}
/*销毁*/
Status DestroyStack(SqStack &S)
{
free(S.base);
S.base = NULL;
S.top = NULL;
S.stacksize = 0;
return OK;
}
//author: 小柳学渣
/*十进制转R进制*/
void conversion()
{
int N,e,R;
SqStack S;
InitStack(S);
printf("请输入一个十进制数:");
scanf_s("%d", &N);
printf("请输入要转化成的进制:");
scanf_s("%d", &R);
while (N)
{
Push(S, N % R);
N = N / R;
}
printf("转换成的%d进制数为:",R);
while (!StackEmpty(S))
{
Pop(S, e);
printf("%d", e);
}
printf("\n");
}
/*行编辑*/
void LineEdit()
{
char ch;
SElemType c,e;
SqStack S;
InitStack(S);
printf("请输入一行字符(#:退格;@:清行;$:结束):\n");
ch = getchar();
while (ch != '$')
{
while (ch != '$' && ch != '\n')
{
switch (ch)
{
case '#': Pop(S, c);break;
case '@': ClearStack(S);break;
default: Push(S, ch);break;
}
ch = getchar();
}
for(SElemType*i=S.base;i!=S.top;i++)
printf("%c", *i);//输出当前行字符
ClearStack(S);
printf("\n");
if (ch != '$')
{
printf("请输入一行字符(#:退格;@:清行;$:结束):\n");
ch = getchar();
}
}
DestroyStack(S);
}
//author: 小柳学渣
/*阶乘*/
int f(int n)
{
if (n == 1)
return 1;
else
return n*f(n - 1);
}
//author: 小柳学渣
/*汉诺塔*/
void hanoi(int n, char x, char y, char z)
{
if (n == 1)
move(x, 1, z);
else
{
hanoi(n - 1, x, z, y);
move(x, n, z);
hanoi(n - 1, y, x, z);
}
}
void move(char x, int n, char z)
{
printf("%3d、Move disk %d from %c to %c\n", ++Count, n, x, z);
}
//author: 小柳学渣
void main()
{
char choice;
int n;
char x='x', y = 'y', z = 'z';
while (1)
{
system("cls");
printf("1、十进制转八进制\n");
printf("2、行编辑\n");
printf("3、阶乘\n");
printf("4、汉诺塔\n");
printf("0、退出\n");
choice = _getch();
switch (choice)
{
case'1':
system("cls");
conversion();
system("pause");
break;
case'2':
system("cls");
LineEdit();
break;
case'3':
system("cls");
printf("请输入一个数:");
scanf_s("%d", &n);
printf("阶乘为%d\n", f(n));
system("pause");
break;
case'4':
system("cls");
printf("请输入盘数:");
scanf_s("%d", &n);
hanoi(n, x, y, z);
Count = 0;
system("pause");
break;
case'0':
exit(0);
default:
system("cls");
printf("输入错误,请重新输入!\n");
system("pause");
}
}
}
栈的基本操作:初始化栈、判栈为空、出栈、入栈等运算;
顺序栈的定义和基本操作;
利用栈的基本操作解决实际应用,例如进制转换,阶乘,行编辑,汉诺塔等。