栈的实现
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#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); //初始化栈
void StackPush(Stack* ps, STDataType x); //入栈
void StackPop(Stack* ps); //出栈
STDataType StackTop(Stack* ps); //获取栈顶元素
int StackSize(Stack* ps); //获取栈中有效元素个数
bool StackEmpty(Stack* ps); //检测栈是否为空,如果不为空返回0,为空返回非0
void StackDestory(Stack* ps); //销毁栈
void StackInit(Stack* ps) //初始化栈
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackPush(Stack* ps, STDataType x) //入栈
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
perror("realloc::Stack");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(Stack* ps) //出栈
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
STDataType StackTop(Stack* ps) //获取栈顶元素
{
assert(ps);
assert(ps->top > 0);
STDataType num = ps->a[ps->top - 1];
return num;
}
int StackSize(Stack* ps) //获取栈中有效元素个数
{
assert(ps);
return ps->top;
}
bool StackEmpty(Stack* ps) //检测栈是否为空,如果不为空返回0,为空返回非0
{
assert(ps);
return ps->top == 0;
}
void StackDestory(Stack* ps) //销毁栈
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void Test1()
{
Stack st;
StackInit(&st);
StackPush(&st, 10);
StackPush(&st, 11);
StackPush(&st, 12);
StackPush(&st, 13);
StackPop(&st);
StackTop(&st);
printf("%d\n", StackTop(&st));
StackSize(&st);
printf("%d\n", StackSize(&st));
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
StackDestory(&st);
}
int main()
{
Test1();
return 0;
}
队列的实现
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int QDataType;
typedef struct QListNode
{
struct QListNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;
void QueueInit(Queue* q); //初始化队列
void QueuePush(Queue* q, QDataType x); //队尾入队列
void QueuePop(Queue* q); //队头出队列
QDataType QueueFront(Queue* q); //获取队列头部元素
QDataType QueueBack(Queue* q); //获取队列队尾元素
int QueueSize(Queue* q); //获取队列中有效元素个数
void QueueDestory(Queue* q); //销毁队列
bool QueueEmpty(Queue* q); //检测队列是否为空,如果为空返回非0结果,如果非空返回0
void QueueInit(Queue* q) //初始化队列
{
assert(q);
q->head = q->tail = NULL;
}
void QueuePush(Queue* q, QDataType x) //队尾入队列
{
assert(q);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("QNode::malloc");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (q->tail == NULL)
{
q->head = q->tail = newnode;
}
else
{
q->tail->next = newnode;
q->tail = newnode;
}
}
void QueuePop(Queue* q) //队头出队列
{
assert(q);
assert(!QueueEmpty(&q));
if (q->head->next == NULL)
{
free(q->head);
q->head = q->tail = NULL;
}
else
{
QNode* next = q->head->next;
free(q->head);
q->head = next;
}
}
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);
int count = 0;
QNode* cur = q->head;
while(cur)
{
count++;
cur = cur->next;
}
return count;
}
bool QueueEmpty(Queue* q) //检测队列是否为空,如果为空返回非0结果,如果非空返回0
{
assert(q);
return q->head == NULL;
}
void QueueDestory(Queue* q) //销毁队列
{
assert(q);
while (q->head)
{
QNode* next = q->head->next;
free(q -> head);
q->head = next;
}
q->head = q->tail = NULL;
}
void TestQueue()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
while (!QueueEmpty(&q))
{
QueueFront(&q);
printf("%d ", QueueFront(&q));
QueuePop(&q);
}
printf("\n");
QueuePush(&q, 1);
QueuePush(&q, 2);
QueueBack(&q);
printf("%d\n", QueueBack(&q));
QueueSize(&q);
printf("%d\n", QueueSize(&q));
QueueDestory(&q);
}
int main()
{
TestQueue();
return 0;
}
🙇感谢大家的阅读,如有错误请指出,我们下次再见。