3.1 栈的类型定义
description
Transcript of 3.1 栈的类型定义
-
3.1 3.3 3.2 3.4 3.5
-
Insert(L, i, x) Insert(S, n+1, x) Insert(Q, n+1, x) 1in+1 Delete(L, i) Delete(S, n) Delete(Q, 1) 1in(LIFO)(FIFO)
-
3.1 ADT Stack { D{ ai | ai SElemSet, i=1,2,...,n, n0 } R1{ | ai-1, aiD, i=2,...,n } an a1 } ADT Stack
-
InitStack(&S)ClearStack(&S)IsEmpty(S)StackLength(S)GetTop(S, &e)Push(&S, e)Pop(&S, &e)StackTravers(S, visit())IsFull(S)
-
InitStack(&S) S
-
IsEmpty(S) S S TRUE FALSE
-
IsFull(S) S S TRUE FALSE
-
StackLength(S) S S
-
GetTop(S, &e) S e S a1a2an
-
ClearStack(&S) S S
-
Push(&S, e) S e
a1a2ane
-
Pop(&S, &e) S S e a1a2anan-1
-
3.2
-
#define MaxSize 100typedef struct { SElemType data[MaxSize]; int top; //top (base0) } SqStack; ()
-
void InitStack(SqStack *s){ s->top=-1;} /*InitStack;*/
-
int Push(SqStack *s,SElemType e){ if(s->top>=MaxSize-1) return ERROR; /**/ s->data[++s->top]=e; return OK;}
-
int Pop(SqStack *s,SElemType *e){ if(s->top==-1)return ERROR;/**/ *e=s->data[ s->top--]; return OK;}
-
SElemType GetTop(SqStack s){ return(s.data[s.top]); }
-
Sint IsEmpty(SqStack s){ return(s.top==-1);}
-
Sint IsFull(SqStack s){ return(s.top==MaxSize-1);}
-
a1an: an-1
-
3.3 ( )
-
N = (N div d)d + N mod d
-
1348)10 = (2504)8
N N div 8 N mod 81348 168 4 168 21 0 21 2 5 2 0 2
-
void conversion () { InitStack(&S); scanf ("%d",&N); while (N) { Push(&S, N % 8); N = N/8; } while (!IsEmpty(S)) { Pop(&S,&e); printf ( "%d", e ); }} // conversion
-
)
-
: [ ( [ ] [ ] ) ] 1 2 3 4 5 6 7 8
-
12 (,) 3 ()
- Status matching(string exp) { int state = 1,i=0; while (i
-
?
-
#@
-
whli##ilr#es#*s) outcha@putchar(*s=#++); while (*s) putchar(*s++);
-
while (ch != EOF && ch != '\n') { switch (ch) { case '#' : If(!StackEmpty(S)) Pop(&S, &c); break; case '@': ClearStack(&S); break;// S default : Push(&S, ch); break; // } ch = getchar(); // }ClearStack(&S); // Sif (ch != EOF) ch = getchar();while (ch != EOF) { //EOF
}
-
:
::= () + () + () ::= | :: = |
-
1),2)3), ,(#
-
,.OPTR,;OPND,.:1)OPND,#OPTR2),OPND,,OPTR,(OPTR#)
-
#(2+3)*4# # ( 2 + 3 )
-
#(2+3)*4# # ( 2 + 3 ) =5 5
-
#(2+3)*4# # 5 * 4 #
-
#(2+3)*4# # 5 # 4 * =20 20
-
c=getchar();while(c!='#' || GetTop(OPTR) !='#') { if(!In(c,op)) { Push(&OPND,c);c=getchar();}/*OPND*/ else switch( precede(GetTop(OPTR),c)) { case '': Pop(&OPTR,&theta); Pop(&OPND,&b);Pop(&OPND, &a); Push(&OPND,Operate(a, theta,b)); break; }/*switch end here*/ } /*while end here */
-
n! 0=1 n!=n*(n-1)! n>=1int factorial(int n){ if(n==0) return 1; return n*factorial(n-1);}
-
1 nF1=1F2=1Fn=Fn-1+Fn-2
int Fib(int n){ /* 1,1,2,3,5,8,...*/if (n
-
2 void hanoi (int n, char x, char y, char z) {// x1n// nzy if (n==1) move(x, 1, z); // xz else { hanoi(n-1, x, z, y); // xn-1 //y, z move(x, n, z); // nxz hanoi(n-1, y, x, z); // yn-1 //z, x } }
-
void main( ){ void a( ){ void b( ){ a( ); b( ); }//main }// a }// bMainab
-
ADT Queue { D{ai | aiElemSet, i=1,2,...,n, n0} R1{ | ai-1, ai D, i=2,...,n} a1 an 3.4 } ADT Queue
-
InitQueue(&Q)DestroyQueue(&Q)QueueEmpty(Q)QueueLength(Q)GetHead(Q, &e)ClearQueue(&Q)DeQueue(&Q, &e)EnQueue(&Q, e)QueueTravers(Q, visit())
-
InitQueue(&Q) Q
DestroyQueue(&Q)Q Q
-
QueueEmpty(Q)
Q QTRUEFALSE
-
QueueLength(Q)
Q Q
-
GetHead(Q, &e) Q eQa1a2an
-
ClearQueue(&Q)
Q Q
-
EnQueue(&Q, e) Q eQa1a2ane
-
DeQueue(&Q, &e) Q Qea1a2an
-
3.5
-
typedef struct QNode {// QElemType data; struct QNode *next; } QNode, *QueuePtr;
-
typedef struct { // QueuePtr front; // QueuePtr rear; // } LinkQueue;a1anQ.frontQ.rearQ.frontQ.rear
-
int InitQueue (LinkQueue *Q) { // Q Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); if (!Q->front) return ERROR; // Q->front->next = NULL; return OK;}
-
int EnQueue (LinkQueue &Q, QElemType e) { // eQ p = (QueuePtr) malloc (sizeof (QNode)); if (!p) return ERROR; // p->data = e; p->next = NULL; Q->rear->next = p; Q->rear = p; return OK;}
-
int DeQueue (LinkQueue *Q, QElemType *e) { // Q // e OKERROR if (Q->front == Q->rear) return ERROR; p = Q->front->next; *e = p->data; Q->front->next = p->next; if (Q->rear == p) Q->rear = Q->front;// free (p); return OK;}
-
#define MAXQSIZE 100 // typedef struct { QElemType data[MAXQSIZE]; int front; // // int rear; // // } SqQueue;
-
1.,,frontrear?2.?:
-
int EnQueue (SqQueue *Q, ElemType e) { // eQ if ((Q->rear+1) % MAXQSIZE == Q->front) return ERROR; // Q->data[Q->rear] = e; Q->rear = (Q->rear+1) % MAXQSIZE; return OK;}
-
int DeQueue (SqQueue *Q, ElemType *e) { // Q // eOK; ERROR if (Q->front == Q->rear) return ERROR; *e = Q->data[Q->front]; Q->front = (Q->front+1) % MAXQSIZE; return OK;}
-
int QueueEmpty (SqQueue Q) {//return (Q.front==Q.rear);}int QueueFull (SqQueue Q) {//return ((Q.rear+1)% MAXQSIZE ==Q.front);}void ClearQueue (SqQueue *Q) {//Q->rear=Q->front=0;}
-
3.6 anq
-
1. 2. 3. 4.