头文件"SqStackQueue.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType
;
typedef int Status
;
typedef struct {
ElemType
* base
;
ElemType
* top
;
int stacksize
;
}SqStack
;
Status
InitStack(SqStack
&S
);
Status
DestroyStack(SqStack
& S
);
Status
ClearStack(SqStack
& S
);
Status
GetTop(SqStack S
, ElemType
& e
);
Status
Push(SqStack
& S
, ElemType e
);
Status
Pop(SqStack
& S
, ElemType
& e
);
Status
StackEmpty(SqStack S
);
typedef struct {
ElemType
* base
;
int front
;
int rear
;
}SqQueue
;
Status
InitQueue_Sq(SqQueue
& Q
);
Status
EnQueue_Sq(SqQueue
& Q
, ElemType e
);
Status
DeQueue_Sq(SqQueue
& Q
, ElemType
& e
);
Status
GetHead(SqQueue Q
, ElemType
& e
);
Status
QueueEmpty(SqQueue Q
);
Status
DestroyQueue(SqQueue
& Q
);
Status
ClearQueue(SqQueue
& Q
);
栈的功能实现StackFun.c
#include "SqStackQueue.h"
Status
InitStack(SqStack
& S
) {
S
.base
= (ElemType
*)malloc(STACK_INIT_SIZE
* sizeof(ElemType
));
if (!S
.base
) exit(OVERFLOW
);
S
.top
= S
.base
;
S
.stacksize
= STACK_INIT_SIZE
;
return OK
;
}
Status
DestroyStack(SqStack
& S
) {
if (!S
.base
) return ERROR
;
free(S
.base
);
S
.base
= S
.top
= NULL;
S
.stacksize
= 0;
return OK
;
}
Status
ClearStack(SqStack
& S
) {
if (!S
.base
) return ERROR
;
S
.top
= S
.base
;
return OK
;
}
Status
GetTop(SqStack S
, ElemType
& e
) {
if (S
.top
== S
.base
) return ERROR
;
e
= *(S
.top
- 1);
return OK
;
}
Status
Push(SqStack
& S
, ElemType e
) {
if (S
.top
- S
.base
>= S
.stacksize
) {
S
.base
= (ElemType
*)realloc(S
.base
, (S
.stacksize
+ STACKINCREMENT
) * sizeof(ElemType
));
if (!S
.base
) exit(OVERFLOW
);
S
.top
= S
.base
+ S
.stacksize
;
S
.stacksize
+= STACKINCREMENT
;
}
*S
.top
++ = e
;
return OK
;
}
Status
Pop(SqStack
& S
, ElemType
& e
) {
if (S
.top
== S
.base
) return ERROR
;
e
= *--S
.top
;
return OK
;
}
Status
StackEmpty(SqStack S
) {
if (S
.top
== S
.base
) return OK
;
else return ERROR
;
}
循环队列功能实现QueueFun.c
#include "SqStackQueue.h"
Status
InitQueue_Sq(SqQueue
& Q
) {
Q
.base
= (ElemType
*)malloc(MAXSIZE
* sizeof(ElemType
));
if (!Q
.base
) exit(OVERFLOW
);
Q
.front
= Q
.rear
= 0;
return OK
;
}
Status
EnQueue_Sq(SqQueue
& Q
, ElemType e
) {
if ((Q
.rear
+ 1) % MAXSIZE
== Q
.front
) return ERROR
;
Q
.base
[Q
.rear
] = e
;
Q
.rear
= (Q
.rear
+ 1) % MAXSIZE
;
return OK
;
}
Status
DeQueue_Sq(SqQueue
& Q
, ElemType
& e
) {
if (Q
.rear
== Q
.front
) return ERROR
;
e
= Q
.base
[Q
.front
];
Q
.front
= (Q
.front
+ 1) % MAXSIZE
;
return OK
;
}
Status
GetHead(SqQueue Q
, ElemType
& e
) {
if (Q
.rear
== Q
.front
) return ERROR
;
e
= Q
.base
[Q
.front
];
return OK
;
}
Status
QueueEmpty(SqQueue Q
) {
if (Q
.rear
== Q
.front
) return OK
;
else return ERROR
;
}
Status
DestroyQueue(SqQueue
& Q
) {
if (!Q
.base
) return ERROR
;
free(Q
.base
);
Q
.front
= Q
.rear
= NULL;
return OK
;
}
Status
ClearQueue(SqQueue
& Q
) {
if (!Q
.base
) return ERROR
;
Q
.front
= Q
.rear
;
return OK
;
}
转载请注明原文地址: https://lol.8miu.com/read-11332.html