赞
踩
双链表是在单链表的基础上实现的,不过他们之间又有不同的之处。
1.在使用表头法插入的时候,需要注意的点是要判断表头的下一的元素是否为空(NULL)代码如下:
- //4.链表的插入,表头插入法
- void insertNewNodebyhead(struct Node* head, int data)
- {
- struct Node* NewNode = createNewNode(data);//创建节点
- NewNode->next = head->next;
-
- if(head->next!=NULL)//如果表头的下一个节点不空的话
- head->next->front = NewNode;
-
- NewNode->front = head;
- head->next = NewNode;
- }
2.在指定位置加入元素时,需要判度该位置是否存在,注意逻辑关系
完整代码:
- #include<stdio.h>
- #include<stdlib.h>
-
- struct Node
- {
- struct Node* front;
- int data;
- struct Node* next;
- };
- //1.创建表头,表示整个链表
- struct Node* createlistHead()
- {
- struct Node* listHead = (struct Node*)malloc(sizeof(struct Node));
- //初始化表头
- //表头无前驱
- //表头的无数据
- listHead->next = NULL;
- return listHead; //函数为结构体指针函数,即返回结构体变量
-
- }
- //2.创建节点为插入做准备
- struct Node* createNewNode(int data)
- {
- struct Node* NewNode = (struct Node*)malloc(sizeof(struct Node));
- NewNode->data = data;
- NewNode->next = NULL;
- return NewNode;
- }
- //3.打印链表
- void print(struct Node* Head)
- {
- //创建一个移动指针指向该链表的头结点
- struct Node* Pmove;
- Pmove = Head->next;//表头为空从下一个节点开始打印
-
- while (Pmove != NULL)
- {
- printf("%d\t", Pmove->data);
- Pmove = Pmove->next;
- }
- printf("\n");
- }
- //4.链表的插入,表头插入法
- void insertNewNodebyhead(struct Node* head, int data)
- {
- struct Node* NewNode = createNewNode(data);//创建节点
- NewNode->next = head->next;
-
- if(head->next!=NULL)//如果表头的下一个节点不空的话
- head->next->front = NewNode;
-
- NewNode->front = head;
- head->next = NewNode;
- }
- //5.表尾插入法
- void insertNewNodebytial(struct Node* head, int data)
- {
- struct Node* NewNode = createNewNode(data);
-
- //创建一个指针去找到表尾
- struct Node* Ptail=head;
- while (Ptail->next != NULL)
- {
- Ptail = Ptail->next;
- }
-
- //退出循环即找到表尾
- NewNode->next = Ptail->next;
- NewNode->front = Ptail;
- Ptail->next = NewNode;
- }
- //6.在指定位置前插入
- void insertNewNodebyAppoint(struct Node* head, int data,int m)
- {
- struct Node* NewNode = createNewNode(data);
- //创建一个移动指针去找到该位置
- struct Node* Pmove = head;
-
- if (head->next == NULL)
- {
- printf("该链表为空!!!\n");
- }
- else
- {
- while (Pmove->data!=m)
- {
- Pmove = Pmove->next;
- if (Pmove == NULL)
- {
- printf("未能找到该信息!!\n");
- return;
- }
- }
-
- NewNode->next = Pmove;
- NewNode->front = Pmove->front;
- Pmove->front->next = NewNode;
- Pmove->front = NewNode;
- }
- }
- //7.表头删除
- void deleteByhead(struct Node* head)
- {
- if (head->next == NULL)
- printf("该链表为空!!!");
- else
- {
- struct Node* Delete = head->next;
-
- head->next = Delete->next;
- Delete->next->front = head;
- free(Delete);
- Delete = NULL;
- }
- }
- //8.表尾删除
- void deleteBytial(struct Node* head)
- {
- struct Node* Deletetial = head->next;
-
- if (Deletetial == NULL)
- printf("该链表为空!!!");
- else
- {
- while (Deletetial->next != NULL)
- {
- Deletetial = Deletetial->next;
- }
-
- Deletetial->front->next = Deletetial->next;
- free(Deletetial);
- Deletetial = NULL;
- }
- }
- //9.删除指定位置的元素
- void deleteByAppoint(struct Node* head,int m)
- {
- //创建一个移动的指针去找到指定位置
- struct Node* DeletePmove = head->next;
-
- if (DeletePmove == NULL)
- printf("该链表为空!!!!");
- else
- {
- while (DeletePmove->data != m)
- {
- DeletePmove = DeletePmove->next;
- if (DeletePmove == NULL)
- {
- printf("未能找到该位置!!\n");
- return;
- }
- }
-
- DeletePmove->next->front = DeletePmove->front;
- DeletePmove->front->next = DeletePmove->next;
- free(DeletePmove);
- DeletePmove = NULL;
- }
- }
- int main()
- {
- int n;
- printf("请你输入你要插入数的个数:");
- scanf("%d", &n);
- printf("表头法插入list1链表:\n");
- struct Node* list1 = createlistHead();
-
- for (int i = 0; i < n; i++)
- {
- int m;
- scanf("%d", &m);
- insertNewNodebyhead(list1, m);//调用表头法插入函数
- }
-
- print(list1);
-
- printf("请输入你要插入的元素个数:");
- scanf("%d", &n);
- printf("表尾法插入list2链表:\n");
- struct Node* list2 = createlistHead();
-
- for (int i = 0; i < n; i++)
- {
- int m;
- scanf("%d", &m);
- insertNewNodebytial(list2, m);
- }
-
- print(list2);
-
- printf("在指定list2链表中插入元素\n");
- int k,l;
- printf("请你输入在那个元素之前插入和插入的值:");
- scanf("%d %d", &k,&l);
- insertNewNodebyAppoint(list2, l, k);
-
- print(list2);
-
- printf("表头删除list2的元素\n");
- deleteByhead(list2);
-
- print(list2);
-
- printf("表尾删除list2的元素\n");
- deleteBytial(list2);
-
- print(list2);
-
- printf("请你输入删除list2中的元素值:");
- int m;
- scanf("%d", &m);
- deleteByAppoint(list2, m);
-
- print(list2);
- system("pause");
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。