顺序栈
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define ElemType int
#define Status int
#define ok 1
#define error 0
typedef struct{
ElemType data[MAXSIZE];
int top;
}stack;
void InitStack(stack *S){
S->top = -1;
}
Status Push(stack *S, ElemType e){
if(S->top==MAXSIZE-1) return error;
S->data[++S->top] = e;
return ok;
}
int EmptyStack(stack *S){
if(S->top==-1) return 1;
else return 0;
}
Status GetTopStack(stack *S, ElemType *x){
if(EmptyStack(S)==1) return error;
*x = S->data[S->top];
return ok;
}
Status Pop(stack *S,ElemType *x){
if(EmptyStack(S)==1) return error;
*x = S->data[S->top];
S->top--;
return ok;
}
Status DestroyStack(stack *S);
Status ClearStack(stack *S){
S->top = -1;
return ok;
}
int StackLength(stack *S){
if(S->top==-1) return 0;
return S->top+1;
}
void Display(stack *S){
if(EmptyStack(S)==1){
printf("stack is null\n");
}
while(S->top!=-1){
printf("%d ",S->data[S->top]);
S->top--;
}
}
int main(void){
stack S;
InitStack(&S);
int i=5,n,m=0;
printf("请输入5个元素:\n");
while(i){
scanf("%d",&n);
Push(&S,n);
i--;
}
printf("此时栈顶元素为:");
GetTopStack(&S,&m);
printf("%d\n",m);
printf("弹出栈顶元素为:");
Pop(&S,&m);
printf("%d\n",m);
printf("栈中还有元素个数/栈的长度为:");
printf("%d\n",StackLength(&S));
printf("栈中元素为:");
Display(&S);
return 0;
}
循环队列
#include <stdio.h>
#define MAXSIZE 10
#define ElemType int
#define Status int
#define ok 1
#define error 0
typedef struct {
ElemType data[MAXSIZE];
int front;
int rear;
}queue;
void InitQueue (queue *Q);
Status EnQueue (queue *Q, ElemType e);
Status DeQueue (queue *Q, ElemType *x);
Status GetTop (queue *Q, ElemType *x);
int EmptyQueue (queue *Q);
int FullQueue (queue *Q);
void ClearQueue (queue *Q);
int QueueLen (queue *Q);
void Display (queue *Q);
int main (void) {
queue Q;
InitQueue (&Q);
int n,t;
ElemType m, x,y;
printf ("请输入要输入的元素个数:\n");
scanf ("%d", &n);
int i;
for (i = 0; i < n; i++){
scanf ("%d", &m);
EnQueue (&Q, m);
}
printf ("当前队列的队首元素为:\n");
GetTop (&Q, &x);
printf ("%d\n", x);
printf ("请输入出队元素个数:\n");
scanf ("%d", &t);
for (i = 0; i < t; i++) {
DeQueue (&Q, &y);
}
printf ("当前队列元素为:\n");
Display (&Q);
return 0;
}
void InitQueue (queue *Q) {
Q->front = Q->rear = 0;
}
Status EnQueue (queue *Q, ElemType e) {
if (FullQueue (Q) == 1) {
printf ("队列满,入队失败\n");
return error;
}
Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1) % MAXSIZE;
return ok;
}
Status DeQueue (queue *Q, ElemType *x) {
if (EmptyQueue (Q) == 1) {
printf ("队列空,出队失败\n");
return error;
}
*x = Q->data[Q->front];
Q->front++;
return ok;
}
Status GetTop (queue *Q, ElemType *x) {
if (EmptyQueue (Q) ==1) {
printf ("队列空,获取队首元素失败\n");
return error;
}
*x = Q->data[Q->front];
return ok;
}
int EmptyQueue (queue *Q) {
if (Q->front == Q->rear) return 1;
return 0;
}
int FullQueue (queue *Q) {
if (Q->front == (Q->rear + 1) % MAXSIZE) return 1;
return 0;
}
void ClearQueue (queue *Q) {
Q->front = Q->rear = 0;
}
int QueueLen (queue *Q) {
return (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
}
void Display (queue *Q) {
int i, n = QueueLen (Q);
for (i = 0; i < n; i++) {
printf ("%d", Q->data[ (Q->front + i) % MAXSIZE]);
}
}