//双向链表的基本操作(增删改查...)
//新创建双链表为 41->67->34->0->69->24->78
//在表中第 4 的位置插入元素 1 41->67->34->1->0->69->24->78
//在表中第 2 的位置插入元素 8 41->8->67->34->1->0->69->24->78
//表中删除元素 8 41->67->34->1->0->69->24->78
//元素 1 的位置是 :4
//表中第 4 个节点中的数据改为存储3 41->67->34->3->0->69->24->78
#include <stdio.h>
#include <stdlib.h>
//随机数的范围
#define Max 100
//节点结构体
typedef struct Node
{
struct Node *pre; //指针域-->指向前驱节点
int data; //数据域
struct Node *next;//指针域-->指向后继节点
}Node;
Node *CreatNode(Node *head);
Node *CreatList(Node *head, int length);
Node *InsertListHead(Node *head, int add, int data);
Node *InsertListEnd(Node *head, int add, int data);
void PrintList(Node *head);
Node *DeleteList(Node *head, int data);
Node *ModifyList(Node *head, int add, int newElem);
int FindList(Node *head, int elem);
int main()
{
Node * head=NULL;
//创建双链表
head=CreatList(head,7);
printf("新创建双链表为\t");
PrintList(head);
//在表中第 4 的位置插入元素 1
head=InsertListHead(head, 4,1);
printf("在表中第 4 的位置插入元素 1\t");
PrintList(head);
//在表中第 2 的位置插入元素 8
head=InsertListEnd(head, 2, 8);
printf("在表中第 2 的位置插入元素 8\t");
PrintList(head);
//表中删除元素 8
head=DeleteList(head, 8);
printf("表中删除元素 8\t\t\t");
PrintList(head);
printf("元素 1 的位置是\t:%d\n",FindList(head,1));
//表中第 4 个节点中的数据改为存储 3
head = ModifyList(head,4,3);
printf("表中第 4 个节点中的数据改为存储3\t");
PrintList(head);
return 0;
}
//创建双向链表
Node *CreatNode(Node *head)
{
head = (Node *)malloc(sizeof(Node));
if(head == NULL)
{
printf("malloc error\r\n");
return NULL;
}
head->pre = NULL;
head->data = rand() % Max;
head->next = NULL;
return head;
}
Node *CreatList(Node *head, int length)
{
if(length == 1)
return (head = CreatNode(head));
else
{
head = CreatNode(head);
Node *list = head;
for(int i = 1; i < length; i++)
{
Node *body = (Node *)malloc(sizeof(Node));
body->pre = NULL;
body->next = NULL;
body->data = rand() % Max;
//这样也可以创建 创建的第一个设为head
// if(i == 1)
// head = body;
// else
// {
// list->next = body;
// body->pre = list;
// }
// list = body;
list->next = body;
body->pre = list;
list = list->next;
}
}
// 加上下面两句就是双向链表
// list->next = head;
// head->pre = list;
return head;
}
//双向链表的插入(头部、中部、尾部)
//add 向前插入数据
Node *InsertListHead(Node *head, int add, int data)
{
Node *temp = (Node *)malloc(sizeof(Node));
if(temp == NULL)
{
printf("malloc error\r\n");
return NULL;
}
else
{
temp->data = data;
temp->pre = NULL;
temp->next = NULL;
}
//插入到链表表头
if(add == 1)
{
temp->next = head;
head->pre = temp;
head = temp;
}
else
{
Node *body = head;
for(int i = 1; i < add; i++)
{
body = body->next; //插入到第2个只需要找到第1个 head是第1个
}
body->pre->next = temp;
temp->pre = body->pre;
temp->next = body;
body->pre = temp;
}
return head;
}
//add 向后插入数据
Node *InsertListEnd(Node *head, int add, int data)
{
int i = 1;
Node *temp = (Node *)malloc(sizeof(Node));
if(temp == NULL)
{
printf("malloc error\r\n");
return NULL;
}
else
{
temp->data = data;
temp->pre = NULL;
temp->next = NULL;
}
Node *body = head;
while((body->next) && (i < add-1))
{
body = body->next; //插入到第2个只需要找到第1个 head是第1个
i++;
}
if(body->next == NULL)
{
body->next = temp;
temp->pre = body;
}
else
{
//搭上四条线----建立链接
body->next->pre = temp;
temp->next = body->next;
body->next = temp;
temp->pre = body;
}
return head;
}
//双向链表的删除
Node *DeleteList(Node *head, int data)
{
Node *temp = head;
while(temp)
{
if(temp->data == data)
{
if(temp->pre == NULL)
{
head = temp->next;
temp->next = NULL;
}
else if(temp->next == NULL)
{
temp->pre->next = NULL;
}
else
{
temp->pre->next = temp->next;
temp->next->pre = temp->pre;
}
free(temp);
return head;
}
temp = temp->next;
}
printf("No find %d\n", data);
return head;
}
//双向链表修改节点数据
Node *ModifyList(Node *head, int add, int newElem)
{
Node *temp = head;
for(int i = 1; i < add; i++)
{
temp = temp->next;
}
temp->data = newElem;
return head;
}
//双向链表的查找
int FindList(Node *head, int elem)
{
Node *temp = head;
int i = 1;
while(temp)
{
if(temp->data == elem)
return i;
i++;
temp = temp->next;
}
return -1;
}
//双向链表的打印
void PrintList(Node *head)
{
Node *temp = head;
while(temp)
{
if(temp->next == NULL)
printf("%d\n", temp->data);
else
printf("%d->", temp->data);
temp = temp->next;
}
}
双向链表的基本操作大全(增删改查...)-C语言
最新推荐文章于 2022-12-09 15:32:03 发布