目录
本篇文章是在介绍栈和队列的相关概念及其用C语言的实现。
一、栈的概念
栈:栈是一种特殊的线性表,之允许在固定的一端进行插入和删除元素。进行数据的插入和删除的一端叫做栈顶,另一端叫做栈底。栈中的数据元素遵循先入后出的原则,也就是LIFO(Last In First Out)。
压栈:栈的插入操作叫做压栈也叫做入栈或是进栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈,出数据在栈顶。
二、栈的实现
栈的实现可以用链表或是数组,相比较而言数组的方式更优一些。
1、栈的结构体及其相关函数的声明
此栈的栈顶设计成栈顶元素的下一个位置。
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
//栈的初始化
void STInit(ST* pst);
//栈的销毁
void STDestroy(ST* pst);
//进栈
void STPush(ST* pst, STDataType x);
//出栈
void STPop(ST* pst);
//取栈顶元素
STDataType STTop(ST* pst);
//判断是否为空
bool STEmpty(ST* pst);
//栈中元素个数
int STSize(ST* pst);
2、相关函数的详细实现
(1)栈的初始化
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->capacity = 0;
pst->top = 0;
}
(2)栈的销毁
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->capacity = pst->top = 0;
}
(3)进栈
要设计成一个动态增长的栈,进栈时如果容量是0,初始化几个空间,如果进栈时栈满了,二倍增加。
void STPush(ST* pst, STDataType x)
{
if (pst->top == pst->capacity)
{
int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
STDataType* tmp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
pst->a = tmp;
pst->capacity = newCapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
(4)出栈
出栈时还要判断栈是否为空
void STPop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
pst->top--;
}
(5)取栈顶元素
因为栈顶设计成了栈顶元素的下一个位置,所以取栈顶元素就要top-1。
STDataType STTop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
return pst->a[pst->top - 1];
}
(6)栈中元素个数
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
三、队列的概念
队列也是一种线性结构,它是只允许在一端进行插入操作,在另一端进行删除操作的特殊线性表,队列遵顼先进先出的原则,也就是FIFO(First In First Out)。进行插入操作的一端叫做队尾,进行删除操作的一端叫做队头。
四、队列的实现
队列的实现也可以使用链表或是数组的方式,这里链表的方式更优,入队就是尾插,出队就是头删,正好符合队列的结构。
1、队列的结构体及其相关函数的声明
这里要设计两个结构体一个是链表的节点,一个是队列。
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* phead;
QNode* ptail;
int size;
}Queue;
//队列的初始化
void QueueInit(Queue* pq);
//队列的销毁
void QueueDestroy(Queue* pq);
//入队
void QueuePush(Queue* pq, QDataType x);
//出队
void QueuePop(Queue* pq);
//取对头元素
QDataType QueueFront(Queue* pq);
//取队尾元素
QDataType QueueBack(Queue* pq);
//队列大小
int QueueSize(Queue* pq);
//判空
bool QueueEmpty(Queue* pq);
2、相关函数的实现
(1)队列的初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = NULL;
pq->ptail = NULL;
pq->size = 0;
}
(2)队列的销毁
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->phead;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
(3)入队
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail!");
return;
}
newnode->data = x;
newnode->next = NULL;
if (pq->phead == NULL)
{
assert(pq->ptail == NULL);
pq->phead = pq->ptail = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
pq->size++;
}
(4)出队
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->phead->next == NULL)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
QNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
(5)取队头元素
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->phead->data;
}
(6)取队尾元素
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
(7)队列元素个数
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
(8)队列判空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->phead == NULL && pq->ptail == NULL;
}
以上就是本篇文章的所有内容,如有问题请批评指正。