打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C语言 实现链表
C语言 实现链表
向顺序表插入元素的时候需要移动大量的数据
链表采用动态存储分配,
可以根据需要申请内存单元
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "stdafx.h"
#include "stdlib.h"
#include "string.h"
typedef struct{
char key[15];
char name[20];
int age;
}DATA;
typedef struct Node{
DATA data;
struct Node * next;
}ChainListType;
//  添加到节点的尾部
ChainListType * ChainListAddEnd(ChainListType * head,DATA data){
//head 为链表的头指针,data为节点保存的数据
ChainListType *node, *h;
//因为需要动态分配内存 所以需要引入 stdlib.h 头文件
if (!(node = (ChainListType *)malloc(sizeof(ChainListType)))){
printf("为保存的节点数据申请内存失败");
return NULL;
}
node->data = data;
node->next = NULL;
if (head == NULL){
head = node;
return head;
}
h = head;
while (h->next!=NULL)
h = h->next;
h->next = node;
return head;
}
//添加节点到首部
ChainListType * ChainListAddFirst(ChainListType *head,DATA data){
ChainListType * node, *h;
if (!(node = (ChainListType *)malloc(sizeof(ChainListType)))){
printf("为保存的节点数据申请内存失败");
return NULL;
}
node->data = data;
node->next = head;     //指向头指针所指节点
head = node;          //头指针指向新增节点
return head;
}
//按照关键字查找节点
ChainListType * ChainListFind(ChainListType * head,char *key){
ChainListType *h;
h = head;
while (h)
{
if (strcmp(h->data.key, key) == 0){      //若节点的关键字与传入关键字相同
return h;                          //  返回该节点指针
h = h->next;                       //  处理下一个节点
}
}
}
//插入节点到链表
ChainListType * ChainListInsert(ChainListType *head,char *findkey,DATA data){
ChainListType * node, *node1;
if (!(node = (ChainListType *)malloc(sizeof(ChainListType)))){
printf("为保存的节点数据申请内存失败");
return 0;
}
node->data = data;
node1 = ChainListFind(head, findkey);
if (node1){
node->next = node1->next;
node1->next = node;
}
else{
free(node);
printf("未找到插入位置\n");
}
return head;
}
//删除节点
int ChainListDelete(ChainListType *head, char *key){
ChainListType *node, *h;
node = h = head;
while (h){
if (strcmp(h->data.key, key) == 0){
node->next = h->next;
free(h);
return 1;
}
else{
node = h;
h = h->next;
}
}
return 0;
}
void ChainListAll(ChainListType *head){
ChainListType *h;
DATA data;
h = head;
printf("链表所有的数据如下\n");
while (h)
{
data = h->data;
printf("%s%s%d\n", data.key, data.name, data.age);
h = h->next;
}
}
//统计链表的长度
int ChainListLength(ChainListType * head){
ChainListType *h;
int i = 0;
h = head;
while (h){
i++;
h = h->next;
}
return i;
}
实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main(){
ChainListType *node, *head = NULL;
DATA data;
char key[15], findkey[15];
printf("输入链表中的数据.包括关键字,姓名,年龄,关键字输入0\n");
do{
fflush(stdin);
scanf("%s", data.key);
if (strcmp(data.key, "0") == 0) break; //若输入0,则退出
scanf("%s%d", data.name, &data.age);
head = ChainListAddEnd(head, data);
} while (1);
ChainListAll(head);
printf("在链表中查找,请输入关键字\n");
fflush(stdin);      //  清空输入缓冲区
scanf("%s", key);
node = ChainListFind(head, key);
if (node){
data = node->data;
printf("关键字%s对应的节点数据(%s,%s,%d)\n",key,data.key,data.name,data.age);
}
else{
printf("在链表中未找到关键字为%s的节点\n", key);
}
printf("在链表中删除节点,输入要删除的关键字\n");
fflush(stdin);
scanf("%s", key);
ChainListDelete(head, key);
ChainListAll(head);
//getch();
system("pause");
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
环形缓冲区的设计与实现
结构体类型变量的定义和引用
查找算法集
单链表的逆序(反转)
编程中国
详解双向链表的基本操作(C语言)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服