}
while(q->coef) {
r->coef=-q->coef; r->exp=q->exp; q++;r++; }
}//Subtract_SqPoly 2.41
void QiuDao_LinkedPoly(LinkedPoly &L)//对有头结点循环链表结构存储的稀疏多项式L求导 {
p=L->next; if(!p->data.exp) {
L->next=p->next;p=p->next; //跳过常数项 }
while(p!=L) {
p->data.coef*=p->data.exp--;//对每一项求导 p=p->next; }
}//QiuDao_LinkedPoly 2.42
void Divide_LinkedPoly(LinkedPoly &L,&A,&B)//把循环链表存储的稀疏多项式L拆成只含奇次项的A和只含偶次项的B {
p=L->next;
A=(PolyNode*)malloc(sizeof(PolyNode)); B=(PolyNode*)malloc(sizeof(PolyNode)); pa=A;pb=B; while(p!=L) {
if(p->data.exp!=2*(p->data.exp/2)) {
pa->next=p;pa=p; } else {
pb->next=p;pb=p; }
p=p->next; }//while
pa->next=A;pb->next=B; }//Divide_LinkedPoly
第三章 栈和队列
3.15
#define StackSize 100; typedef struct { SElemType *base; SElemType *top0; SElemType *top1; }TSqStack;
Status InitStack(TSqStack tws){ //构造一个空的双向栈
if (!(tws.base=(SElemType
*)malloc(StackSize*sizeof(SElemType))))exit(OVERFLOW); tws.top0=tws.base;
tws.top1=tws.base[StackSize-1]; return OK; }
Status Push(TSqStack &tws, int i, SElemType x){ //将元素x压入双向栈tws的第i个栈中,i=0,1 if (tws.top0==tws.top1+1)exit(OVERFLOW); if (i==0) *tws.top0++=x; else if (i==1) *tws.top1--=x; else return ERROR; return OK; }
SElemType Pop(TSqStack &tws, int i){
//若双向栈tws的第i个栈不空,则删除第i个栈的栈顶元素并返回其值,否则返回空元素
if (i!=0||i!=1) return NULL;
if (i==0&&tws.top0) return *--tws.top0;
if (i==1&&tws.top1!=tws.base[StackSize-1]) return *++tws.top1;
return NULL; } 3.17
Status Model(){
//识别依次读入的一个以@为结束符的字符序列是否为形如‘序列1&序列2’模式的字符序列
//序列1和序列2中不包含字符‘&’,序列1是序列2的逆序列 InitStack(s); c=getchar();
while (c!='&') {Push(s,c); c=getchar();} c=getchar();
while (c!='@'&&!StackEmpty(s)) { Pop(s,x);
if (c==x) c=getchar(); else return FALSE; }
if (c=='@' && StackEmpty(s)) return TRUE; else return FALSE; } 3.19
Status BracketsMatch(){
//判断依次读入的以‘@’为结束符的算术表达式中括号是否正确配对出现 InitStack(s); c=getchar(); while (c!='@'){
if ( c=='(' || c=='[' || c=='{') Push(s,c); if ( c==')' || c==']' || c=='}'){ if EmptyStack(s) return FALSE; Pop(s,x);
if (!((x=='('&&c==')')||(x=='['&&c==']')||(x=='{'&&c=='}'))) return FALSE; }//if c=getchar(); }//while
if EmptyStack(s) return TRUE; else return FALSE; } 3.28
typedef struct CQNode { CQElemType data; struct CQNode *next; }CQNode, *CQueuePtr;
typedef struct {