栈和队列基本操作—数据结构
1、栈的基本操作
栈:一种特殊的线性表,只允许在固定的一端进行元素的插入和删除操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵循先进先出的原则
头文件:Stack.h
#pragma once
typedef int SDataType;
typedef struct Stack
{
SDataType* _array;
int _capacity;
int _top; //标记栈顶
}Stack;
void _CheckCapacity(Stack* ps);
void StackPrint(Stack* ps);
void StackInit(Stack* ps);
void StackPush(Stack* ps, SDataType data);
void StackPop(Stack* ps);
SDataType StackTop(Stack* ps);
int StackSize(Stack* ps);
int StackEmpty(Stack* ps);
void StackDestory(Stack* ps);
源文件:Stack.c
#include "Stack.h"
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>
void _CheckCapacity(Stack* ps)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
//开辟新空间
int newcapacity = ps->_capacity*2;
SDataType* pTemp = (SDataType*)malloc(sizeof(SDataType)*newcapacity);
if (NULL == pTemp)
{
assert(0);
return;
}
//拷贝元素
memcpy(pTemp, ps->_array, ps->_top*sizeof(SDataType));
//释放旧空间
free(ps->_array);
ps->_array = pTemp;
ps->_capacity = newcapacity;
}
}
void StackPrint(Stack* ps)
{
assert(ps);
int i = 0;
for (i = 0; i < ps->_top ; i++)
{
printf("%d ", ps->_array[i]);
}
printf("\n");
}
void StackInit(Stack* ps)
{
assert(ps);
ps->_array = (SDataType*)malloc(sizeof(SDataType)*3);
if (NULL == ps->_array)
{
assert(0);
return;
}
ps->_capacity =3;
ps->_top = 0;
}
void StackPush(Stack* ps, SDataType data)//尾插
{
assert(ps);
_CheckCapacity(ps);//扩容
ps->_array[ps->_top] = data;
ps->_top++;
}
void StackPop(Stack* ps)//尾删
{
assert(ps);
if (0 == ps->_top)
return;
ps->_top--;
}
SDataType StackTop(Stack* ps)
{
assert(ps);
return ps->_array[ps->_top-1];
}
int StackSize(Stack* ps)
{
assert(ps);
return ps->_top;
}
int StackEmpty(Stack* ps)
{
assert(ps);
return 0 == ps->_top;
}
void StackDestory(Stack* ps)
{
assert(ps);
if (ps->_array)
{
free(ps->_array);
ps->_array = NULL;
ps->_capacity = 0;
ps->_top = 0;
}
}
测试文件:Test.c
#include "Stack.h"
#include <stdio.h>
void TestStack()
{
Stack s;
StackInit(&s);
// _CheckCapacity(&s);
StackPush(&s, 2);//尾插
StackPush(&s, 4);
StackPush(&s, 3);
StackPrint(&s);
//求栈顶元素,求有效元素的个数
printf("top = %d,size = %d\n", StackTop(&s), StackSize(&s));
StackPush(&s, 5);
StackPrint(&s);
//求栈顶元素,求有效元素的个数
printf("top = %d,size = %d\n", StackTop(&s), StackSize(&s));
StackPop(&s);//尾删
StackPrint(&s);
//求栈顶元素,求有效元素的个数
printf("top = %d,size = %d\n", StackTop(&s), StackSize(&s));
printf("empty = %d\n", StackEmpty(&s));//栈是否为空
StackDestory(&s);
}
int main()
{
TestStack();
return 0;
}
2、队列的基本操作
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。队列具有先进先出的特点。入队列:进行插入操作的一端为队尾;出队列:进行删除操作的一段成为队头。
头文件:Queue,h
#pragma once
typedef int QDataType;//用链式结构
typedef struct QListNode
{
struct QListNode* _pNext; //下一个结点的地址
QDataType data;
}QNode;
typedef struct Queue
{
QNode* _front;
QNode* _rear;
}Queue;
void QueueInit(Queue* q);//初始化
void QueuePush(Queue* q, QDataType data);
QNode* BuyNode(QDataType data);
void QueuePop(Queue* q);
QDataType QueueFront(Queue* q);
QDataType QueueBack(Queue* q);
int QueueSize(Queue* q);
int QueueEmpty(Queue* q);
void QueueDestory(Queue* q);
void QueuePrint(Queue* q);
源文件:Queue.c
#include "Queue.h"
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
void QueueInit(Queue* q) //初始化
{
assert(q);
q->_front = q->_rear = NULL;
}
QNode* BuyNode(QDataType data)
{
QNode* pNewNode = (QNode*)malloc(sizeof(QNode));
if (NULL == pNewNode)
{
assert(0);
return NULL;
}
pNewNode->_pNext = NULL;
pNewNode->data = data;
return pNewNode;
}
void QueuePush(Queue* q, QDataType data)
{
QNode* pNewNode = NULL;
assert(q);
pNewNode = BuyNode(data);
if (NULL == q->_front)
{
q->_front = q->_rear = pNewNode;
}
else
{
q->_rear->_pNext = pNewNode;
q->_rear = pNewNode;
}
}
void QueuePop(Queue* q)
{
assert(q);
//空队列
if (q->_front == NULL)
{
assert(0);
return;
}
//只有一个结点
else if (q->_front == q->_rear)
{
free(q->_front);
q->_front = q->_rear = NULL;
}
//结点数大于1
else
{
QNode* pCur = q->_front;
q->_front = pCur->_pNext;
free(pCur);
}
}
QDataType QueueFront(Queue* q)
{
assert(q);
return q->_front->data;
}
QDataType QueueBack(Queue* q)
{
assert(q);
return q->_rear->data;
}
int QueueSize(Queue* q)
{
assert(q);
QNode* pCur = NULL;
int count = 0;
pCur = q->_front;
while (pCur)
{
count ++ ;
pCur = pCur->_pNext;
}
return count;
}
int QueueEmpty(Queue* q)
{
assert(q);
return NULL == q->_front;
}
void QueueDestory(Queue* q)
{
assert(q);
QNode* pCur = NULL;
pCur = q->_front;
while (pCur)
{
q->_front = pCur->_pNext;
free(pCur);
pCur = q->_front;
}
q->_front = q->_rear = NULL;
}
void QueuePrint(Queue* q)
{
assert(q);
QNode* pCur = NULL;
pCur = q->_front;
while (pCur)
{
printf("%d ", pCur->data);
pCur = pCur->_pNext;
}
printf("\n");
}
测试文件:Test.c
#include "Queue.h"
#include <stdio.h>
void TestQueue()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
QueuePrint(&q);
printf("front = %d\n", QueueFront(&q));
printf("rear = %d\n", QueueBack(&q));
printf("size = %d\n", QueueSize(&q));
printf("\n");
QueuePop(&q);//队头出队
QueuePrint(&q);
printf("front = %d\n",QueueFront(&q));
printf("rear = %d\n",QueueBack(&q));
printf("size = %d\n",QueueSize(&q));
printf("empty = %d\n",QueueEmpty(&q));
QueueDestory(&q);
}
int main()
{
TestQueue();
return 0;
}