- 3
- 0
- 约6.36千字
- 约 25页
- 2017-05-03 发布于四川
- 举报
习题3.15
const int StackSize=500;
typedef struct{????SElemtype *base;???? int top0, top1;?????}TwoStack; //双向栈类型
Status InitStack(TwoStack tws){??tws.base=new SElemtype[StackSize];????tws.top0=-1;?? tws.top1=StackSize;??return OK;};Status push(TwoStack tws,int i,SElemtype x)
//x入栈,i=0表示低端栈,i=1表示高端栈
{??if(tws.top0+1==tws.top1)
return OVERFLOW; //栈满if(i==0) tws.base[++tws.top0]=x;??else if(i==1) tws.base[--tws.top1]=x;?? else return ERROR;??return OK;
};Status pop(TwoStack tws,int i,SElemtype x)
//x出栈,i=0表示低端栈,i=1表示高端栈
{??if(i==0)
{??if(tws.top0==-1) return OVERFLOW;????x=tws.base[tws.top0--]; }
?else if(i==1)? ?{?if(tws.top1==StackSize) return OVERFLOW;???? x=tws.base[tws.top1++]; ?}?? else return ERROR;
?return OK;
};习题3.18
Status Ex3_18(char *str)//判别表达式中的小括号是否匹配
{ count=0;for(p=str; p ;p++){??if(*p==‘(’) count++;????else if(*p==‘)’) count--;????if (count0) return ERROR;//避免…)..(…假匹配?}??if(count) return ERROR; //注意括号不匹配的两种情况??return OK;
};习题3.19
Status Ex3_19(SqList str)//判别表达式中三种括号是否匹配
{??InitStack(S);
for(i=0;istr.length;i++){??if(str.elem[i]==(||str.elem[i]==[||str.elem[i]==‘{’)
Push(S, str.elem[i]);????else if(str.elem[i]==‘)’||str.elem[i]==‘]’||str.elem[i]==‘}’)????{?if(StackEmpty(S)) return ERROR;?????? Pop(S,c);?????? if(str.elem[i]==‘)’c!=‘(’) return ERROR;?????? if(str.elem[i]==‘]’c!=‘[’) return ERROR;?????? if(str.elem[i]==‘}’c!=‘{’) return ERROR; ????}//else if?}//for?if(!StackEmpty(S)) return ERROR;??return OK;
} ;习题3.28
void InitCiQueue(CiQueue Q)
//初始化循环链表表示的队列Q
{?? Q=new CiLNode;?Q-next=Q; }
void EnCiQueue(CiQueue Q,QElemType e)
//把元素e插入循环链表表示的队列Q,Q指向队尾元素,
//Q-next-next指向队头元素
{ ??p=new CiLNode;??p-data=e;??p-next=Q-next; //直接把p加在Q的后面??Q-next=p;??Q=p;??//修改尾指针
};Status DeCiQueue(CiQueue Q,QElemType e)
//从循环链表表示的队列Q头部删除元素e
{ if(Q==Q-next) return ERROR; //队列已空?r=Q-next; p=r-next;?e=p-data;?r-next=p-next;?if(p==Q)Q=r;
delete
原创力文档

文档评论(0)