在这里插入代码片
```#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node* pre;//前驱
struct Node* next;//后继
}Node;
Node* tail = NULL;
// 初始化函数
Node* initList()
{
// 开辟空间,创建节点
Node* head = (Node*)malloc(sizeof(Node));
// 容错判断
if (head == NULL)
{
perror("init malloc error\n");
}
head->data = 0;
head->next = NULL;
head->pre = NULL;
return head;
}
// 头插法
void headInsert(Node* list, int data)
{
// 开辟空间,创建节点
Node* node = (Node*)malloc(sizeof(Node));
// 容错判断
if (node == NULL)
{
perror("headinsert malloc error\n");
return NULL;
}
// 接收数据
node->data = data;
node->next = list->next;
node->pre = list;//优美的代码:node-next, node-pre, list-next-pre, list-next,
if (list->next != NULL)
{
list->next->pre = node;//只能这么写了,如果将list->next = node提取出来,那么,list指向已经改变,这行代码将无效
list->next = node;
}
else
{
list->next = node;
}
list->data++;
}
// 任意位置插入
void insert(Node* list, int position, int data)
{
// 对位置合法性进行判断
if (position <= 0 || position > list->data + 1)
{
printf("位置超范围!\n");
return;
}
// 尾指针的复制品
Node* tail_cp = tail;
// 头指针的复制品
Node* head_cp = list;
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
// 对具体位置进行判断
// 如果是第一个位置或者最后一个直接插入
// 开辟空间,创建节点
if (list->next == NULL || position == list->data + 1)
{
node->next = tail->next;
node->pre = tail;
tail->next = node;
tail = tail->next;
list->data++;
}
else if (position <= (list->data / 2))//在前半段时,移动前面的指针
{
// 头指针移动到插入位置前一个
for (int i = 0; i < position; i++)
{
head_cp = head_cp->next;
}
// 插入操作
node->next = head_cp;
node->pre = head_cp->pre;
head_cp->pre->next = node;
head_cp->pre = node;
list->data++;
}
else//在后半段时,移动尾指针
{
// 移动次数:list->data - position
for (int i = 0; i < (list->data - position); i++)
{
tail_cp = tail_cp->pre;
}
// 插入操作
node->next = tail_cp;
node->pre = tail_cp->pre;
tail_cp->pre->next = node;
tail_cp->pre = node;
list->data++;
}
}
// 尾插法
void tailInsert(Node* list, int data)
{
// 开辟空间,创建节点
Node* node = (Node*)malloc(sizeof(Node));
// 接收数据
node->data = data;
// 找到尾节点
Node* move_ptr = list->next;
while (move_ptr->next != NULL)
{
move_ptr = move_ptr->next;
}
node->next = move_ptr->next;
move_ptr->next = node;
node->pre = move_ptr;
list->data++;
}
// 删除指定数据操作
void delete(Node* list, int data)
{
Node* pre = list;
Node* current = list->next;
while (current)
{
if (current->data == data)
{
pre->next = current->next;//调整前一个的后继
current->next->pre = pre;//调整后一个的前驱,使他们跨过current
free(current);
current = NULL;
list->data--;
printf("已删除!\n");
return;
}
pre = current;
current = current->next;
}
}
// 删除指定位置的数据
void deleteAny(Node* list, int post)
{
Node* pre = list;
Node* current = list->next;
if (post < 0 || post > list->data)
{
printf("位置超范围!\n");
return;
}
// 移动到指定位置
for (int i = 1; i < post; i++)
{
pre = current;
current = current->next;
}
// 判断是否是最后位置
if (current->next == NULL)
{
pre->next = current->next;
}
else
{
pre->next = current->next;
current->next->pre = pre;
}
free(current);
current = NULL;
list->data--;
}
// 修改指定位置
void modifyList(Node* list, int post, int data)
{
Node* current = list->next;
if (post < 0 || post > list->data)
{
printf("位置超范围!\n");
return;
}
// 移动到指定位置
for (int i = 1; i < post; i++)
{
current = current->next;
}
// 修改数据
current->data = data;
}
// 打印
void printList(Node* list)
{
Node* move_ptr = list->next;
while (move_ptr)
{
printf("%d->", move_ptr->data);
move_ptr = move_ptr->next;
}
printf("NULL\n");
}
int main(int argc, char const *argv[])
{
// 初始化
Node* list = initList();
tail = list;
// headInsert(list, 1);
// headInsert(list, 2);
// headInsert(list, 3);
// headInsert(list, 4);
// headInsert(list, 5);
// printList(list);
// tailInsert(list, 6);
// tailInsert(list, 7);
// tailInsert(list, 8);
// printList(list);
// deleteAny(list, 4);
// deleteAny(list, 7);
// printList(list);
// insert(list, 4, 9);
// printList(list);
// modifyList(list, 4, 1);
// printList(list);
insert(list, 1, 1);
insert(list, 2, 2);
insert(list, 2, 3);
printf("%d\n", list->data);
insert(list, 4, 3);
printList(list);
return 0;
}
双向链表操作集合(头尾指针)
最新推荐文章于 2024-01-20 20:07:40 发布