这次来记录下链表和队列的操作
首先在头文件中声明节点
typedef enum
{
STATE_SUCCESS = 0x00U,
STATE_ERROR = 0x01U,
STATE_BUSY = 0x02U,
}Status_e;
typedef struct Node
{
int data;
struct Node * next;
}Node;
typedef struct Node * Linklist; /* 定义 LinkList */
建立链表和删除链表
/**
* @brief 随机产生n个元素的值,建立带表头节点的单链表L 尾插法.
* @param L : 链表指针
* @param n : 创建的节点数
* @note None
* @retval status
*/
Status_e CreatListTail(Linklist *L ,int n)
{
Linklist P,r;
int i;
*L = (Linklist)malloc(sizeof(Node)); //整个表 创建了头节点,L指向头节点
r = *L;
for(i=0;i<n;i++)
{
P=(Linklist)malloc(sizeof(Node)); //生成新节点
P->data = i;
r->next = P;
r = P;
}
r->next = NULL;
return STATE_SUCCESS ;
}
/**
* @brief 删除一个链表
* @param L : 链表指针
* @note None
* @retval status
*/
Status_e DeleteListTail(Linklist *L )
{
Linklist P,Q;
P = (*L)->next; /* P指向第一个节点 */
while(P)
{
Q = P->next;
free(P);
P = Q;
}
(*L)->next = NULL;
return STATE_SUCCESS ;
}
插入节点和删除节点
/**
* @brief 在L链表中第i个位置之前插入新的元素,L的长度+1
* @param L : 链表指针
* @param i : 在第几个位置
* @param e : 要插入的元素
* @note None
* @retval status
*/
Status_e InsertListNode(Linklist *L,int i, int e)
{
int j;
Linklist P,S;
P = *L;
j = 1;
while(P && j<i) //找到要插入的那个节点
{
P = P->next;
j++;
}
if(!P&&j>i)
{
return STATE_ERROR ;
}
S = (Linklist)malloc(sizeof(Node));
S->data = e; //赋值
S->next = P->next;
P->next = S;
return STATE_SUCCESS ;
}
/**
* @brief 在L链表中第i个位置删除节点,L的长度-1
* @param L : 链表指针
* @param i : 在第几个位置
* @param e : 删除节点的元素
* @note None
* @retval status
*/
Status_e DeleteListNode(Linklist *L,int i, int *e)
{
int j;
Linklist P,Q;
P = *L;
j = 1;
while(P && j<i) //找到要插入的那个节点
{
P = P->next;
j++;
}
if(!P && j>i)
{
return STATE_ERROR ;
}
Q = P->next; //单独设置一个指针指向被删除结点,以防丢失
P->next = Q->next; //删除某个结点的方法就是更改前一个结点的指针域
free(Q); //手动释放该结点
return STATE_SUCCESS ;
}
接下来是队列的操作
声明队列
#define MaxNum 5
typedef struct
{
int dataBuffer[MaxNum];
uint8_t front;
uint8_t rear;
}SqQueue;
/**
* @brief 初始化一个队列
* @param Pqueue : 队列指针
* @note None
* @retval status
*/
Status_e InitQueue(SqQueue * Pqueue)
{
Pqueue->front = 0;
Pqueue->rear = 0;
return STATE_SUCCESS ;
}
/**
* @brief 入队 将元素e放到队列中
* @param Pqueue : 队列指针
* @param e : 入队列的元素
* @note None
* @retval status
*/
Status_e EnQueue(SqQueue * Pqueue,int e)
{
if((Pqueue->rear+1)%MaxNum == Pqueue->front) //队列满
{
return STATE_ERROR ;
}
else //队列空
{
Pqueue->dataBuffer[Pqueue->rear] = e;
Pqueue->rear = (Pqueue->rear + 1) % MaxNum;
}
return STATE_SUCCESS ;
}
/**
* @brief 出队
* @param Pqueue : 队列指针
* @param e:
* @note none
* @retval status
*/
Status_e DeQueue(SqQueue * Pqueue,int *e)
{
if(Pqueue->front == Pqueue->rear) //队列空
{
return STATE_ERROR ;
}
else //
{
*e = Pqueue->dataBuffer[Pqueue->front];
Pqueue->front = (Pqueue->front + 1)%MaxNum;
}
return STATE_SUCCESS ;
}
接下来看下链表队列
先声明下
ypedef struct QNode
{
int data;
struct QNode* next;
}QNode,*Queueptr;
typedef struct
{
Queueptr front,rear;
}LinkQueue;
/**
* @brief 初始化链表队列
* @param q : 链表队列的指针
* @note None
* @retval status
*/
Status_e InitListQueue(LinkQueue * q)
{
// 分配内存空间,建立头结点
q->front = q->rear = (Queueptr)malloc(sizeof(QNode));
//判断内存是否失败
if(!q->front)
{
return STATE_ERROR ;
}
else
{
// 指针域指向空
q->front->next = NULL;
}
return STATE_SUCCESS ;
}
//入链表队列
Status_e InsertListQueue(LinkQueue * q,int e)
{
Queueptr p;
p = (Queueptr)malloc(sizeof(QNode)); //生成新的节点
if (p == NULL) //判断内存是否失败
{
return STATE_ERROR ;
}
p->data = e; //赋值给新节点
p->next = NULL; // 新节点的指针域为空
/* 尾指针后移,指向新节点 */
q->rear->next = p ;
q->rear=p;
return STATE_SUCCESS ;
}
// 出链表队列
Status_e OutListQueue(LinkQueue * q,int* e)
{
Queueptr p; //临时节点
p = q->front->next;
*e = p->data;
/* 将队首移动到第二个结点,第一个结点出队 */
q->front->next = p->next;
/* 判断队列是否只有一个结点,只有一个结点则出队后队首队尾指向同一结点 */
if(q->rear == p)
{
q->rear =q->front;
}
free(p);
return STATE_SUCCESS ;
}