#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){
ChainListType *node, *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;
}