
自己写的栈,再让其他函数去调用自己写的栈
- typedef int QDataType;
- typedef struct QueueNode
- {
- struct QueueNode* next;//单链表
- QDataType data;//放数据
- }QNode;
-
- typedef struct Queue
- {
- QNode* phead;//头节点
- QNode* ptail;//尾节点
- QDataType size; //统计有多少节点
- }Queue;
-
- void QueueInit(Queue *pq);//初始化
- void QueueDestroy(Queue*pq);//销毁
- void QueuePush(Queue*pq,QDataType x);//插入数据
- void QueuePop(Queue* pq);//队头出数据就是删队头
- QDataType QueueFront(Queue* pq);//返回对头数据
- QDataType QueueBack(Queue* pq);//返回队尾数据
- int QueueSize(Queue* pq);//返回总的数据个数
- bool QueueEmpty(Queue* pq);//是空的返回真
-
-
- 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 = NULL;
- pq->ptail = NULL;
- pq->size = 0;
- }
-
-
- void QueuePush(Queue* pq, QDataType x)//插入数据
- {
- QNode* newnode = (QNode*)malloc(sizeof(QNode));//扩大的是第一个结构体
- if (newnode == NULL)
- {
- perror("malloc");
- return;
- }
- newnode->data = x;
- newnode->next = NULL;
-
- if (pq->phead == NULL)
- {
-
- pq->phead = pq->ptail = newnode;
- }
- else
- {
- pq->ptail->next = newnode;
- pq->ptail = newnode;
- }
- pq->size++;
- }
-
-
- void QueuePop(Queue* pq)//队头出数据就是删队头
- {
- assert(pq);
- assert(!QueueEmpty(pq));
- 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(!QueueEmpty(pq));
- return pq->phead->data;
- }
-
- QDataType QueueBack(Queue* pq)//返回队尾数据
- {
- assert(pq);
- assert(!QueueEmpty(pq));
- return pq->ptail->data;
- }
-
- int QueueSize(Queue* pq)//返回总的数据个数
- {
- assert(pq);
-
- return pq->size;
- }
-
- bool QueueEmpty(Queue* pq)//是空的返回真
- {
- assert(pq);
-
- return pq->phead == NULL && pq->ptail == NULL;
- }
1.一个结构体包含俩个队列

- typedef struct {
- Queue q1;//第一个队列
- Queue q2;//第二个队列
- } MyStack;
2.希望创造一个包含两个队列的结构体,并且把这样的结构返回去,通过MyStack结构体一把molloc两个队列结构体

-
- MyStack* myStackCreate() {
- MyStack*obj = (MyStack*)malloc(sizeof(MyStack));
- if(obj == NULL)
- {
- perror("molloc");
- }
-
- QueueInit(&obj->q1);
- QueueInit(&obj->q2);
- return obj;
- }
3.插入数据,q1队列有数据就进入(if语句)把新数据插入q1队列,q1队列没数据就把新数据插入q2队列,若是两个队列都是空就把新数据随便入一个队列

- //插入数据
- void myStackPush(MyStack* obj, int x) {
- if(!QueueEmpty(&obj->q1))
- {
- QueuePush(&obj->q1, x);
- }
- else
- {
- QueuePush(&obj->q2, x);
- }
- }
4.用两个队列像一个要像一个栈一样出数据取数据,把q1队列的数据倒q2,倒到了最后一个元素再去返回和删除元素

- //移除并返回栈顶元素
- int myStackPop(MyStack* obj) {
- //空数据队列
- Queue* PEmptyQ = &obj->q1;
-
- //有数据队列
- Queue* PNonEmptyQ = &obj->q2;
- //假设空数据和有数据队列假设赋值错了
- if(!QueueEmpty(&obj->q1))
- {
- PEmptyQ = &obj->q2;
- PNonEmptyQ = &obj->q1;
- }
- //倒数据
- while(QueueSize(PNonEmptyQ) > 1)
- {
- QueuePush(PEmptyQ,QueueFront(PNonEmptyQ));
- QueuePop(PNonEmptyQ);
- }
- int top = QueueFront(PNonEmptyQ);//调用了取对头数据函数
- QueuePop(PNonEmptyQ);//调用了删除队头数据函数
- return top;
- }
5.返回栈顶数据,就是返回队列的队尾数据
- //取栈顶元素
- int myStackTop(MyStack* obj) {
- if(!QueueEmpty(&obj->q1))
- {
- return QueueBack(&obj->q1);//调用了取队尾的数据的函数
- }
- else
- {
- return QueueBack(&obj->q2);//调用了取队尾的数据的函数
- }
- }
6.判创建的MyStack结构体q1和q2地址是不是空,如果是空的则标题2创建结构体失败了molloc也失败了
- bool myStackEmpty(MyStack* obj) {
- return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
- }
7.程序结束销毁所有建立的空间避免内存泄漏

- //释放数据
- void myStackFree(MyStack* obj) {
- QueueDestroy(&obj->q1);
- QueueDestroy(&obj->q2);
- free(obj);
- }