CQueuePtr rear; }CLinkQueue;
Status InitCQueue(CLinkQueue &cq) {
//初始化一个只有尾指针的带头结点的循环链表表示的队列
if (!(cq.rear=(CQueuePtr)malloc(sizeof(CQNode))) exit(OVERFLOW); cq.rear->next=cq.rear; return OK; }
Status EnCQueue(CLinkQueue &cq, CQElemType e){ //插入元素e为cq的新的队尾元素
if (!(p=(CQueuePtr)malloc(sizeof(CQNode)))) exit(OVERFLOW); p->data=e; p->next=cq.rear->next; cq.rear->next=p; cq.rear=p; return OK; }
Status DeCQueue(CLinkQueue &cq, CQElemType &e){ //若队列不空,则删除cq的队头元素,用e返回其值 //并返回OK,否则返回ERROR if (cq.rear=cq.rear->next) return ERROR;
p=cq.erar->next->next;
e=p->data; cq.rear->next->next=p->next; if (cq.rear==p) cq.rear=cq.rear->next; free(p); return OK; } 3.30
#define MAXSIZE 100; typedef struct { QElemType *base; int rear; int length; }SqQueue;
Status EnQueue(SqQueue &q, QElemType e){ //插入元素e为q的新的队尾元素 if (q.length==MAXSIZE) return ERROR; q.base[q.rear]=e;
q.rear=(q.rear+1)%MEXSIZE; return OK; }
Status DeQueue(sQQueue &q, QElemType &e){ //若队列不空,则删除q的队头元素,用e返回其值 //并返回OK,否则返回ERROR if (q.length==0) return ERROR;
e=q.base[(q.rear-q.length+MEXSIZE)%MAXSIZE]; q.length--; return OK; } 3.31
Status ReturnText(){
//判断读入的一个以‘@’为结束符的字符序列是否为回文 InitStack(s); InitQueue(q); c=getchar(); while (c!='@'){
Push(s,c); EnQueue(q,c); c=getchar(); }
while (!EmptyStack(s)){ Pop(s,x); DeQueue(q,y); if (x!=y) return FALSE; }
return TRUE;
}
(以下内容来自http://bbs.kaoyan.com)
第三章 栈与队列 3.15 typedef struct{
Elemtype *base1[1]; Elemtype *top1[1]; }BDStacktype; //双向栈类型
Status Init_Stack(BDStacktype &tws,int m)//初始化一个大小为m的双向栈tws {
tws.base[ 0 ]=(Elemtype*)malloc(sizeof(Elemtype)); tws.base=tws.base[ 0 ]+m; tws.top[ 0 ]=tws.base[ 0 ]; tws.top=tws.base; return OK; }//Init_Stack
Status push(BDStacktype &tws,int i,Elemtype x)//x入栈,i=0表示低端栈,i=1表示高端栈 {
if(tws.top[ 0 ]>tws.top) return OVERFLOW; //注意此时的栈满条件 if(i==0) *tws.top[ 0 ]++=x; else if(i==1) *tws.top--=x; else return ERROR; return OK; }//push
Status pop(BDStacktype &tws,int i,Elemtype &x)//x出栈,i=0表示低端栈,i=1表示高端栈 { if(i==0) {
if(tws.top[ 0 ]==tws.base[ 0 ]) return OVERFLOW;
x=*--tws.top[ 0 ]; }
else if(i==1) {
if(tws.top==tws.base) return OVERFLOW; x=*++tws.top; }
else return ERROR; return OK; }//pop 3.16
void Train_arrange(char *train)//这里用字符串train表示火车,'H'表示硬席,'S'表示软席 {
p=train;q=train; InitStack(s); while(*p) {
if(*p=='H') push(s,*p); //把'H'存入栈中 else *(q++)=*p; //把'S'调到前部 p++; }
while(!StackEmpty(s)) {
pop(s,c);
*(q++)=c; //把'H'接在后部 }
}//Train_arrange 3.17
int IsReverse()//判断输入的字符串中'&'前和'&'后部分是否为逆串,是则返回1,否则返回0 {
InitStack(s);