1.栈的概念

2.栈的结构

3.实现栈的基本操作

3.1栈的初始化

3.2压栈

3.3出栈

3.4 取栈顶元素

3.5 计算栈内元素个数

3.6栈的判空

3.7栈的销毁

4.源代码

4.1stack.c

4.2stack.h

4.3test.c

4.4效果

1.队列的概念

2.队列的结构

3.实现队列的基本操作

3-1结构体定义

3-2队列的初始化

3-3入队列

3-4出队列

3-5取队头元素

3-6取队尾元素

3-7队列判空

3-8队列长度

3-9队列销毁

4.源代码

4-1queue.c

4.2queue.h

4.3test.c

4.4效果图

# 3.实现栈的基本操作

## 3.1栈的初始化

1.  选择哪一个方式初始化top都可以，但是记得做到和后面的push等做到统一。
2.  建议再主函数内判空操作不直接自己if(ps->top==0)等，因为这个内部怎么实现的不一定是初始化为ps->top=0,而是建议调用函数去判空if(StackEmpty(&ST))
``````void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}``````

## 3.2压栈

``````void StackPush(Stack* ps, STDateType x)
{
assert(ps);

if(ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity*2;
STDateType* temp = (STDateType*)realloc(ps->a, sizeof(STDateType)*newcapacity);
if (temp == NULL)
{
perror("malloc fail.\n");
exit(-1);
}
ps->capacity = newcapacity;
ps->a = temp;
}
ps->a[ps->top] = x;
ps->top++;
}``````

## 3.3出栈

``````void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
``````

## 3.4 取栈顶元素

``````STDateType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}``````

## 3.5 计算栈内元素个数

``````int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}``````

## 3.6栈的判空

``````bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}``````

## 3.7栈的销毁

``````void StackDestory(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}``````

# 4.源代码

## 4.1stack.c

``````#define _CRT_SECURE_NO_WARNINGS 1
#include"stack.h"
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}

void StackPush(Stack* ps, STDateType x)
{
assert(ps);

if(ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity*2;
STDateType* temp = (STDateType*)realloc(ps->a, sizeof(STDateType)*newcapacity);
if (temp == NULL)
{
perror("malloc fail.\n");
exit(-1);
}
ps->capacity = newcapacity;
ps->a = temp;
}
ps->a[ps->top] = x;
ps->top++;
}

void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}

STDateType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}

int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}

bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}

void StackDestory(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}``````

## 4.2stack.h

``````#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>

typedef int STDateType;
typedef struct Stack
{
STDateType* a;
int top;
int capacity;
}Stack;

void StackInit(Stack* ps);

void StackPush(Stack* ps,STDateType x);

void StackPop(Stack* ps);

STDateType StackTop(Stack* ps);

bool StackEmpty(Stack* ps);

int StackSize(Stack* ps);

void StackDestory(Stack* ps);

``````

## 4.3test.c

``````#define _CRT_SECURE_NO_WARNINGS 1
#include"stack.h"

int main()
{
Stack ST;
StackInit(&ST);
StackPush(&ST, 1);
StackPush(&ST, 2);
StackPush(&ST, 3);
StackPush(&ST, 4);
STDateType top = StackTop(&ST);
printf("top:%d\n", top);
StackPop(&ST);
top = StackTop(&ST);
printf("top:%d\n", top);
int size = StackSize(&ST);
printf("size:%d\n", size);
StackDestory(&ST);
return 0;
}``````

# 3.实现队列的基本操作

## 3-1结构体定义

``````typedef int QDateType;
typedef struct QueueNode
{
struct QueueNode* next;
QDateType val;
}QueueNode;

typedef struct Queue
{
QueueNode* tail;
}Queue;``````

## 3-2队列的初始化

``````void QueueInit(Queue* ps)
{
assert(ps);
}
``````

## 3-3入队列

``````//入队列：尾插
void QueuePush(Queue* ps,QDateType x)
{
assert(ps);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
newnode->next = NULL;
newnode->val = x;
if (newnode == NULL)
{
perror("malloc fail.");
exit(-1);
}
if (ps->tail == NULL)
{
}
else
{
ps->tail->next = newnode;
ps->tail = ps->tail->next;
}
}``````

## 3-4出队列

``````void QueuePop(Queue* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
{
}
else
{

}
}
``````

## 3-5取队头元素

``````QDateType QueueFront(Queue* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
}
``````

## 3-6取队尾元素

``````QDateType QueueBack(Queue* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
return ps->tail->val;
}``````

## 3-7队列判空

``````bool QueueEmpty(Queue* ps)
{
assert(ps);
return ps->tail == NULL;
}``````

## 3-8队列长度

``````int QueueSize(Queue* ps)
{
assert(ps);
int size = 0;
while(cur)
{
++size;
cur = cur->next;
}
return size;
}``````

## 3-9队列销毁

``````void QueueDestory(Queue* ps)
{
assert(ps);
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
}
``````

# 4.源代码

## 4-1queue.c

``````#define _CRT_SECURE_NO_WARNINGS 1
#include"queue.h"

void QueueInit(Queue* ps)
{
assert(ps);
}

void QueueDestory(Queue* ps)
{
assert(ps);
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
}

//入队列：尾插
void QueuePush(Queue* ps,QDateType x)
{
assert(ps);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
newnode->next = NULL;
newnode->val = x;
if (newnode == NULL)
{
perror("malloc fail.");
exit(-1);
}
if (ps->tail == NULL)
{
}
else
{
ps->tail->next = newnode;
ps->tail = ps->tail->next;
}
}

void QueuePop(Queue* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
{
}
else
{

}
}

QDateType QueueFront(Queue* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
}

QDateType QueueBack(Queue* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
return ps->tail->val;
}

bool QueueEmpty(Queue* ps)
{
assert(ps);
return ps->tail == NULL;
}

int QueueSize(Queue* ps)
{
assert(ps);
int size = 0;
while(cur)
{
++size;
cur = cur->next;
}
return size;
}``````

## 4.2queue.h

``````#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>

typedef int QDateType;
typedef struct QueueNode
{
struct QueueNode* next;
QDateType val;
}QueueNode;

typedef struct Queue
{
QueueNode* tail;
}Queue;

void QueueInit(Queue* ps);

void QueuePush(Queue* ps, QDateType x);

void QueuePop(Queue* ps);

QDateType QueueFront(Queue* ps);

QDateType QueueBack(Queue* ps);

bool QueueEmpty(Queue* ps);

int QueueSize(Queue* ps);

void QueueDestory(Queue* ps);``````

## 4.3test.c

``````#define _CRT_SECURE_NO_WARNINGS 1
#include"queue.h"

int main()
{
Queue Q;
QueueInit(&Q);
QueuePush(&Q, 1);
QueuePush(&Q, 2);
QueuePush(&Q, 3);
QueuePush(&Q, 4);
QueuePush(&Q, 5);
QDateType ret1 = QueueFront(&Q);
printf("QueueFront:%d\n", ret1);
QDateType ret2 = QueueBack(&Q);
printf("QueueBack:%d\n", ret2);
int size = QueueSize(&Q);
printf("size：%d\n", size);
while (!QueueEmpty(&Q))
{
printf("%d", QueueFront(&Q));
QueuePop(&Q);
}
QueueDestory(&Q);
return 0;
}``````

## 4.4效果图

制作不易，敬请三连

https://blog.csdn.net/qq_64428099/article/details/126173181

Scroll to Top