if(S.base) return ERROR; S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return OK; }
Status Pop(SqStack &S,SElemType &e) { if(S.top==S.base) return ERROR; e=*--S.top; return OK; }
Status GetTop(SqStack S,SElemType &e) { if(S.top==S.base) return ERROR; e=*(S.top-1); return OK; }
int StackLength(SqStack S) { int i=0; while(S.top!=S.base) { i++; S.top--; } return i; }
Status StackTraverse(SqStack S) { SElemType *p=(SElemType*)malloc(sizeof(SElemType)); p=S.top; if(S.top==S.base) printf(\ else {
printf(\ p--; S.base--; while(p!=S.base) { printf(\ p--; } } printf(\ return OK; }
int main() { int a; SqStack S; SElemType x,e; if(InitStack(S)) printf(\ while(1) { printf(\the Top\\n4:Return the Length of the Stack\\n5:Load the Stack\\n0:Exit\\nPlease choose:\\n\ scanf(\ switch(a) { case 1:scanf(\ if(!Push(S,x)) printf(\ else printf(\ break; case 2:if(!Pop(S,e)) printf(\ else printf(\ break; case 3:if(!GetTop(S,e)) printf(\ else printf(\ break; case 4:printf(\
break; case 5:StackTraverse(S); break; case 0:return 1; } } }
2.2循环队列的基本操作 #include
typedef int QElemType; #define MAXQSIZE 100
typedef struct { QElemType *base; int front; int rear; }SqQueue;
Status InitQueue(SqQueue &Q) { Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType)); if(!Q.base) return ERROR; Q.front=Q.rear=0; return OK; }
Status EnQueue(SqQueue &Q,QElemType e) { if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR; Q.base[Q.rear]=e; Q.rear=(Q.rear+1)%MAXQSIZE; return OK; }
Status DeQueue(SqQueue &Q,QElemType &e) { if(Q.front==Q.rear)
return ERROR; e=Q.base[Q.front]; Q.front=(Q.front+1)%MAXQSIZE; return OK; }
Status GetHead(SqQueue Q,QElemType &e) { if(Q.front==Q.rear) return ERROR; e=Q.base[Q.front]; return OK; }
int QueueLength(SqQueue Q) { return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE; }
Status QueueTraverse(SqQueue Q) { int i; i=Q.front; if(Q.front==Q.rear) printf(\ else { printf(\ while(i!=Q.rear) { printf(\ i=i+1; } } printf(\ return OK; }
int main() { int a; SqQueue S; QElemType x,e; if(InitQueue(S))
printf(\ while(1) { printf(\\\n2:Delete \\n3:Get the Front \\n4:Return the Length of the Queue\\n5:Load the Queue\\n0:Exit\\nPlease choose:\\n\ scanf(\ switch(a) { case 1: scanf(\ if(!EnQueue(S,x)) printf(\ else printf(\ break; case 2: if(!DeQueue(S,e)) printf(\ else printf(\ break; case 3: if(!GetHead(S,e)) printf(\ else printf(\ break; case 4: printf(\ break; case 5: QueueTraverse(S); break; case 0: return 1; } } }
2.3栈的应用——进制转换 #include
#define STACK_INIT_SIZE 100 #define STACKINCREMENT 10
typedef int SElemType; typedef int Status;