栈
顺序栈
#define MaxSize 50
typedef struct
{int data[MaxSize];int top;
} SqStack;
void InitStack(SqStack &S)
{S.top = -1;
}
bool Push(SqStack &S, int x)
{if (S.top = MaxSize - 1)return false;S.data[++S.top] = x;return true;
}
bool GetTop(SqStack &S, int &x)
{if (S.top == -1)return false;x = S.data[S.top];return true;
}
bool StackEmpty(SqStack &S)
{if (S.top == -1)return true;return false;
}
bool Pop(SqStack *S, int &x)
{if (S->top == -1)return false;x = S->data[S->top--];return true;
}
链栈
typedef struct LinkNode
{int data;struct LinkNode *next;
} LiStack;
共享栈
#define MaxSize 50
typedef struct
{int data[MaxSize];int top0;int top1;
} ShStack;void InitStack(ShStack &S)
{S.top0 = -1;S.top1 = MaxSize;
}
队列
循环队列
#define MaxSize 50
typedef struct
{int data[MaxSize];int front, rear;
} SqQueue;
void InitQueue(SqQueue &Q)
{Q.rear = Q.front = 0;
}
bool EnEmmpty(SqQueue &Q, int x)
{if ((Q.rear + 1) % MaxSize == Q.front)return false;Q.data[Q.rear] = x;Q.rear = (Q.rear + 1) % MaxSize;return true;
}
bool isEmpty(SqQueue &Q)
{if (Q.rear == Q.front)return true;return false;
}
bool DeEmpty(SqQueue &Q, int &x)
{if (Q.rear == Q.front)return false;x = Q.data[Q.front];Q.front = (Q.front + 1) % MaxSize;return true;
}
bool Gethead(SqQueue Q, int &x)
{if (Q.rear == Q.front)return false;x = Q.data[Q.front];return true;
}
带头结点的链式队列
typedef struct LinkNode
{int data;struct LinkNode *next;
} LinkNode;typedef struct
{LinkNode *front, *rear;
} LinkNode;
void InitQueue(LinkNode &Q)
{Q.front = Q.rear = (LinkNode *)malloc(sizeof(LinkNode));Q.front->next = NULL;
}
bool isEmpty(LinkNode &Q)
{if (Q.front == Q.rear)return true;return false;
}
bool EnEmpty(LinkNode &Q, int x)
{LinkNode *s = (LinkNode *)malloc(sizeof(LinkNode));s->data = x;s->next = NULL;Q.rear->next = s;Q.rear = s;return true;
}
bool DeEmpty(LinkNode &Q, int &x)
{if (Q.rear == Q.front)return false;LinkNode *p = Q.front->next;x = p->data;Q.front->next = Q.front->next->next;if (Q.rear == p)Q.rear = Q.front;free(p);return true;
}
不带头结点的链式队列
typedef struct LinkNode
{int data;struct LinkNode *next;
} LinkNode;typedef struct
{LinkNode *front, *rear;
} LinkNode;
void InitQueue(LinkNode &Q)
{Q.front = Q.rear = NULL;
}
bool isEmpty(LinkNode &Q)
{if (Q.front == NULL)return true;return false;
}
bool EnEmpty(LinkNode &Q, int x)
{LinkNode *s = (LinkNode *)malloc(sizeof(LinkNode));s->data = x;s->next = NULL;if (Q.front == NULL){Q.front = s;Q.rear = s;}else{Q.rear->next = s;Q.rear = s;}return true;
}
bool DeEmpty(LinkNode &Q, int &x)
{if (Q.rear == NULL)return false;LinkNode *p = Q.front;x = p->data;Q.front = p->next;if (Q.rear == p){Q.rear = NULL;Q.front = NULL;}free(p);return true;
}