1.栈(先入后出,后入先出)—stack
- 栈顶入插入,栈顶删除
push:入栈
pop:出栈
top:获取栈顶元素
size:元素的个数
empty:判空
#include "stack.h"
#define DEFSTACKSIZE 100
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
void CheckCapacity(Stack* ps)
{
if (ps->size >= ps->capacity)
{
ps->capacity *= 2;
ps->array = (STDataType *)realloc(ps->array, ps->capacity * sizeof(STDataType));
}
}
//初始化栈
void StackInit(Stack* ps)
{
ps->array = (STDataType *)calloc(DEFSTACKSIZE, sizeof(STDataType));
ps->capacity = DEFSTACKSIZE;
ps->size = 0;
}
//入栈
void StackPush(Stack* ps, STDataType x)
{
CheckCapacity(ps);
ps->array[ps->size] = x;
ps->size++;
}
//出栈
void StackPop(Stack* ps)
{
if (ps->size == 0)
{
return;
}
ps->size--;
}
//获取栈顶元素
STDataType StackTop(Stack* ps)
{
if (ps->size == 0)
{
return (STDataType)0;
}
return ps->array[ps->size - 1];
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{
return ps->size == 0;
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
return ps->size;
}
// 销毁栈
void StackDestory(Stack* ps)
{
if (ps->array)
{
free(ps->array);
ps->array = NULL;
ps->size = 0;
ps->capacity = 0;
}
}
2.队列(先入先出,后入后出)—Queue
- 队尾插入,队头删除
#include "queue.h"
// 链式结构:表示队列
typedef struct QListNode
{
struct QListNode* _next;
QDataType _data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* _front;
QNode* _rear;
}Queue;
QueueNode * BuyQueueNode(QuDataType x)
{
QueueNode * cur = (QueueNode *)malloc(sizeof(QueueNode));
cur->_data = x;
cur->_next = NULL;
return cur;
}
// 初始化队列
void QueueInit(Queue* q)
{
q->_front = NULL;
q->_rear = NULL;
}
// 队尾入队列
void QueuePush(Queue* q, QuDataType x)
{
QueueNode * cur = BuyQueueNode(x);
if (q->_front == NULL)
{
q->_front = q->_rear = cur;
}
else
{
q->_rear->_next = cur;
q->_rear = cur;
}
}
// 队头出队列
void QueuePop(Queue* q)
{
if (q->_front == NULL)
{
return;
}
QueueNode* tmp = q->_front->_next;
free(q->_front);
q->_front = tmp;
}
// 获取队列头部元素
QuDataType QueueFront(Queue* q)
{
return q->_front->_data;
}
// 获取队列队尾元素
QuDataType QueueBack(Queue* q)
{
return q->_rear->_data;
}
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q)
{
return q->_front == NULL;
}
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
QListNode * cur;
int count = 0;
for (cur = q->_front; cur; cur = cur->_next)
{
count++;
}
return count;
}
// 销毁队列
void QueueDestory(Queue* q)
{
if (q->_front == NULL)
{
return;
}
while (q->_front)
{
QueuePop(q);
}
}
①环形队列(队尾,在插入时可以将已经有元素删除,腾出来的原队头空间利用)
插入时,队列头不动,队列尾向后移动一位(向队列头方向靠近)
删除时,队列尾不动,队列头向后移动一位(队列尾的方向)
3.链表
#include "SList.h"
typedef int SLTDateType;
typedef struct SListNode
{
SLTDateType data;
struct SListNode* next;
}SListNode;
// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x)
{
SListNode* node = (SListNode*)malloc(sizeof(SListNode));
node->data = x;
node->next = NULL;
return node;
}
// 单链表打印
void SListPrint(SListNode* plist)
{
SListNode* cur = plist;
while (cur)
//while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x)
{
SListNode* newnode = BuySListNode(x);
if (*pplist == NULL)
{
*pplist = newnode;
}
else
{
SListNode* tail = *pplist;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
// 单链表的尾删
void SListPopBack(SListNode** pplist)
{
SListNode* prev = NULL;
SListNode* tail = *pplist;
// 1.空、只有一个节点
// 2.两个及以上的节点
if (tail == NULL || tail->next == NULL)
{
free(tail);
*pplist = NULL;
}
else
{
while (tail->next)
{
prev = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prev->next = NULL;
}
}
// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x)
{
assert(pplist);
// 1.空
// 2.非空
SListNode* newnode = BuySListNode(x);
if (*pplist == NULL)
{
*pplist = newnode;
}
else
{
newnode->next = *pplist;
*pplist = newnode;
}
}
// 单链表头删
void SListPopFront(SListNode** pplist)
{
// 1.空
// 2.一个
// 3.两个及以上
SListNode* first = *pplist;
if (first == NULL)
{
return;
}
else if (first->next == NULL)
{
free(first);
*pplist = NULL;
}
else
{
SListNode* next = first->next;
free(first);
*pplist = next;
}
}
// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
SListNode* cur = plist;
while (cur)
{
if (cur->data == x)
return cur;
cur = cur->next;
}
return NULL;
}
//单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
assert(pos);
SListNode* next = pos->next;
// pos newnode next
SListNode* newnode = BuySListNode(x);
pos->next = newnode;
newnode->next = next;
}
//单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos)
{
assert(pos);
// pos next nextnext
SListNode* next = pos->next;
if (next != NULL)
{
SListNode* nextnext = next->next;
free(next);
pos->next = nextnext;
}
}
//销毁链表
void destroyList(SListNode &L) {
SListNode p = L;
while (p)
{
L = L->next;
// free(p);
delete(p);
P = L;
}
}
//清空链表
void clearList(SListNode &L) {
SListNode p;
while (L->next) {
p = L->next;
L->next = p->next;
//free(p);
delete(p);
}
}