题目
用双向链表实现:将输入的数值按特定规则排序,格式为基数靠左且依次增大,偶数靠右且依次减少
如:固定链表各结点数据域分别为1 2 3 4 5 6 遍历输出 1 3 5 6 4 2
步骤
双向链表的使用步骤:
1、初始化链表:创建头结点(判断申请空间是否成功,memset初始化,然后让它的前后指针指向空)
2、创建新结点(判断申请空间是否成功,memset初始化,数据的赋值等等操作)
3、尾插法插入结点
4、完成特定任务
5、遍历链表,打印结果
移动结点
双向链表的结点移动,包含了断开结点和插入结点的操作
断开结点之后如果不用了,可以释放掉,即删除结点
代码示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
struct Node
{
int data;
struct Node *prev;
struct Node *next;
};
struct Node *init_list(void)
{
struct Node *head = (struct Node* )malloc(sizeof(struct Node));
if(head != NULL)
{