一 . 用队列实现栈
1.1栈的初始化创建
malloc 使其能在函数外还能存在 初始化两个队列
MyStack* myStackCreate() {
MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
QueueInit(&obj->q1);
QueueInit(&obj->q2);
return obj;
}
1.2入栈
如果队列1非空 入队队列1 反之 入队队列2
void myStackPush(MyStack* obj, int x) {
if(!QueueEmpty(&obj->q1))
{
QueuePush(&(obj->q1),x);
}
else
{
QueuePush(&(obj->q2),x);
}
}
1.3出栈
用假设法来实现判断哪个队列为空
int myStackPop(MyStack* obj) {
//假设
Queue* Empty = &obj->q1;
Queue* noEmpty = &obj->q2;
if(!QueueEmpty(&obj->q1))
{
Empty = &obj->q2;
noEmpty = &obj->q1;
}
//不为空 前size-1个数据导到空队列去 删除最后一个就是栈顶数据
while(QueueSize(noEmpty)>1)
{
QueuePush(Empty,QueueFront(noEmpty));
QueuePop(noEmpty);
}
int top = QueueFront(noEmpty);
QueuePop(noEmpty);
return top;
}
1.4取栈顶数据加判空
int myStackTop(MyStack* obj) {
if(!QueueEmpty(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}
1.5销毁栈
不可以直接free(obj) 要先销毁队列1 2
void myStackFree(MyStack* obj) {
QueueDestroy(&obj->q1);
QueueDestroy(&obj->q2);
free(obj);
}
代码如下:
由于C语言OJ题没有接口 所有我们要自己“造轮子 ” 引入队列的接口实现。
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType val;
}QNode;
typedef struct Queue
{
QNode* phead;
QNode* ptail;
int size;
}Queue;
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = NULL;
pq->ptail = NULL;
pq->size = 0;
}
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->phead;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
// 队尾插入
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
return;
}
newnode->next = NULL;
newnode->val = x;
if (pq->ptail == NULL)
{
pq->phead = pq->ptail = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
pq->size++;
}
// 队头删除
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->size != 0);
// 一个节点
if (pq->phead->next == NULL)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else // 多个节点
{
QNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->phead);
return pq->phead->val;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->ptail);
return pq->ptail->val;
}
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->size == 0;
}
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate() {
MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
QueueInit(&obj->q1);
QueueInit(&obj->q2);
return obj;
}
void myStackPush(MyStack* obj, int x) {
if(!QueueEmpty(&obj->q1))
{
QueuePush(&(obj->q1),x);
}
else
{
QueuePush(&(obj->q2),x);
}
}
int myStackPop(MyStack* obj) {
//假设
Queue* Empty = &obj->q1;
Queue* noEmpty = &obj->q2;
if(!QueueEmpty(&obj->q1))
{
Empty = &obj->q2;
noEmpty = &obj->q1;
}
//不为空 前size-1个数据导到空队列去 删除最后一个就是栈顶数据
while(QueueSize(noEmpty)>1)
{
QueuePush(Empty,QueueFront(noEmpty));
QueuePop(noEmpty);
}
int top = QueueFront(noEmpty);
QueuePop(noEmpty);
return top;
}
int myStackTop(MyStack* obj) {
if(!QueueEmpty(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj) {
QueueDestroy(&obj->q1);
QueueDestroy(&obj->q2);
free(obj);
}
二. 用栈实现队列
由于栈 后进先出 的性质 我们只需要让一个栈为pushst 一个栈为popst
2.1 队列的初始化创建
MyQueue* myQueueCreate() {
MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
STInit(&obj->pushst);
STInit(&obj->popst);
return obj;
}
2.2入队
void myQueuePush(MyQueue* obj, int x) {
STPush(&(obj->pushst),x);
}
2.3出队
int myQueuePop(MyQueue* obj) {
int cur = myQueuePeek(obj);
STPop(&(obj->popst));
return cur;
}
2.4取队头数据加判空
int myQueuePeek(MyQueue* obj) {
if(STEmpty(&(obj->popst)))
{
while(!STEmpty(&(obj->pushst)))
{
int top = STTop(&(obj->pushst));
STPush(&(obj->popst),top);
STPop(&(obj->pushst));
}
}
return STTop(&(obj->popst));
}
bool myQueueEmpty(MyQueue* obj) {
return STEmpty(&(obj->pushst)) && STEmpty(&(obj->popst));
}
2.5销毁队列
void myQueueFree(MyQueue* obj) {
STDestroy(&(obj->pushst));
STDestroy(&(obj->popst));
free(obj);
}
代码如下:
由于C语言OJ题没有接口 所有我们要自己“造轮子 ” 引入栈的接口实现。
typedef int STDataType;
typedef struct stack
{
STDataType* a;
int top;
int capacity;
}ST;
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0;
pst->capacity = 0;
}
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->top = pst->capacity = 0;
}
void STPush(ST* pst, STDataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
void STPop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
pst->top--;
}
STDataType STTop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
return pst->a[pst->top - 1];
}
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
typedef struct {
ST pushst;
ST popst;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
STInit(&obj->pushst);
STInit(&obj->popst);
return obj;
}
void myQueuePush(MyQueue* obj, int x) {
STPush(&(obj->pushst),x);
}
int myQueuePop(MyQueue* obj) {
int cur = myQueuePeek(obj);
STPop(&(obj->popst));
return cur;
}
int myQueuePeek(MyQueue* obj) {
if(STEmpty(&(obj->popst)))
{
while(!STEmpty(&(obj->pushst)))
{
int top = STTop(&(obj->pushst));
STPush(&(obj->popst),top);
STPop(&(obj->pushst));
}
}
return STTop(&(obj->popst));
}
bool myQueueEmpty(MyQueue* obj) {
return STEmpty(&(obj->pushst)) && STEmpty(&(obj->popst));
}
void myQueueFree(MyQueue* obj) {
STDestroy(&(obj->pushst));
STDestroy(&(obj->popst));
free(obj);
}
三.设计循环队列
typedef struct {
int front;
int rear;
int size;
int *data;
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
//队列的创建(注意队列在创建时大小为k,即数组的size为k+1)
//front指的是头指针,rear为尾指针,但该尾指针是尾指针的下一个【下一次插入的位置】
//队列为空时,front与rear相同,队列为满时,rear与front相减的绝对值为k)
MyCircularQueue *obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
obj -> front = obj -> rear = 0;
obj -> data = (int*)malloc(sizeof(int)*(k+1));
obj -> size = k+1;
return obj;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(obj -> front == ((obj -> rear + 1)%(obj -> size))){
//判断队列为满
return false;
}
obj -> data[obj -> rear] = value;
obj -> rear = (obj -> rear + 1)%(obj -> size);
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(obj -> front == obj -> rear){//判断队列为空
return false;
}
obj -> front = (obj -> front + 1)%(obj -> size);
if(obj -> front == obj -> rear){
obj -> front = obj -> rear = 0;
}
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(obj -> front == obj -> rear){//判断队列为空
return -1;
}
int num = obj -> data[obj -> front];
return num;
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(obj -> front == obj -> rear){//判断队列为空
return -1;
}
int num = obj -> data[(obj -> rear-1 + obj -> size)%(obj ->size)];
return num;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
if(obj -> front == obj -> rear){
return true;
}
return false;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
if((obj -> rear +1)%(obj -> size) == (obj -> front)%(obj -> size)){
return true;
}
return false;
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj -> data);
free(obj);
}
要熟练掌握队列和栈的结构与接口实现