栈和队列也是线性表的两种不同的形式,它们的实现方式和顺序表与链表类似,但是在逻辑结构上有一些差别。
栈
栈是一种特殊的线性表。栈的形式类似于弹夹,其只允许在固定的一端进行插入和删除元素操作。也就是说只允许在出口(弹夹顶部)插入元素(放入子弹)和删除元素(发射子弹)。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。
压栈:栈的插入操作叫做进栈/压栈/入栈,存入的数据在栈顶。
出栈:栈的删除操作叫做出栈。输出数据也在栈顶。
由于栈的这些特性,它的数据遵守先进后出或者后进先出的原则。
了解了栈的逻辑,再来看一下下面的问题:
若进栈序列为 1,2,3,4 ,进栈过程中可以出栈,则下列不可能的一个出栈序列是()
A 1,4,3,2
B 2,3,4,1
C 3,1,4,2
D 3,4,2,1
A:1入栈后出栈,然后2,3,4入栈,4,3,2出栈
B:1,2入栈,2出栈,3入栈后出栈,4入栈后出栈,最后1出栈
C:错误
D:1,2,3入栈,3出栈,4入栈,4,2,1出栈
栈的实现可以采用顺序表或者单向链表的方式:
顺序表的形式就是采用类似数组的方式,顺序表的开头作为栈底,它的尾部作为栈顶,每次入栈出栈的过程就相当于顺序表的尾插尾删的过程,由于顺序表有一个记录大小的size变量,可以直接访问到最后一个元素,因此可以很轻松的实现尾插和尾删,时间复杂度是O(1)。
链表的形式有单链表和双向链表两种,单链表有头去做栈顶效率更高,因为头插头删的时间复杂度是O(1)。如果用尾做栈顶,双向量表会更好一些,因为单链表的尾删时间复杂度为O(N)。
因此采用顺序表和链表的形式都没问题,但相比之下顺序表的形式效率更高一些,因为链表每次都需要开辟新的节点。
栈的顺序表形式实现
- 声明栈的内容
typedef int STDataType;
typedef struct Stack
{
STDataType* a;//存放数据的指针
int top;//用来记录栈顶的位置,类似于顺序表的size,
int capacity;//容量
}ST;
- 初始化
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);//申请空间
if (ps->a == NULL)//申请空间失败的情况
{
exit(-1);
}
ps->capacity = 4;
//top初始为0,top指向栈顶元素的下一个元素,top初识为-1,top指向栈顶的元素
ps->top = 0;
}
- 入栈
//入栈
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)//如果栈的空间已满则增容
{
STDataType* tmp = realloc(ps->a, ps->capacity * 2 * sizeof(STDataType));
if (tmp == NULL)//增容失败的情况
{
exit(-1);
}
else
{
ps->a = tmp;
ps->capacity *= 2;
}
}
ps->a[ps->top] = x;//在栈顶存放数据
ps->top++;
}
- 出栈
//出栈
void StackPop(ST* ps)
{
assert(ps);
ps->top--;
}
- 获取栈顶的元素
//获取栈顶的数据
STDataType StackTop(ST* ps)
{
assert(ps);
assert(ps->top>0);//由于top这里指向栈顶元素的下一个元素位置,因此top为0则代表没有元素
return ps->a[ps->top - 1];
}
- 获取数据的个数
//求数据的个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;//top记录了元素的个数
}
- 判断栈是否为空
//判断是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;//如果top为0则代表为空,此时返回true,否则返回false
}
- 销毁栈
//销毁
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
栈函数的调用情况:
栈的测试代码
- Stack.h
#pragma once
#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
//初始化
void StackInit(ST* ps);
//销毁
void StackDestory(ST* ps);
//入栈
void StackPush(ST* ps, STDataType x);
//出栈
void StackPop(ST* ps);
//取栈顶的数据
STDataType StackTop(ST* ps);
//求数据的个数
int StackSize(ST* ps);
//判断是否为空
bool StackEmpty(ST* ps);
- Stack.c
#include"Stack.h"
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = (STDataType*)malloc(sizeof(STDataType*) * 4);
if (ps->a == NULL)
{
exit(-1);
}
ps->capacity = 4;
//top初始为0,top指向栈顶元素的下一个元素,top初识为-1,top指向栈顶的元素
ps->top = 0;
}
//销毁
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
//入栈
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
STDataType* tmp = realloc(ps->a, ps->capacity * 2 * sizeof(STDataType*));
if (tmp == NULL)
{
exit(-1);
}
else
{
ps->a = tmp;
ps->capacity *= 2;
}
}
ps->a[ps->top] = x;
ps->top++;
}
//出栈
void StackPop(ST* ps)
{
assert(ps);
ps->top--;
}
//取栈顶的数据
STDataType StackTop(ST* ps)
{
assert(ps);
assert(ps->top>0);
return ps->a[ps->top - 1];
}
//求数据的个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
//判断是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
- test.c
#include"Stack.h"
void test()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
printf("%d ", StackTop(&st));
StackPop(&st);
printf("%d ", StackTop(&st));
StackPop(&st);
StackPush(&st, 3);
StackPush(&st, 4);
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
StackDestory(&st);
}
int main()
{
test();
return 0;
}
队列
队列与栈不同,队列是只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。
入队列:进行插入操作的一端称为队尾 ,出队列:进行删除操作的一端称为队头。
因此队列遵守先进先出的原则。
队列也可以使用顺序表和链表两种方式实现。
但是顺序表的头作为队头,出数据的时候相当于头删,时间复杂度为O(N)。
单链表的头删效率高,尾插的效率较低,但是如果使用一个头指针和尾指针指向单链表的头结点和尾结点,那么它出队和入队的时间复杂度为O(1),效率相对较高。
明白了队列的基本逻辑,就可以代码实现了,队列一般需要一个头指针和一个尾指针来指向队头和队尾的数据。
队列的单链表实现
- 声明队列
typedef int QDataType;
typedef struct QueueNode;
typedef struct QueueNode
{
struct QueueNode* next;//指向下一个节点
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;//头指针,指向第一个节点
QNode* tail;//尾指针,指向最后一个节点
}Queue;
- 定义一个队列变量
Queue q;
- 初始化
//初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
}
- 入队
/队尾入
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));//创建新的节点
if (newnode == NULL)//创建失败的情况
{
exit(-1);
}
newnode->data = x;//存入数据
newnode->next = NULL;//新创建的节点的指向的节点为空
if (pq->tail == NULL)//没有节点的情况
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;//原来的队尾指向新的节点
pq->tail = newnode;//队尾改成新的节点
}
}
- 出队
//队头出
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->head);
//如果只有一个节点,释放后就没有节点了
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;//头指针指向下一个节点
}
}
- 获取队头的数据
//取队头的数据
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->head);//防止没有节点的情况出现
return pq->head->data;
}
- 获取队尾的数据
//取队尾的数据
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->head);//防止没有节点的情况出现
return pq->tail->data;
}
- 获取数据的个数
//个数
int QueueSize(Queue* pq)
{
assert(pq);
int size = 0;
QNode* cur = pq->head;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}
- 判断是否为空
//判空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;//如果head为NULL则说明链表为空,返回true,否则返回false
}
- 销毁队列
//销毁
void QueueDestory(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;//保存下一个节点
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;//头尾指针都置为空
}
队列函数调用情况:
队列测试代码
- Queue.h
#pragma once
#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
#include<stdlib.h>
typedef int QDataType;
typedef struct QueueNode;
typedef struct QueueNode
{
struct QueueNode* next;//指向下一个节点
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;//头指针,指向第一个节点
QNode* tail;//尾指针,指向最后一个节点
}Queue;
//初始化
void QueueInit(Queue* pq);
//销毁
void QueueDestory(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);
- Queue.c
#include"Queue.h"
//初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
}
//销毁
void QueueDestory(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
//队尾入
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->tail == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
//队头出
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->head);
//如果只有一个节点,释放后就没有节点了
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;//头指针指向下一个节点
}
}
//取队头的数据
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->head);
return pq->head->data;
}
//取队尾的数据
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->head);
return pq->tail->data;
}
//个数
int QueueSize(Queue* pq)
{
assert(pq);
int size = 0;
QNode* cur = pq->head;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}
//判空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
- test.c
#include"Queue.h"
void test()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
printf("%d ", QueueFront(&q));
printf("%d ", QueueFront(&q));
printf("%d ", QueueFront(&q));
QueuePop(&q);
printf("%d ", QueueFront(&q));
printf("%d ", QueueFront(&q));
QueuePop(&q);
QueuePush(&q, 4);
QueuePush(&q, 5);
QueuePush(&q, 6);
while (!QueueEmpty(&q))
{
printf("%d ", QueueFront(&q));
QueuePop(&q);
}
QueueDestory(&q);
}
int main()
{
test();
return 0;
}