栈
“栈”的认识
什么是栈?
栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。LIFO(Last In First Out)
图示
我为什么用数组实现栈?
栈既可以用数组也可以用链表实现,但相对比之下用数组更加便捷一点,因为它对尾部的删除我们只需要移动栈顶的下标就行,也就是减减。
在这里我们还是用动态开辟内存的方法实现。
C语言
stack.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;//动态实现
int _top;//有效的数据,栈顶
int _capacity;//容量的大小
}Stack;
void StackInit(Stack* ps);
void StackDestroy(Stack* ps);
void StackPush(Stack* ps, STDataType x);//压栈
void StackPop(Stack* ps);//出栈
STDataType StackTop(Stack* ps);
int StackEmpty(Stack* ps);
int StackSize(Stack* ps);
stack.c
#include"stack.h"
void StackInit(Stack* ps)
{
assert(ps);
ps->_a = NULL;
ps->_capacity = ps->_top = 0;
}
void StackPush(Stack* ps, STDataType x)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
int newcapacity = ps->_capacity == 0 ? 5 : ps->_capacity * 2;
STDataType* newstack = (STDataType*)realloc(ps->_a, newcapacity * sizeof(STDataType));
if (NULL == newstack)
{
printf("realloc error!");
exit(0);
}
ps->_capacity = newcapacity;
ps->_a = newstack;
}
ps->_a[ps->_top] = x;
ps->_top++;
}
void StackPop(Stack* ps)
{
assert(ps);
if (ps->_top == 0)
{
return;
}
ps->_top--;
}
STDataType StackTop(Stack* ps)
{
assert(ps);
if (ps->_top == 0)
{
return;
}
return ps->_a[ps->_top-1];
}
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top;
}
int StackSize(Stack* ps)
{
assert(ps);
return ps->_top;
}
void StackDestroy(Stack* ps)
{
assert(ps);
if (ps->_a != NULL)
{
free(ps->_a);
ps->_a = NULL;
ps->_capacity = ps->_top = 0;
}
}
void StackPrint(Stack* ps)
{
assert(ps);
if (ps->_top == 0)
{
return;
}
while (ps->_top != 0)
{
printf("%d--->", ps->_a[ps->_top-1]);
ps->_top--;
}
printf("NULL\n");
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"stack.h"
int main()
{
Stack ps;
StackInit(&ps);
StackPush(&ps, 2);
StackPush(&ps, 5);
StackPush(&ps, 8);
StackPush(&ps, 6);
StackPop(&ps);
printf("%d\n", StackTop(&ps));
printf("%d\n", StackSize(&ps));
printf("%d\n", StackEmpty(&ps));
StackPrint(&ps);
StackDestroy(&ps);
system("pause");
return 0;
}
队列
“队列”的认识
什么是队列?
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。FIFO(First In First Out)
图示
用单链表实现栈
队列也可以用数组和链表实现,在这里我们用链表效果会更好一些,因为数组的话要出数据的话要在队头出,数组就不会很方便。
C语言
Queue.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
typedef int QUDataType;
typedef struct QueueNode
{
QUDataType _data;
struct QueueNode* _next;
}QueueNode;
typedef struct Queue
{
QueueNode* _front;//队头
QueueNode* _rear;//队尾
}Queue;
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
QueueNode* BuyQueueNode(QUDataType x);
void QueuePush(Queue* pq, QUDataType x);
void QueuePop(Queue* pq);
QUDataType QueueFront(Queue* pq);
QUDataType QueueBack(Queue* pq);
int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
void TestQueue();
Queue.c
#include"Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->_front = pq->_rear = NULL;
}
QueueNode* BuyQueueNode(QUDataType x)
{
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
if (newnode == NULL)
{
printf("malloc error");
exit(0);
}
newnode->_data = x;
newnode->_next = NULL;
}
void QueuePush(Queue* pq, QUDataType x)
{
assert(pq);
QueueNode* newnode = BuyQueueNode(x);
if (pq->_front == NULL)
{
pq->_front = pq->_rear = newnode;
}
else
{
pq->_rear->_next = newnode;
pq->_rear = newnode;
}
}
void QueuePop(Queue* pq)
{
assert(pq && pq->_front);
QueueNode* next = pq->_front->_next;
if (pq->_front == pq->_rear)
{
pq->_rear = next;
}
free(pq->_front);
pq->_front = next;
}
QUDataType QueueFront(Queue* pq)
{
assert(pq && pq->_front);
return pq->_front->_data;
}
QUDataType QueueBack(Queue* pq)
{
assert(pq && pq->_front);
return pq->_rear->_data;
}
int QueueEmpty(Queue* pq)
{
assert(pq);
return pq->_front == NULL ? 0 : 1;
}
int QueueSize(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->_front;
size_t count = 0;
while (cur)
{
count++;
cur = cur->_next;
}
return count;
}
void QueueDestroy(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->_front;
while (cur)
{
QueueNode* next = cur->_next;
free(cur);
cur = next;
}
}
void TestQueue()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 5);
QueuePush(&q, 8);
QueuePop(&q);
printf("%d\n", QueueEmpty(&q));
printf("%d\n", QueueSize(&q));
printf("%d\n", QueueFront(&q));
printf("%d\n", QueueBack(&q));
while (q._front)
{
printf("%d-->", q._front->_data);
q._front = q._front->_next;
}
printf("NULL\n");
QueueDestroy(&q);
}
test.c
#include"Queue.h"
int main()
{
TestQueue();
system("pause");
return 0;
}