C语言 实现链表
发布时间:2020-12-16 07:42:36 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #include "stdafx.h"#include "stdlib.h"#include "string.h"typedef struct{ char key[15]; char name[20]; int age;}DATA; typedef struct Node{ DA
|
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #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,*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%dn",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;
}
实现 int main(){
ChainListType *node,*head = NULL;
DATA data;
char key[15],findkey[15];
printf("输入链表中的数据.包括关键字,姓名,年龄,关键字输入0n");
do{
fflush(stdin);
scanf("%s",data.key);
if (strcmp(data.key,"0") == 0) break; //若输入0,则退出
scanf("%s%d",&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.age);
}
else{
printf("在链表中未找到关键字为%s的节点n",key);
}
printf("在链表中删除节点,输入要删除的关键字n");
fflush(stdin);
scanf("%s",key);
ChainListDelete(head,key);
ChainListAll(head);
//getch();
system("pause");
}
以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读
