=NULL)
{
j++;
p=p->next;
}
if(p==NULL)//未找到第i-1个结点
return0;
else//找到第i-1个结点*p
{
s=(link*)malloc(sizeof(link));//创建新结点*s
s->data=pos;
s->next=p->next;//将*s插入到*p之后
p->next=s;
return1;
}
}intGetElem(link*top,inti)
{
intj=0;
link*p;
p=top;
while(p->next&&j
{
p=p->next;
j++;
}
if(i==j)
printf("该位置的元素为%d",p->data);
returnOK;
}
intmenu_select_list()
{
intchoice;
printf("*****************************************\n");
printf("*1------------------顺序表*\n");
printf("*2------------------链表*\n");
printf("*3------------------退出*\n");
printf("*****************************************\n");
printf("请输入选择:
(1-3)");
scanf("%d",&choice);
while(choice<1||choice>3)
{
printf("输入选择有误,请重新选择(1-3):
\n");
scanf("%d",&choice);
}
returnchoice;
}
intmenu_select_2_sqlist()
{
intchoice;
printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("\t*1----------建立顺序表*\n");
printf("\t*2----------显示*\n");
printf("\t*3----------插入*\n");
printf("\t*4----------删除*\n");
printf("\t*5----------查找*\n");
printf("\t*6----------求线性表的长度*\n");
printf("\t*7----------返回*\n");
printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("请输入选择(1-7):
");
scanf("%d",&choice);
while(choice<1||choice>7)
{
printf("输入选择有误,请重新选择(1-7):
");
scanf("%d",&choice);
}
returnchoice;
}
intmenu_select_2_list()
{
intchoice;
printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("\t*1----------前插建立链表*\n");
printf("\t*2----------显示*\n");
printf("\t*3----------后插建立链表*\n");
printf("\t*4----------插入*\n");
printf("\t*5----------删除*\n");
printf("\t*6----------查找*\n");
printf("\t*7----------求线性表的长度*\n");
printf("\t*8----------返回*\n");
printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("请输入选择(1-8):
");
scanf("%d",&choice);
while(choice<1||choice>8)
{
printf("输入选择有误,请重新选择(1-8):
\n");
scanf("%d",&choice);
}
returnchoice;
}
intshunxvbiao()
{
SqListL;inti,e;
linktop,p;
intpos,choice1=1,choice2;
while(choice1==1||choice1==2)
{
choice1=menu_select_list();
switch(choice1){
case1:
choice2=menu_select_2_sqlist();
while(choice2!
=7)
{
switch(choice2){
case1:
InitList_Sq(&L);system("cls");break;
case2:
printlist_sq(L);getch();system("cls");break;
case3:
printf("请输入要插入的位置:
");scanf("%d",&i);printf("请输入要插入的元素:
");
scanf("%d",&e);
ListInsert_Sq(&L,i,e);
printf("插入后的线性表:
");
printlist_sq(L);
getch();
system("cls");break;
case4:
printf("请输入被删除的位置:
");
scanf("%d",&i);
ListDelete_Sq(&L,i,e);
printf("已成功删除!
");
getch();
system("cls");
break;
case5:
GetElem_Sq(&L,i,&e);getch();system("cls");
break;
case6:
ListLength_Sq(&L);
getch();
system("cls");
break;
case7:
break;system("cls");
default:
printf("\t\t\t输入有误,请重新输入:
\n");break;}
choice2=menu_select_2_sqlist();}break;
case2:
choice2=menu_select_2_list();
while(choice2!
=8)
{switch(choice2){
case1:
add_top(&top);
printf("建立好的链表为:
");
arrive_end(&top);
getch();
system("cls");
break;
case2:
printf("建立好的链表为:
");
arrive_end(&top);
getch();
system("cls");
break;
case3:
add_bottom(&top);
arrive_end(&top);
getch();
system("cls");
break;
case4:
printf("请输入插入位置:
");
scanf("%d",&i);
printf("请输入要插入的元素:
");
scanf("%d",&pos);
insert(&top,i,pos);
arrive_end(&top);
getch();
system("cls");
break;
case5:
printf("请输入删除位置:
");
scanf("%d",&pos);
delete(&top,pos);printf("删除后的链表为:
");
arrive_end(&top);
getch();
system("cls");
break;
case6:
printf("请输入查找位置:
");
scanf("%d",&i);
GetElem(&top,i);
getch();
system("cls");
break;
case7:
pos=length(&top);
printf("链表长度为:
%d\n",pos);
getch();
system("cls");
break;
case8:
break;
default:
;break;}
choice2=menu_select_2_list();}
break;}}
system("pause");
return0;
}
/*-------------------------栈---------------------*/
#include
#defineMAXSIZE16
typedefstruct{
intdata[MAXSIZE];
inttop;
intbase;
}seqstack;/*顺序栈的定义*/
voidInitStack(seqstack*);
intEmpty(seqstack*);
voidPush(seqstack*,int);
intPop(seqstack*);
intGetTop(seqstack*);
intOperate(int,char,int);
charProceed(char,char);
intIn(char);
intEvalExpres(void);
/*定义两个栈分别存放运算符和操作数*/
seqstackStackR,StackD;
/*主函数*/
intzhan()
{
intv;
charch;
printf("\t*****************用顺序栈实现整型算术表达式的求值**************\n");
v=EvalExpres();
printf("结果为:
%d",v);
/*以下为程序控制*/
printf("\n输入q为退出程序,输入回车则继续:
");
scanf("%c",&ch);
while(ch!
='q')
{
v=EvalExpres();
printf("结果为:
%d",v);
printf("\n输入q为退出程序,输入回车则继续:
");
scanf("%c",&ch);
}
system("cls");
return0;
}
voidInitStack(seqstack*s)
{s->top=0;
s->base=0;
}/*初始化栈*/
intEmpty(seqstack*s)
{if(s->top==s->base)
return1;
else
return0;
}/*判断栈是否为空*/
voidPush(seqstack*s,intx)
{
if(s->top==MAXSIZE)
{printf("OVERFLOW!
\n");
exit(0);
}
else
{s->data[s->top]=x;
s->top++;
}
}/*进栈*/
intPop(seqstack*s)
{inte;
if(Empty(s))
{printf("Underflow!
\n");
return0;
}/*下溢*/
else
{s->top--;
e=s->data[s->top];
returne;
}
}/*出栈*/
intGetTop(seqstack*s)/*取栈顶元素*/
{
if(Empty(s))
{printf("Underflow!
\n");
return0;
}
else
returns->data[s->top-1];
}
intEvalExpres(void)/*表达式求解函数*/
{
inta,b,i=0,s=0;
charc[80],r;
InitStack(&StackR);
Push(&StackR,'#');
InitStack(&StackD);
fflush(stdin);
printf("请输入表达式并以‘#’结束:
\n");
gets(c);
while(c[i]!
='#'||GetTop(&StackR)!
='#')
{
if(!
In(c[i]))/*判断读入的字符不是运算符是则进栈*/
{if(c[i]>='0'&&c[i]<='9')
{
s+=c[i]-'0';
while(!
In(c[++i]))/*此处实现的功能为当输入的数字为多位数时*/
{s*=10;
s+=c[i]-'0';
}
Push(&StackD,s+'0');
s=0;
}
else
{
printf("你输入的表达式有误!
\n");
return0;
}
}
else
switch(Proceed(GetTop(&StackR),c[i]))/*此函数用来比较读取的运算符和栈顶运算符的优先级*/
{
case'<':
/*栈顶的元素优先级高*/
Push(&StackR,c[i]);
i++;
break;
case'=':
/*遇到匹配的小括号时则去掉他*/
Pop(&StackR);
i++;
break;
case'>':
/*栈顶的优先级低则出栈并将结果写入栈内*/
r=Pop(&StackR);
a=Pop(&StackD)-'0';
b=Pop(&StackD)-'0';
Push(&StackD,Operate(a,r,b));
break;
}
}
return(GetTop(&StackD)-'0');/*返回运算结果*/
}
intIn(charc)/