【C·数据结构】详细实现单链表代码+图例直观理解-创新互联
文章目录
分享题目:【C·数据结构】详细实现单链表代码+图例直观理解-创新互联
文章来源:http://pwwzsj.com/article/cejgco.html
- 前言
- 单链表的初始化
- 1.结构体
- 2.初始化
- 单链表节点的建立
- 1.尾插
- 2.头插
- 查找
- 修改
- 删除
- 添加
- 打印
- 代码呈现
- 总结
没前言
单链表的初始化 1.结构体链表的节点需要一个数据域和一个指针域,指针用来连接各节点
typedef struct Linklist
{int key;
struct Linklist* next;
}List;
2.初始化初始化需要一个头节点(List* head),通过它来连接多个节点.
先分配空间(malloc),再对相应的参数赋值(NULL,key).(头节点可以不用存储数据)
List* Initlist(List*head,int key)
{head = (List*)malloc(sizeof(List));
head->next = NULL;
head->key = key;
return head;
}
单链表节点的建立链表的建立首先需要建立新的节点,用头插和尾插两种方式插入到链表中,建立的函数需要
一个数(key)和头节点.
List* setList(List* head, int key)
{List* node = (List*)malloc(sizeof(List));
node->key = key;
}
上述这是两种建立(或插入)的通用的方式,接下来详细分别介绍这两种插入方式
1.尾插尾插是按照顺序头节点依次往下建立,需要一个工具指针(end)随新节点建立而移动
从而保证头指针不动(尾指针使用全局变量即可).下面是使用processon作图表示链
接过程(用箭头表示指针,省略数据)
end->next = node; //
node->next = NULL;
end = node;
2.头插头插和尾插不同之处在于头插不需要移动的指针(end),插入的节点从头结点开始
(node2为新节点node)
node->next = head->next;
head->next = node;
查找查找需要头节点(head)和所要查找的数据(key),返回类型int为该数据在链表的位置
int find(List* head,int key)
{int i = 0;
while(head!=NULL)
{if(head->key)
{ return i;
}
head = head->next;
i++;
}
return -1;
}
修改修改操作需要一步查找操作,
List* modify(List* head,int key,int data)//key需要替换成data.
{int index = find(head,key);
if(index == -1)
{ printf("Not Found");
return;
}
else
{List* temp = head;
for(int i = 0; i< index; i++)
{ head = head->next;
}//指针移动到目标节点.
head->key = data;
return temp;
}
}
删除- 先找到对应节点的上一个节点再进行删除操作
List* delete_node(List* head,int key)
{int index = find(head,key);
if(index == -1)
{ printf("Not Found");
return;
}
else
{if(head->key == key)
{ List* temp = head->next;
free(head);
return temp;
}
else
{ List* temp = head;
while(temp->next!=NULL)//移动到目标节点前一个节点
{if(temp->next->key == key)
{break;
}
temp = temp->next;
}
List* free_node = temp->next;
temp->next = temp->next->next;
free(free_node);
return head;
}
}
}
添加在某数据后添加一个新节点
List* insert(List* head,int key,int data)
{int index = find(head,key);
if(index == -1)
{ printf("Not Found");
return;
}
else
{ List* temp = head;
for(int i = 0; i< index; i++)
{ head = head->next;
}//指针移动到目标节点.
List* node = (List*)malloc(sizeof(List));
node->next = head->next;
head->next = node;
node->key = data;
return temp;
}
}
打印void print(List* head)
{while(head!=NULL)
{printf("%d ",head->key);
head = head->next;
}
printf("\n");
}
代码呈现#include#includetypedef struct Linklist
{int key;
struct Linklist* next;
}List;
List* end;
List* Initlist(List* head, int key);
List* setList(List* head, int key);
int find(List* head, int key);
List* modify(List* head, int key, int data);
void print(List* head);
List* insert(List* head, int key, int data);
List* delete_node(List* head, int key);
int main(void)
{List* head = {NULL};
head = Initlist(head,1);
head = setList(head, 2);
head = setList(head, 3);
head = setList(head, 4);
head = setList(head, 5);
head = setList(head, 6);
print(head);
head = insert(head, 5, 99);
print(head);
head = delete_node(head, 5);
print(head);
head = modify(head, 6, 666);
print(head);
}
List* Initlist(List* head, int key)
{head = (List*)malloc(sizeof(List));
head->next = NULL;
head->key = key;
end = head;
return head;
}
List* setList(List* head, int key)
{List* node = (List*)malloc(sizeof(List));
node->key = key;
node->next = NULL;
end->next = node;
end = node;
return head;
}
int find(List* head, int key)
{int i = 0;
while (head != NULL)
{if(head->key==key)
{ return i;
}
head = head->next;
i++;
}
return -1;
}
List* modify(List* head, int key, int data)//key需要替换成data.
{int index = find(head, key);
if (index == -1)
{printf("Not Found");
return;
}
else
{List* temp = head;
for (int i = 0; i< index; i++)
{ head = head->next;
}//指针移动到目标节点.
head->key = data;
return temp;
}
}
void print(List* head)
{while (head != NULL)
{printf("%d ", head->key);
head = head->next;
}
printf("\n");
}
List* insert(List* head, int key, int data)
{int index = find(head, key);
if (index == -1)
{printf("Not Found");
return;
}
else
{List* temp = head;
for (int i = 0; i< index; i++)
{ head = head->next;
}//指针移动到目标节点.
List* node = (List*)malloc(sizeof(List));
node->next = head->next;
head->next = node;
node->key = data;
return temp;
}
}
List* delete_node(List* head, int key)
{int index = find(head, key);
if (index == -1)
{printf("Not Found");
return;
}
else
{if (head->key == key)
{ List* temp = head->next;
free(head);
return temp;
}
else
{ List* temp = head;
while(temp->next != NULL)//移动到目标节点前一个节点
{ if (temp->next->key == key)
{break;
}
temp = temp->next;
}
List* free_node = temp->next;
temp->next = temp->next->next;
free(free_node);
return head;
}
}
}
运行结果为
新人第一次写博客,如有错误,可以联系作者改正,如有更好的方式,也欢迎交流(❁´◡`❁)
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
分享题目:【C·数据结构】详细实现单链表代码+图例直观理解-创新互联
文章来源:http://pwwzsj.com/article/cejgco.html