目录
1、栈
1.1栈的概念以及结构
栈:是一种特殊的线性表,它只允许在固定的一端进行插入和删除操作。数据插入和删除操作的一端为栈顶,另一端为栈底。栈中的数据遵循先进先出(LIFO)原则。
说明:这里提到的栈,是我们自己建立的一种数据结构,并不是内存区域中的栈区,虽然名字都叫做栈,但是两者是不一样的。
进栈出栈的过程如下图:
1.2栈的实现
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
下面是定长的静态栈结构,实际中一般不实用,所以我们主要实现下面的支持动态增长的栈。
typedef int STDataType;
#define N 10
typedef struct Stack
{
STDataType a[N];
int top; //栈顶
}Stack;
支持动态增长的栈:
typedef int STDataType;
typedef struct Stack
{
STDataType* st;
int top; //栈顶
int capacity; //容量
}Stack;
stack.h代码
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* st;
int top; //栈顶
int capacity; //容量
}Stack;
void StackInit(Stack* ps);//初始化
void StackDestroy(Stack* ps);//销毁
void StackPush(Stack* ps, STDataType x);//入栈
void StackPop(Stack* ps); //出栈
bool StackEmpty(Stack* ps); //判空
size_t StackSize(Stack* ps); //有效元素的个数
STDataType StackTop(Stack* ps); //获取栈顶元素
stack.c代码实现
#include "stack.h"
Stack* stackCreate(int Cpacity)
{
Stack* ret = malloc(sizeof(Stack));
ret->st = malloc(sizeof(int) * Cpacity);
ret->top = 0;
ret->capacity = Cpacity;
return ret;
}
void StackInit(Stack* ps) //初始化
{
assert(ps);
ps->st = NULL;
ps->capacity = 0;
ps->top = 0;
}
void StackDestroy(Stack* ps)//销毁
{
assert(ps);
free(ps->st);
ps->st = NULL;
ps->capacity = ps->top = 0;
}
void StackPush(Stack* ps, STDataType x)//入栈
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
ps->st =(STDataType*) realloc(ps->st, newCapacity*sizeof(STDataType));
if (ps->st == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->capacity = newCapacity;
}
ps->st[ps->top] = x;
ps->top++;
}
void StackPop(Stack* ps) //出栈
{
assert(ps);
assert(ps->top > 0);
--ps->top;
}
bool StackEmpty(Stack* ps) //判空
{
assert(ps);
return ps->top == 0;
}
size_t StackSize(Stack* ps)//有效元素的个数
{
assert(ps);
return ps->top;
}
STDataType StackTop(Stack* ps) //获取栈顶元素
{
assert(ps);
assert(ps->top > 0);
return ps->st[ps->top - 1];
}
测试代码:
#include "stack.h"
void Test0()
{
Stack st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
StackPush(&st, 4);
printf("%d\n", StackSize(&st));
while (!StackEmpty(&st))
{
printf("%d", StackTop(&st));
StackPop(&st);
}
printf("\n");
}
int main()
{
Test0();
system("pause");
return 0;
}
结果显示:
2、队列
2.1队列的概念及结构
队列:是只允许在一端进行插入数据操作,在另一端进行删除操作的特殊线性表,队列具有先进先出FIFO。进行插入操作的一端称为队尾,进行删除操作的一端称为队头。
出队入队的过程如下图:
2.2队列的实现
队列也可以用数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。
queue.h代码实现:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
QDataType data;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq,QDataType x);
void QueuePop(Queue* pq);
bool QueueEmpty(Queue* pq);
size_t QueueSize(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
queue.c代码实现:
#include "Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
newnode->data = x;
newnode->next = NULL;
if (pq->tail == NULL)
{
assert(pq->head == NULL);
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->head && pq->tail);
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL && pq->tail == NULL;
}
size_t QueueSize(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
size_t size = 0;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->head);
return pq->head->data;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->tail);
return pq->tail->data;
}
测试代码:
#include "Queue.h"
void Test0()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
printf("%d\n", QueueFront(&q));
QueuePop(&q);
QueuePush(&q, 3);
QueuePush(&q, 4);
while (!QueueEmpty(&q))
{
printf("%d", QueueFront(&q));
QueuePop(&q);
}
printf("\n");
}
int main()
{
Test0();
system("pause");
return 0;
}
结果显示: