-
尝试实现链式队列
结构体定义
typedef int data_t;
typedef struct lkq
{
union
{
int len;
data_t data;
};
struct lkq *next;
} Node_t;
typedef struct
{
Node_t *front;
Node_t *rear;
} Lq_t;
创建链队
Lq_t *creat_lkque()
{
Node_t *H = malloc(sizeof(Node_t));
if (NULL == H)
{
puts("创建头节点失败!");
return NULL;
}
H->len = 0;
H->next = NULL;
Lq_t *Q = malloc(sizeof(Lq_t));
if (NULL == Q)
{
puts("创建节点指针失败!");
return NULL;
}
Q->front = Q->rear = H;
return Q;
}
创建节点
Node_t *creat_node(data_t data)
{
Node_t *N = malloc(sizeof(Node_t));
if (NULL == N)
{
puts("创建节点失败!");
return NULL;
}
N->data = data;
N->next = NULL;
return N;
}
判空
int empty_lkque(Lq_t *Q)
{
if (NULL == Q)
{
puts("节点指针错误!");
return -1;
}
if (NULL == Q->front->next)
{
puts("链队列为空!");
return 1;
}
return 0;
}
入队
// 入队
int pash_lkque(Lq_t *Q, data_t data)
{
if (NULL == Q)
{
puts("节点指针错误!");
return -1;
}
Node_t *new = creat_node(data);
Q->rear->next = new;
Q->rear = new;
Q->front->len++;
return 0;
}
出队
// 出队
int pop_lkque(Lq_t *Q)
{
if (empty_lkque(Q))
return -1;
Node_t *del = Q->front->next;
Q->front->next = del->next;
free(del);
Q->front->len--;
return 0;
}
遍历
// 遍历
int show_lkque(Lq_t *Q)
{
if (empty_lkque(Q))
return -1;
Node_t *p = Q->front->next;
puts("-------------------------------------------------");
while (NULL != p)
{
printf("%d\t", p->data);
p = p->next;
}
printf("len=%d\n", Q->front->len);
puts("-------------------------------------------------");
return 0;
}
销毁
// 释放
int free_lkque(Lq_t **Q)
{
if (NULL == *Q)
{
puts("节点指针错误!");
return -1;
}
if (NULL == (*Q)->front->next)
{
free((*Q)->front); // 释放头节点
free(*Q); // 释放节点指针
*Q = NULL;
return 0;
}
Node_t *del = (*Q)->front;
while (del != NULL)
{
(*Q)->front = del->next;
free(del);
del = (*Q)->front;
}
free(*Q);
*Q = NULL;
}
主函数
#include "lkque.h"
int main(int argc, const char *argv[])
{
Lq_t *Q = creat_lkque();
for (int i = 1; i < 6; i++)
{
pash_lkque(Q, i * 11);
}
show_lkque(Q);
for (int i = 1; i < 6; i++)
{
pop_lkque(Q);
puts("出队后:");
show_lkque(Q);
}
free_lkque(&Q);
return 0;
}
y@DESKTOP-1DH5HNK:~/23071/数据结构/day6/lkqueue$ ./a.out
-------------------------------------------------
11 22 33 44 55 len=5
-------------------------------------------------
出队后:
-------------------------------------------------
22 33 44 55 len=4
-------------------------------------------------
出队后:
-------------------------------------------------
33 44 55 len=3
-------------------------------------------------
出队后:
-------------------------------------------------
44 55 len=2
-------------------------------------------------
出队后:
-------------------------------------------------
55 len=1
-------------------------------------------------
出队后:
链队列为空!
y@DESKTOP-1DH5HNK:~/23071/数据结构/day6/lkqueue$
2.使用递归实现程序:输入一个数,输出该数的每一位
int fun(int num)
{
if (num / 10 == 0)
{
printf("%d\n", num);
return 0;
}
printf("%d\n", num % 10);
return fun(num / 10);
}
int main(int argc, const char *argv[])
{
int num;
scanf("%d",&num);
fun(num);
return 0;
}
y@DESKTOP-1DH5HNK:~/23071/数据结构/day6$ ./a.out
121545
5
4
5
1
2
1
y@DESKTOP-1DH5HNK:~/23071/数据结构/day6$ gcc digui.c
y@DESKTOP-1DH5HNK:~/23071/数据结构/day6$ ./a.out
12233
3
3
2
2
1
y@DESKTOP-1DH5HNK:~/23071/数据结构/day6$