栈的概念及结构
栈:一种特殊的线性表,只允许在固定的一端进行插入和删除操作。进行数据插入删除操作的一端称为栈顶,另一端称为栈底。栈中的元素遵循先进后出(后进先出)的原则
入栈:栈的插入操作叫做入栈/进栈/压栈。入数据在栈顶
出栈:栈的删除操作叫做出栈。出栈也在栈顶
栈的实现
Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<malloc.h>
#include<stdbool.h>
typedef int STDateType;
typedef struct Stack
{
STDateType* _array;//数组
size_t _top;//栈顶
size_t capcity;//容量
}Stack;
void StackInit(Stack* ps);
void StackDestroy(Stack* ps);
void StackPush(Stack* ps, STDateType x);
void StackPop(Stack* ps);
STDateType StackTop(Stack* ps);
size_t StackSize(Stack* ps);
bool StackEmpty(Stack* ps);
Stack.c
#include"Stack.h"
void StackInit(Stack* ps)
{
assert(ps);
ps->_array = NULL;
ps->_top = 0;
ps->capcity = 0;
}
void StackDestroy(Stack* ps)
{
assert(ps);
if (ps->_array != NULL)
{
free(ps->_array);
ps->_array = NULL;
ps->capcity = ps->_top = 0;
}
}
void StackPush(Stack* ps, STDateType x)
{
assert(ps);
if (ps->_top == ps->capcity)
{
size_t newcapcity = ps->capcity = 0 ? 2 : ps->capcity * 2;
ps->_array = (STDateType*)realloc(ps->_array, newcapcity * sizeof(STDateType));
ps->capcity = newcapcity;
}
ps->_array[ps->_top] = x;
ps->_top++;
}
void StackPop(Stack* ps)
{
assert(ps && ps->_top > 0);
--ps->_top;
}
STDateType StackTop(Stack* ps)
{
assert(ps && ps->_top > 0);
return ps->_array[ps->_top - 1];
}
size_t StackSize(Stack* ps)
{
assert(ps);
return ps->_top;
}
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top == 0;
}
队列的概念及结构
队列:只允许在一端进行插入操作,在另一端进行删除操作的特殊线性表。进行插入操作的一端为队尾,进行删除操作的一端为对头。队列遵循先进先出(后进后出)的原则。
队列的实现
Queue.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* _next;
QDataType _data;
}QueueNode;
typedef struct Queue
{
QueueNode* _front;
QueueNode* _tail;
}Queue;
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
QueueNode* BuyQueueNode(QDataType x);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
bool QueueEmpty(Queue* pq);
size_t QueueSize(Queue* pq);
Queue.c
#include"Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->_front = NULL;
pq->_tail = NULL;
}
void QueueDestroy(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->_front;
while (cur)
{
QueueNode* next = cur->_next;
free(cur);
cur = next;
}
pq->_front = pq->_tail = NULL;
}
QueueNode* BuyQueueNode(QDataType x)
{
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
newnode->_data = x;
newnode->_next = NULL;
return newnode;
}
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newnode = BuyQueueNode(x);
if (pq->_tail == NULL)
{
pq->_front = pq->_tail = newnode;
}
else
{
pq->_tail->_next = newnode;
pq->_tail = newnode;
}
}
void QueuePop(Queue* pq)
{
assert(pq && pq->_front);
QueueNode* next = pq->_front->_next;
free(pq->_front);
pq->_front = next;
if (pq->_front == NULL)
{
pq->_tail = NULL;
}
}
QDataType QueueFront(Queue* pq)
{
assert(pq && pq->_front);
return pq->_front->_data;
}
QDataType QueueBack(Queue* pq)
{
assert(pq && pq->_tail);
return pq->_tail->_data;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->_front == NULL;
}
size_t QueueSize(Queue* pq)
{
assert(pq);
size_t s = 0;
QueueNode* cur = pq->_front;
while (cur)
{
++s;
cur = cur->_next;
}
return s;
}