栈和队列学习笔记
栈
**定义:**一种线性数据结构,线性表的一种,在规定栈顶后,数据的出入皆由栈顶出入,栈可以使用链表或者数组进行实现。
C语言中的实现:
这里使用数组进行实现
- 头文件声明:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
- 函数的实现:
#include "stack.h"
void StackInit(Stack* ps){
assert(ps);
ps->_a=NULL;
ps->_capacity=0;
ps->_top=0; //注意这里的top指向的是最后元素的下一位下标,因此空时为0
//ps->_top=-1 这里是指向最后元素时的情况
};
void StackPush(Stack* ps, STDataType data){
assert(ps);
if(ps->_capacity==ps->_top){
int NewCapacity =ps->_capacity==0 ? 4 : ps->_capacity*2;
STDataType *tmp=(STDataType *) realloc(ps->_a,NewCapacity*sizeof(STDataType));
if(tmp==NULL){
perror("realloc fail");
return;
}
else{
ps->_a=tmp;
ps->_capacity=NewCapacity;
}
}
ps->_a[ps->_top]=data;
ps->_top++;
};
void StackPop(Stack* ps){
assert(ps);
assert(ps->_top>0);
ps->_top--;
};
STDataType StackTop(Stack* ps){
assert(ps);
assert(ps->_top > 0);
return ps->_a[ps->_top-1];
}
int StackSize(Stack* ps){
assert(ps);
return ps->_top;
}
int StackEmpty(Stack* ps){
assert(ps);
int ret;
if(ps->_top!=0)ret =-1;
else ret=0;
return ret;
}
void StackDestroy(Stack* ps){
assert(ps->_a);
ps->_a=NULL;
ps->_top=ps->_capacity=0;
free(ps);
}
队列
**定义:**在一端进行插入数据操作,另一端进行删除数据操作的数据结构,插入的一端是队尾,删除的一端是队首,具有先进先出的性质。
C语言中的实现:
头文件声明
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int QDataType;
typedef struct QListNode
{
struct QListNode* _next;
QDataType _data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* _front;
QNode* _rear;
int size;
}Queue;
// 初始化队列
void QueueInit(Queue* q);
// 队尾入队列
void QueuePush(Queue* q, QDataType data);
// 队头出队列
void QueuePop(Queue* q);
// 获取队列头部元素
QDataType QueueFront(Queue* q);
// 获取队列队尾元素
QDataType QueueBack(Queue* q);
// 获取队列中有效元素个数
int QueueSize(Queue* q);
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);
函数实现
#include "quene.h"
void QueueInit(Queue* q){
q->_front=q->_rear=NULL;
q->size=0;
}
void QueuePush(Queue* q, QDataType data){
assert(q);
QNode * newnode = (QNode*) malloc(sizeof (QNode));
if(newnode==NULL){
perror("malloc fail");
return;
}
newnode->_data=data;
newnode->_next=NULL;
if(q->_rear==NULL){
q->_rear=q->_front=newnode;
}
else{
q->_rear->_next=newnode;
q->_rear=q->_rear->_next;
}
q->size++;
}
void QueuePop(Queue* q){
assert(q);
assert(q->size!=0);
if(q->size==1){
free(q->_rear);
q->_front=q->_rear=NULL;
}
else{
QNode *next=q->_front->_next;
free(q->_front);
q->_front=next;
}
q->size--;
}
QDataType QueueFront(Queue* q){
assert(q);
assert(q->size!=0);
return q->_front->_data;
}
QDataType QueueBack(Queue* q){
assert(q);
assert(q->size!=0);
return q->_rear->_data;
}
int QueueSize(Queue* q){
assert(q);
return q->size;
}
int QueueEmpty(Queue* q){
assert(q);
if(q->size)return 0;
else return -1;
}
void QueueDestroy(Queue* q){
assert(q);
QNode *pcur=q->_front;
while (pcur){
QNode* next = pcur->_next;
free(pcur);
pcur=next;
}
q->size=0;
q->_front=q->_rear=NULL;
}