介绍
栈
栈是一种基础的数据结构,具有一些特定的性质,例如先进后出,压栈出栈等,对于这些性质,一般可以采用顺序表来实现,当然也可用链表
队列
队列具有的性质是先进先出,队尾进,队头出,这很符合链表的特性,所以我们采用单链表来实现队列
实现
栈
头文件
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);
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)); 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) {
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);
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; }
|