栈和队列实现

806 words

介绍

栈是一种基础的数据结构,具有一些特定的性质,例如先进后出,压栈出栈等,对于这些性质,一般可以采用顺序表来实现,当然也可用链表

队列

队列具有的性质是先进先出,队尾进,队头出,这很符合链表的特性,所以我们采用单链表来实现队列

实现

头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#pragma once

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#include<stdbool.h>
#include<assert.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
bool StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);

函数实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include"stack.h"

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

void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
//realloc可以申请新的空间,也可以申请空间跟之前的连上
if (tmp == NULL)
{
perror("realloc fail\n");
return;
}//注意判断是否申请空间成功
ps->a = tmp;
ps->capacity = newcapacity;
}

ps->a[ps->top] = data;
ps->top++;
}

void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));

ps->top--;
}

bool StackEmpty(Stack* ps)
{
/*if (ps->top == 0)
return true;
else
return false;*/

return ps->top == 0;
//更加简洁明了
}

int StackSize(Stack* ps)
{
assert(!StackEmpty(ps));

return ps->top;
}
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));

return ps->a[ps->top - 1];
}

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

队列

头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once

#define _CRT_SECURE_NO_WARNINGS 1

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


// 链式结构:表示队列
typedef int QDataType;
typedef struct QListNode
{
struct QListNode* next;
QDataType data;
}QNode;

// 队列的结构
typedef struct Queue
{
QNode* phead;
QNode* ptail;
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
bool QueueEmpty(Queue* q);

// 销毁队列
void QueueDestroy(Queue* q);

函数实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include"queue.h"

void QueueInit(Queue* q)
{
assert(q);

q->phead = NULL;
q->ptail = NULL;
q->size = 0;
}

void QueuePush(Queue* q, QDataType data)
{
assert(q);

QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail\n");
return;
}

newnode->data = data;
newnode->next = NULL;
//判断队列是否为空,因为具体的代码不同
if (q->phead == NULL)
{
assert(q->ptail == NULL);
q->phead = q->ptail = newnode;
}
else
{
q->ptail->next = newnode;
q->ptail = newnode;
}
q->size++;
}

bool QueueEmpty(Queue* q)
{
assert(q);
return q->size == 0;
}
void QueuePop(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
if (q->phead->next == NULL)
{
free(q->phead);
q->phead = NULL;
q->ptail = NULL;

}
else
{
Queue* tmp = q->phead;
q->phead = q->phead->next;
free(tmp);
}
q->size--;
}

QDataType QueueFront(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));

return q->phead->data;
}

QDataType QueueBack(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));

return q->ptail->data;
}

int QueueSize(Queue* q)
{
return q->size;
}

void QueueDestroy(Queue* q)
{
assert(q);
QNode* cur = q->phead;
while (cur != NULL)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
q->phead = NULL;
q->ptail = NULL;
q->size = 0;
}
Comments