記事のディレクトリ
スタックとキュー
1.スタックの定義
スタックは、テーブルの最後でのみ挿入および削除操作に制限された線形テーブルです。
データの挿入と削除を許可する端をスタックの最上位と呼び、もう一方の端をスタックの最下位と呼び、データ要素のないスタックを空のスタックと呼びます。スタックは後入れ先出し(後入れ先出し)線形テーブルとも呼ばれます。
1.1スタックのシーケンシャルストレージ構造
スタックの実装は通常、配列またはリンクリストを使用して実装でき、配列の構造は比較的優れています。配列の最後にデータを挿入するコストが比較的小さいためです。
//支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; //栈顶下标
int _capacity;//容量
}Stack;
1.2動的配列の実装に基づくシーケンシャルスタック
//初始化
void StackInit(Stack* pst)
{
assert(pst);
pst->_a = malloc(sizeof(STDataType) * 4);
pst->_top = 0;
pst->_capacity = 4;
}
//销毁
void StackDestory(Stack* pst)
{
assert(pst);
free(pst->_a);
pst->_a = NULL;
pst->_top = pst->_capacity = 0;
}
//入栈
void StackPush(Stack* pst, STDataType x)
{
assert(pst);
//判断栈是否满了
if (pst->_top == pst->_capacity)
{
//增容
pst->_capacity *= 2;
STDataType* temp = realloc(pst->_a, sizeof(STDataType) * pst->_capacity);
if (temp == NULL)
{
printf("内存不足\n");
exit(-1);
}
else
{
pst->_a = temp;
}
}
//数据入栈
pst->_a[pst->_top] = x;
pst->_top++;
}
//出栈
void StackPop(Stack* pst)
{
assert(pst);
assert(pst->_top > 0);
--pst->_top;
}
//获取数据个数
int StackSize(Stack* pst)
{
assert(pst);
return pst->_top;
}
//返回1是空 返回0是非空
int StackEmpty(Stack* pst)
{
assert(pst);
//return pst->_top == 0 ? 1 : 0;
return !pst->_top;
}
//获取栈顶的数据
STDataType StackTop(Stack* pst)
{
assert(pst);
assert(pst->_top > 0);
return pst->_a[pst->_top - 1];
}
2.キューの定義
キューは、一方の端での挿入操作ともう一方の端での削除操作のみを許可する線形テーブルです。
キューは先入れ先出し(先入れ先出し)線形テーブルです。挿入が許可されたセグメントはキューの終わりと呼ばれ、削除が許可された終わりはキューの先頭と呼ばれます。
2.1キューの連鎖ストレージ構造
配列やリンクリストの構造でキューを実装することもできます。配列の構造を使用すると、配列の先頭にキューを出力する効率が低下するため、リンクリストの構造を使用することをお勧めします。 。
//链式结构表示队列
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* _next;
QDataType _data;
}QueueNode;
//队列的结构
typedef struct Queue
{
QueueNode* _head;//队头
QueueNode* _tail;//队尾
}Queue;
2.2単一リンクリストに基づくチェーンキュー
//初始化队列
void QueueInit(Queue* pq)
{
assert(pq);
pq->_head = pq->_tail = NULL;
}
//销毁队列
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->_head;
while (cur)
{
QueueNode* next = cur->_next;
free(cur);
cur = next;
}
pq->_head = pq->_tail = NULL;
}
//队尾入队
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
//申请新空间
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
if (newNode == NULL)
{
printf("内存不足\n");
exit(-1);
}
newNode->_data = x;
newNode->_next = NULL;
//判空
if (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);
//释放队头使其指向下一个结点
QueueNode* next = pq->_head->_next;
free(pq->_head);
pq->_head = next;
//判断只剩一个元素的时候
if (pq->_head == NULL)
{
pq->_tail = NULL;
}
}
//取队头元素
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;
}
//返回1是空 返回0是非空
int QueueEmpty(Queue* pq)
{
assert(pq);
return pq->_head == NULL ? 1 : 0;
}
//获取数据个数
int QueueSize(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->_head;
int size = 0;//计数
while (cur)
{
++size;
cur = cur->_next;
}
return size;
}