链表:
1.1 概念
逻辑结构:线性结构
存储结构:链式存储
1.2有头单向不循环链表的示意图
1.3链表相关的操作:
1.创建链表
2.清空链表
3.销毁链表
4.头插法
5.尾插法
6.任意位置插入法
7.头删法
8.尾删法
9.任意位置删除法
10.查询链表中是否有想要的数据(在链表中获取指定位置的数据)
11.按照位置修改链表中的数据
12.按照值修改链表中的数据
13.两个链表合并
14.链表的排序
15.链表的翻转
16.遍历链表的节点----学习阶段看现象用的
1.4 操作示意图
1.4.1链表插入数据示意图
1.4.2 链表删除数据示意图
1.4.3链表的排序示意图
1.5代码实现
1.5.1头文件:
#ifndef __LINK_LIST_H_
#define __LINKE_LIST_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct __Node{
int data;
struct __Node *next;
}node_t;
int create_node(node_t **p, int in_data);
int insert_data_by_head(node_t *phead, int in_data);
int print_link_list(node_t *phead);
int insert_data_by_tail(node_t *phead, int in_data);
int insert_data_by_pos(node_t *phead, int in_data, int pos);
int del_from_list_by_head(node_t *phead);
int del_from_list_by_tail(node_t *phead);
int del_from_list_by_pos(node_t *phead, int pos);
int update_list_by_pos(node_t *phead, int pos, int new_data);
int update_list_by_value(node_t *phead, int old_value, int new_value);
int clean_list(node_t *phead);
int destroy_list(node_t **phead);
int merge_two_list(node_t *phead1, node_t **phead2);
int overturn_list(node_t *phead);
int sort_list(node_t *phead);
int sort_list(node_t *phead);
#endif
1.5.2函数文件:
#include "link_list.h"
//创建头节点
int create_node(node_t **p, int in_data)
{
if (p == NULL){
printf("传参错误\n");
return -1;
}
*p = (node_t*)malloc(sizeof(node_t));
(*p)->next = NULL;
(*p)->data = in_data;
return 0;
}
//头插法
int insert_data_by_head(node_t *phead, int in_data)
{
if (phead == NULL){
printf("传参错误\n");
return -1;
}
node_t* temp;
create_node(&temp, in_data);
temp->next = phead->next;
phead->next = temp;
return 0;
}
//打印
int print_link_list(node_t *phead)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
node_t *temp = phead;
while (temp->next != NULL){
temp = temp->next;
printf("%d ",temp->data);
}
puts("");
return 0;
}
//尾插法
int insert_data_by_tail(node_t *phead, int in_data)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
node_t* temp = phead;
node_t* pnew;
create_node(&pnew, in_data);
while (temp->next != NULL){
temp = temp->next;
}
temp->next = pnew;
return 0;
}
//按位置插入
int insert_data_by_pos(node_t *phead, int in_data, int pos)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
node_t* temp = phead;
node_t* pnew;
int i;
for (i = 0; i < pos -1; i++){
temp = temp->next;
if (temp == NULL){
break;
}
}
if (i < pos - 1){
printf("位置输入错误\n");
return -1;
}
create_node(&pnew, in_data);
pnew->next = temp->next;
temp->next = pnew;
return 0;
}
//头删法
int del_from_list_by_head(node_t *phead)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
if (phead->next == NULL){
printf("列表为空,不需要删除\n");
return -1;
}
node_t* temp;
temp = phead->next;
phead->next = temp->next;
free(temp);
temp = NULL;
return 0;
}
//尾删法
int del_from_list_by_tail(node_t *phead)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
if (phead->next == NULL){
printf("列表为空,不需要删除\n");
return -1;
}
node_t* temp = phead;
while (temp->next->next != NULL){
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
return 0;
}
//按位置删除法
int del_from_list_by_pos(node_t *phead, int pos)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
if (pos <= 0){
printf("插入位置不合理\n");
return -1;
}
if (phead->next == NULL){
printf("列表为空,不需要删除\n");
return -1;
}
node_t* temp = phead;
int i;
for (i = 0; i< pos -1; i++){
temp = temp->next;
if (temp->next == NULL){
break;
}
}
if (i < pos - 1){
printf("输入位置错误\n");
return -1;
}
node_t* pdel;
pdel = temp->next;
temp->next = pdel->next;
free(pdel);
pdel = NULL;
return 0;
}
//按位置修改数据
int update_list_by_pos(node_t *phead, int pos, int new_data)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
if (pos <= 0){
printf("修改位置不合理\n");
return -1;
}
if (phead->next == NULL){
printf("列表为空,不需要删除\n");
return -1;
}
node_t* temp = phead;
for (int i=0; i < pos; i++){
temp = temp->next;
if (temp == NULL){
printf("输入位置错误\n");
break;
}
}
temp->data = new_data;
return 0;
}
//按数据修改数据
int update_list_by_value(node_t *phead, int old_value, int new_value)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
if (phead->next == NULL){
printf("列表为空,不需要删除\n");
return -1;
}
node_t* temp = phead;
while (temp->next != NULL){
temp = temp->next;
if (temp->data == old_value){
temp->data = new_value;
return 0;
}
}
printf("没有这个数据,请重新输入\n");
return -1;
}
//清空除头节点的其它链表
int clean_list(node_t *phead)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
if (phead->next == NULL){
printf("列表为空,不需要清空\n");
return -1;
}
node_t* del;
while(phead->next != NULL){
del = phead->next;
phead->next = del->next;
free(del);
del = NULL;
}
return 0;
}
//销毁链表
int destroy_list(node_t **phead)
{
if (NULL == phead || *phead ==NULL){
printf("传参错误\n");
return -1;
}
clean_list(*phead);
free(*phead);
(*phead)->next = NULL;
return 0;
}
//合并两个链表
int merge_two_list(node_t *phead1, node_t **phead2)
{
if (NULL == phead1 || NULL == phead2 || *phead2 == NULL){
printf("输入参数为0,请检查\n");
return -1;
}
node_t* temp = phead1;
while (temp->next != NULL){
temp = temp->next;
}
temp->next = (*phead2)->next;
free(*phead2);
*phead2 = NULL;
return 0;
}
//翻转链表
int overturn_list(node_t *phead)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
node_t* p = NULL;
node_t* q = NULL;
p = phead->next;
phead->next = NULL;
while (p!=NULL){
q=p;
p = p->next;
q->next = phead->next;
phead->next = q;
}
return 0;
}
//对链表进行排序
int sort_list(node_t *phead)
{
if (NULL == phead){
printf("传参错误\n");
return -1;
}
if (phead->next == NULL){
printf("列表为空不需要排列\n");
return -1;
}
if (phead->next->next == NULL){
printf("列表就一个数据,不需要排列\n");
return -1;
}
node_t* p = phead->next;
node_t* q = p->next;
int temp;
while (p->next != NULL){
while (q != NULL){
if (p->data > q->data){
temp = p->data;
p->data = q->data;
q->data = temp;
}
q = q->next;
}
p = p->next;
q = p;
}
return 0;
}