数据结构 – 栈和队列
一、栈
1.概念及结构
栈是一种特殊的线性表,只允许在特定的一端进行插入和删除操作。进行插入和删除的一端称为栈顶,另一端称为栈底。
栈遵循先入后出的原则。
栈一般可以使用数组或者链表实现,使用数组更优一些。
其结构定义如下:
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;//top指向栈顶元素加一的位置
int capacity;//容量
}ST;
2.接口实现
代码如下:
1.初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
2.销毁
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
3.判空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
4.打印
void StackPrint(ST* ps)
{
assert(ps);
int cur = 0;
while (cur < ps->top)
{
printf("%d ", ps->a[cur]);
cur++;
}
printf("\n");
}
5.入栈
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)//检查容量
{
int newcapcity = (ps->capacity == 0) ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapcity);
if (tmp == NULL)
{
perror("realloc");
return;
}
ps->a = tmp;
ps->capacity = newcapcity;
}
ps->a[ps->top] = x;
ps->top++;
}
6.出栈
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
7.栈顶元素
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
8.数据个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
3.特性总结
栈是一种先入后出的数据结构。
二、队列
1.概念及结构
队列是一种只允许在一端进行插入操作,在另一端进行删除操作的特殊线性表,具有先入先出的特性。进行插入操作的一端称为队尾,进行删除操作的一端称为队头。
队列可以用链表和数组实现,用链表更优一些。
其结构定义如下:
typedef int QDataType;
typedef struct QueueNode//队列节点
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;//队头指针
QNode* tail;//队尾指针
}Queue;
2.接口实现
代码如下:
1.初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}
2.判空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
3.打印
void QueuePrint(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
QNode* cur = pq->head;
while (cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
4.入队
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc");
return;
}
newnode->data = x;
newnode->next = NULL;
if (pq->tail == NULL)//若队列为空
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
5.出队
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
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;
}
}
6.队头数据
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
7.队尾数据
QDataType QueueTail(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
8.队列大小
int QueueSize(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
int size = 0;
while (cur != NULL)
{
++size;
cur = cur->next;
}
return size;
}
9.销毁
void QueueDestory(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur != NULL)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
3.特性总结
队列是一种遵循先入先出的数据结构。
总结
本文主要介绍了栈和队列的结构、实现和特点。