链表
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
typedef struct {
char key[15];
char name[20];
int age;
}DATA;
typedef struct Node{
DATA data;
struct Node *next;
}ChainListType;
void ChainListAll(ChainListType *head);//显示所有节点
ChainListType *ChainListAddEnd(ChainListType *head, DATA data);//添加在链表结尾
ChainListType *ChainListAddFirst(ChainListType *head, DATA data);//添加节点在表头
ChainListType *ChainListFind(ChainListType *head, char *key);//按关键字查找内容
ChainListType *ChainListInsert(ChainListType *head, char *findkey, DATA data);//插入节点到链表指定位置
int ChainListDelete(ChainListType *head, char *key);//删除指定关键字的节点
int ChainListLength(ChainListType *head);
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;
}
scanf("%s%d", data.name, &data.age);
} while (1);
printf("该链表共有%d个节点。\n", ChainListLength(head));
ChainListAll(head);
printf("\n插入节点,输入插入位置的关键字:");
scanf("%s", &findkey);//输入插入位置关键字
printf("输入插入节点的数据(关键字 姓名 年龄):");
scanf("%s%s%d", data.key, data.name, &data.age);
head = ChainListInsert(head, findkey, data);
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();
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;
}
return;
}
ChainListType *ChainListAddEnd(ChainListType *head, DATA data)
{
//添加在链表结尾
ChainListType *node, *h;
if (!(node = (ChainListType *)malloc(sizeof(ChainListType))))
{
printf("为保存节点数据申请内存失败!\n");
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("为保存节点数据申请内存失败!\n");
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;
}
return NULL;
}
ChainListType *ChainListInsert(ChainListType *head, char *findkey, DATA data)
{
//插入节点到链表指定位置
ChainListType *node, *node1;
if (!(node = (ChainListType *)malloc(sizeof(ChainListType))))
{
printf("分配内存失败!\n");
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;
}
int ChainListLength(ChainListType *head)
{
//链表长度
ChainListType *h;
int i = 0;
h = head;
while (h) {
i++;
h = h->next;
}
return i;
}