C/C++链栈操作源代码
链栈结构
1.头文件与宏定义
#include <stdio.h>
#include <stdlib.h>
#define STACKMAX 100
2.链栈结构
//链栈的存储结构
typedef struct stacknode
{
int data;
struct stacknode *next;
}Stacknode;
//指向栈的指针
typedef struct
{
Stacknode *top;
}linkStack;
3.进栈
//进栈
void Push(linkStack &s,int x)
{
Stacknode *p;
p = new Stacknode; //开辟空间
p->data = x; //构造结点
p->next = s.top; //入栈
s.top = p; //修改栈顶指针
}
4.出栈
//出栈操作
void Pop(linkStack &s,int &x) //特别重要,这里的x要给它分配空间,要不然每次代码运行会释放掉,就丢失了想要的数据,这个地方我先前没注意,代码一直出错。
{
Stacknode * p;
if(s.top != NULL) //栈不为空
{
p = s.top;
x = p->data;
s.top = p->next;
delete p;
}
else
printf("\n\t\t栈为空!"); //栈空
}
5.显示栈内容
//显示栈内容
void show_stack(linkStack s)
{
Stacknode *p;
p = s.top;
if(p == NULL)
printf("\n\t\t栈为空!");
else
{
printf("\n\t\t栈元素为:");
while(p != NULL)
{
printf("%6d",p->data);
p = p->next;
}
printf("\n");
}
}
6.进制转换
//十进制转换二进制
void change_two_ten(int n)
{
linkStack s;
int x;
s.top = NULL; //置栈空
do
{
x = n%2; //取余数
n = n/2; //取新的商
Push(s,x);
}while(n);
printf("\n\t\t转换后二进制数值为:");
while(s.top != NULL)
{
Pop(s,x);
printf("%d",x);
}
printf("\n");
}
7.将中缀表达式转换成后缀表达式(逆波兰式)
//求逆波兰式
void Suffix()
{
char str[STACKMAX]; //存储算术表达式
char stacks[STACKMAX]; //运算符号栈
char exp[STACKMAX];
char ch;
int sum,i,j,t,top = 0;
printf("\n\t\t输入算术表达式,以‘#’结束:\n\t\t");
fflush(stdin);
i = 0;
do
{
i++;
scanf("%c",&str[i]);
}while(str[i] != '#' && i != STACKMAX);
sum = i;
t = 1;
i = 1;
ch = str[i];
i++;
while(ch != '#')
{
switch(ch)
{
case '(':
top++;
stacks[top] = ch;
break;
case ')':
while(stacks[top] != '(')
{
exp[t++] = stacks[top--];
exp[t++] = ',';
}
top--;
break;
case '+':
case '-':
while(top != 0 && stacks[top] != '(')
{
exp[t++] = stacks[top--];
exp[t++] = ',';
}
stacks[++top] = ch;
break;
case '*':
case '/':
while(stacks[top] == '*' || stacks[top] == '/')
{
exp[t++] = stacks[top--];
exp[t++] = ',';
}
stacks[++top] = ch;
break;
case ' ':
break;
default:
while(ch >= '0'&&ch <= 'z') //输入必须是10以内整数或字母变量
{
exp[t++] = ch;
ch = str[i++];
}
i--;
exp[t++] = ',';
}
ch = str[i++];
}
while(top!=0)
{
exp[t++] = stacks[top--];
if(top != 0)
exp[t++] = ',';
}
printf("\n\t\t输入的中缀表达式为:");
for(j=1;j<sum;j++)
printf("%c",str[j]);
printf("\n\t\t输入的后缀表达式为:");
for(j=1;j<t;j++)
printf("%c",exp[j]);
printf("\n");
}
8.main函数
void main()
{
linkStack s;
int i = 1,j = 1,val,n;
char choice;
s.top = NULL;
while(1)
{
printf("\n");
printf("\n\t\t***************************************************");
printf("\n\t\t* 栈子系统 *");
printf("\n\t\t***************************************************");
printf("\n\t\t* 1------进 表 *");
printf("\n\t\t* 2------出 栈 *");
printf("\n\t\t* 3------显 示 *");
printf("\n\t\t* 4------数制转换 *");
printf("\n\t\t* 5------逆波兰式 *");
printf("\n\t\t* 0------退出程序 *");
printf("\n\t\t***************************************************");
printf("\n\t\t请选择菜单号:");
fflush(stdin);
choice = getchar();
switch(choice)
{
case '1':
while(1)
{
printf("\n\t\t键入一个整数并按回车:");
scanf("%d",&val);
if(val != 0)
Push(s,val);
else
break;
}
break;
case '2':
if(s.top != NULL)
{
Pop(s,val);
printf("\n\t\t出栈元素为:%6d\n",val);
}
else
printf("\n\t\t栈为空,无法出栈!");
break;
case '3':
show_stack(s);
break;
case '4':
printf("\n\t\t请输入一个十进制正整数:");
scanf("%d",&n);
change_two_ten(n);
break;
case '5':
Suffix();
break;
case 0:
exit(0);
default:
printf("\n\t\t输入错误!请重新输入!");
break;
}
}
}