单链表的实现
头文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLNDataType;
// Single List
typedef struct SListNode
{
SLNDataType val;
struct SListNode* next;
}SLNode;
// 打印
void SLTPrint(SLNode* phead);
// 尾增
void SLTPushBack(SLNode** pphead, SLNDataType x);
// 头增
void SLTPushFront(SLNode** pphead, SLNDataType x);
// 尾删
void SLTPopBack(SLNode** pphead);
// 头删
void SLTPopFront(SLNode** pphead);
// 查询
SLNode* SLTFind(SLNode* phead, SLNDataType x);
// pos之前插入
void SLTInsert(SLNode** pphead, SLNode* pos, SLNDataType x);
// 删除pos位置
void SLTErase(SLNode** pphead, SLNode* pos);
// 销毁
void SLTDestroy(SLNode** pphead);
接口实现
#define _CRT_SECURE_NO_WARNINGS
#include"987.h"
SLNode* CreateNode(SLNDataType x)
{
SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
if (newnode == NULL)
{
perror("malloc");
}
newnode->val = x;
newnode->next = NULL;
return newnode;
}
void SLTPrint(SLNode* phead)
{
SLNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->val);
cur = cur->next;
}
printf("NULL\n");
}
void SLTPushBack(SLNode** pphead, SLNDataType x)
{
assert(pphead);
SLNode* cur = CreateNode(x);
if (*pphead != NULL)
{
SLNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = cur;
}
else
{
*pphead = cur;
}
}
void SLTPushFront(SLNode** pphead, SLNDataType x)
{
assert(pphead);
SLNode* cur = CreateNode(x);
cur->next = *pphead;
*pphead = cur;
}
void SLTPopBack(SLNode** pphead)
{
assert(pphead);
assert(*pphead);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLNode* tail = *pphead;
while (tail->next->next != NULL)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
void SLTPopFront(SLNode** pphead)
{
assert(pphead);
assert(*pphead);
SLNode* cur = (*pphead)->next;
free(*pphead);
*pphead = cur;
}
SLNode* SLTFind(SLNode* phead, SLNDataType x)
{
SLNode* tail = phead;
while (tail)
{
if (tail->val == x)
{
return tail;
}
tail = tail->next;
}
return NULL;
}
void SLTInsert(SLNode** pphead, SLNode* pos, SLNDataType x)
{
assert(pphead);
assert((*pphead && pos) || (!(*pphead) && !pos));
SLNode* newnode = CreateNode(x);
if (*pphead == pos)
{
*pphead = newnode;
}
else
{
SLNode* tail = *pphead;
while (tail->next != pos)
{
tail = tail->next;
}
tail->next = newnode;
}
newnode->next = pos;
}
void SLTErase(SLNode** pphead, SLNode* pos)
{
assert(*pphead);
assert(pphead);
assert(pos);
if (*pphead == pos)
{
*pphead = pos->next;
}
else
{
SLNode* tail = *pphead;
while (tail->next != pos)
{
tail = tail->next;
}
tail->next = tail->next->next;
}
free(pos);
pos = NULL;
}
void SLTDestroy(SLNode** pphead)
{
assert(pphead);
SLNode* cur = *pphead;
SLNode* next;
while (cur)
{
next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
测试
#define _CRT_SECURE_NO_WARNINGS
#include"987.h"
void test()
{
SLNode* phead = NULL;
SLTPushBack(&phead, 1);
SLTPushBack(&phead, 2);
SLTPushBack(&phead, 3);
SLTPrint(phead);
SLTPushFront(&phead, 0);
SLTPrint(phead);
SLNode* cur = SLTFind(phead, 0);
SLTInsert(&phead, cur, 5);
SLTPrint(phead);
SLTErase(&phead, cur);
SLTPrint(phead);
SLTDestroy(&phead);
SLTPrint(phead);
}
int main()
{
test();
return 0;
}
顺序表的实现
头文件
#pragma once
// SeqList.h
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
int size;
int capacity;
}SeqList;
// 对数据的管理:增删查改
// 初始化
void SeqListInit(SeqList* ps);
// 销毁顺序表
void SeqListDestroy(SeqList* ps);
// 打印顺序表
void SeqListPrint(SeqList* ps);
// 检查容量
void SLCheckCapacity(SeqList* ps);
// 尾插
void SeqListPushBack(SeqList* ps, SLDateType x);
// 头插
void SeqListPushFront(SeqList* ps, SLDateType x);
// 头删
void SeqListPopFront(SeqList* ps);
// 尾删
void SeqListPopBack(SeqList* ps);
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);
接口实现
#define _CRT_SECURE_NO_WARNINGS
#include "980.h"
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SeqListDestroy(SeqList* ps)
{
assert(ps);
if(ps->a != NULL)
{
free(ps->a);
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SLCheckCapacity(SeqList* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDateType* tmp = realloc(ps->a, sizeof(ps->capacity) * newcapacity);
if (tmp == NULL)
{
perror("realloc");
}
ps->a = tmp;
ps->capacity = newcapacity;
}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
SLCheckCapacity(ps);
int end = ps->size;
while (end > 0)
{
ps->a[end] = ps->a[end - 1];
end--;
}
ps->a[end] = x;
ps->size++;
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
assert(ps->size > 0);
ps->size--;
}
void SeqListPopFront(SeqList* ps)
{
assert(ps);
assert(ps->size > 0);
int front = 0;
while (front < ps->size - 1)
{
ps->a[front] = ps->a[front + 1];
front++;
}
ps->size--;
}
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
assert(ps);
assert(pos <= ps->size && pos >= 0);
SLCheckCapacity(ps);
int end = ps->size - 1;
while (end > pos)
{
ps->a[end] = ps->a[end - 1];
end--;
}
ps->a[pos] = x;
ps->size++;
}
void SeqListErase(SeqList* ps, int pos)
{
assert(ps);
assert(pos < ps->size && pos >= 0);
while (pos < ps->size - 1)
{
ps->a[pos] = ps->a[pos + 1];
pos++;
}
ps->size--;
}
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
测试
#define _CRT_SECURE_NO_WARNINGS
#include "980.h"
void test(SeqList* ps)
{
SeqListInit(ps);
SeqListPushBack(ps, 1);
SeqListPushBack(ps, 2);
SeqListPushBack(ps, 3);
SeqListPrint(ps);
SeqListPushFront(ps, 0);
SeqListPushFront(ps, -1);
SeqListPrint(ps);
SeqListPopBack(ps);
SeqListPrint(ps);
SeqListPopFront(ps);
SeqListPrint(ps);
SeqListInsert(ps, 1, 5);
SeqListPrint(ps);
SeqListErase(ps, 1);
SeqListPrint(ps);
printf("%d", SeqListFind(ps, 3));
SeqListDestroy(ps);
}
int main()
{
SeqList ps;
test(&ps);
return 0;
}
循环双向链表的实现
头文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
// 带头+双向+循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{
LTDataType val;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
// 创建返回链表的头结点.
ListNode* ListCreate();
// 双向链表销毁
void ListDestory(ListNode* pHead);
// 双向链表打印
void ListPrint(ListNode* pHead);
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* pHead);
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* pHead);
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);
函数实现
#define _CRT_SECURE_NO_WARNINGS
#include "1026.h"
// 创建节点
ListNode* NodeCreate(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL)
{
perror("malloc");
}
newnode->next = NULL;
newnode->prev = NULL;
newnode->val = x;
return newnode;
}
// 创建返回链表的头结点.
ListNode* ListCreate()
{
ListNode* newnode = NodeCreate(-1);
newnode->next = newnode;
newnode->prev = newnode;
return newnode;
}
// 双向链表销毁
void ListDestory(ListNode* pHead)
{
assert(pHead);
ListNode* cur = pHead->next;
while (cur != pHead)
{
ListPopFront(pHead);
cur = pHead->next;
}
free(pHead);
pHead = NULL;
}
// 双向链表打印
void ListPrint(ListNode* pHead)
{
assert(pHead);
ListNode* cur = pHead->next;
while (cur->next != pHead)
{
printf("%d<=>", cur->val);
cur = cur->next;
}
printf("%d", cur->val);
printf("\n");
}
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* newnode = NodeCreate(x);
ListNode* tail = pHead->prev;
pHead->prev = newnode;
tail->next = newnode;
newnode->prev = tail;
newnode->next = pHead;
}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* newnode = NodeCreate(x);
ListNode* head = pHead->next;
pHead->next = newnode;
head->prev = newnode;
newnode->prev = pHead;
newnode->next = head;
}
// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
assert(pHead);
assert(pHead->next != pHead);
ListNode* tail = pHead->prev;
ListNode* newtail = tail->prev;
pHead->prev = newtail;
newtail->next = pHead;
free(tail);
tail = NULL;
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{
assert(pHead);
assert(pHead->next != pHead);
ListNode* head = pHead->next;
ListNode* newhead = head->next;
pHead->next = newhead;
newhead->prev = pHead;
free(head);
head = NULL;
}
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* cur = pHead->next;
while (cur != pHead)
{
if (cur->val == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* newnode = NodeCreate(x);
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* next = pos->next;
ListNode* prev = pos->prev;
prev->next = next;
next->prev = prev;
free(pos);
pos = NULL;
}
测试函数
#define _CRT_SECURE_NO_WARNINGS
#include"1026.h"
void test()
{
ListNode* pHead = ListCreate();
ListPushBack(pHead, 3);
ListPushBack(pHead, 4);
ListPushBack(pHead, 5);
ListPrint(pHead);
ListPushFront(pHead, 2);
ListPushFront(pHead, 1);
ListPushFront(pHead, 0);
ListPrint(pHead);
ListPopBack(pHead);
ListPrint(pHead);
ListPopFront(pHead);
ListPrint(pHead);
/*ListPopFront(pHead);
ListPrint(pHead);
ListPopFront(pHead);
ListPrint(pHead);
ListPopFront(pHead);
ListPrint(pHead);
ListPopFront(pHead);
ListPrint(pHead);*/
ListNode* node = ListFind(pHead, 3);
ListInsert(node, 7);
ListPrint(pHead);
//ListPopFront(pHead);
//ListPrint(pHead);
//ListPopFront(pHead);
//ListPrint(pHead);
//ListPopFront(pHead);
//ListPrint(pHead);
//ListPopBack(pHead);
//ListPrint(pHead);
ListErase(node);
ListPrint(pHead);
ListDestory(pHead);
}
int main()
{
test();
}
栈的实现
头文件
#include<assert.h>
#include<stdbool.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top; // 栈顶
int capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
函数实现
#define _CRT_SECURE_NO_WARNINGS
#include"1112.h"
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->capacity == ps->top)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* newstack = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
if (newstack == NULL)
{
perror("realloc fail");
return;
}
ps->a = newstack;
ps->capacity = newcapacity;
}
ps->a[ps->top] = data;
ps->top++;
}
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
测试
#define _CRT_SECURE_NO_WARNINGS
#include"1112.h"
void test()
{
Stack ps;
StackInit(&ps);
StackPush(&ps, 0);
StackPush(&ps, 1);
StackPush(&ps, 2);
StackPop(&ps);
StackPush(&ps, 3);
StackPush(&ps, 4);
while (!StackEmpty(&ps))
{
printf("%d ", StackTop(&ps));
StackPop(&ps);
}
StackDestroy(&ps);
}
int main()
{
test();
return 0;
}
队列的实现
头文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;
// 链式结构:表示队列
typedef struct QListNode
{
struct QListNode* next;
QDataType data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;
// 初始化队列
void QueueInit(Queue* q);
// 队尾入队列
void QueuePush(Queue* q, QDataType data);
// 队头出队列
void QueuePop(Queue* q);
// 获取队列头部元素
QDataType QueueFront(Queue* q);
// 获取队列队尾元素
QDataType QueueBack(Queue* q);
// 获取队列中有效元素个数
int QueueSize(Queue* q);
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);
函数实现
#define _CRT_SECURE_NO_WARNINGS
#include"1112.h"
// 初始化队列
void QueueInit(Queue* q)
{
assert(q);
q->head = q->tail = NULL;
q->size = 0;
}
// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
assert(q);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
return;
}
newnode->next = NULL;
newnode->data = data;
if (q->head == NULL)
{
q->head = newnode;
q->tail = newnode;
}
else
{
q->tail->next = newnode;
q->tail = newnode;
}
q->size++;
}
// 队头出队列
void QueuePop(Queue* q)
{
assert(q);
assert(q->head);
if (q->head->next == NULL)
{
q->head = q->tail = NULL;
q->size = 0;
}
else
{
QNode* next = q->head->next;
free(q->head);
q->head = next;
q->size--;
}
}
// 获取队列头部元素
QDataType QueueFront(Queue* q)
{
assert(q);
assert(q->head);
return q->head->data;
}
// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
assert(q);
assert(q->tail);
return q->tail->data;
}
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
assert(q);
return q->size;
}
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q)
{
assert(q);
return q->head == NULL;
}
// 销毁队列
void QueueDestroy(Queue* q)
{
assert(q);
QNode* cur = q->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
q->head = q->tail = NULL;
q->size = 0;
}
函数测试
#define _CRT_SECURE_NO_WARNINGS
#include"1112.h"
void test()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
QueuePush(&q, 5);
QueuePush(&q, 6);
printf("%d \n", QueueSize(&q));
while (QueueEmpty(&q) == 0)
{
printf("%d \n", QueueFront(&q));
QueuePop(&q);
}
QueueDestroy(&q);
}
int main()
{
test();
return 0;
}
用栈实现队列
直接套用上面写的栈
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top; // 栈顶
int capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->capacity == ps->top)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* newstack = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
if (newstack == NULL)
{
perror("realloc fail");
return;
}
ps->a = newstack;
ps->capacity = newcapacity;
}
ps->a[ps->top] = data;
ps->top++;
}
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
typedef struct {
Stack stpush;
Stack stpop;
} MyQueue;
MyQueue* myQueueCreate()
{
MyQueue* q = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&q->stpush);
StackInit(&q->stpop);
return q;
}
void myQueuePush(MyQueue* obj, int x)
{
StackPush(&obj->stpush, x);
}
int myQueuePop(MyQueue* obj)
{
int ret = myQueuePeek(obj);
StackPop(&obj->stpop);
return ret;
}
int myQueuePeek(MyQueue* obj)
{
if (StackEmpty(&obj->stpop))
{
while (!StackEmpty(&obj->stpush))
{
StackPush(&obj->stpop, StackTop(&obj->stpush));
StackPop(&obj->stpush);
}
}
return StackTop(&obj->stpop);
}
bool myQueueEmpty(MyQueue* obj)
{
return StackEmpty(&obj->stpush) && StackEmpty(&obj->stpop);
}
void myQueueFree(MyQueue* obj)
{
StackDestroy(&obj->stpush);
StackDestroy(&obj->stpop);
free(obj);
}
用队列实现栈
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;
// 链式结构:表示队列
typedef struct QListNode
{
struct QListNode* next;
QDataType data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;
// 初始化队列
void QueueInit(Queue* q)
{
assert(q);
q->head = q->tail = NULL;
q->size = 0;
}
// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
assert(q);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
return;
}
newnode->next = NULL;
newnode->data = data;
if (q->head == NULL)
{
q->head = newnode;
q->tail = newnode;
}
else
{
q->tail->next = newnode;
q->tail = newnode;
}
q->size++;
}
// 队头出队列
void QueuePop(Queue* q)
{
assert(q);
assert(q->head);
if (q->head->next == NULL)
{
q->head = q->tail = NULL;
q->size = 0;
}
else
{
QNode* next = q->head->next;
free(q->head);
q->head = next;
q->size--;
}
}
// 获取队列头部元素
QDataType QueueFront(Queue* q)
{
assert(q);
assert(q->head);
return q->head->data;
}
// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
assert(q);
assert(q->tail);
return q->tail->data;
}
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
assert(q);
return q->size;
}
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* q)
{
assert(q);
return q->head == NULL;
}
// 销毁队列
void QueueDestroy(Queue* q)
{
assert(q);
QNode* cur = q->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
q->head = q->tail = NULL;
q->size = 0;
}
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate()
{
MyStack* p = (MyStack*)malloc(sizeof(MyStack));
if (p == NULL)
{
perror("malloc fail");
return 1;
}
QueueInit(&p->q1);
QueueInit(&p->q2);
return p;
}
void myStackPush(MyStack* obj, int x)
{
if (QueueEmpty(&obj->q1))
{
QueuePush(&obj->q2, x);
}
else
{
QueuePush(&obj->q1, x);
}
}
int myStackPop(MyStack* obj)
{
Queue* emptyq = &obj->q1;
Queue* noemptyq = &obj->q2;
if (QueueEmpty(emptyq) == 0)
{
emptyq = &obj->q2;
noemptyq = &obj->q1;
}
while (QueueSize(noemptyq) > 1)
{
QueuePush(emptyq, QueueFront(noemptyq));
QueuePop(noemptyq);
}
int top = QueueFront(noemptyq);
QueuePop(noemptyq);
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);
}