您的位置:首页 > 理论基础 > 数据结构算法

数据结构-单向链表(C语言)

2016-09-03 11:34 603 查看
ChainList.h

#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
DATA data;
struct Node *next;
}ChainListType;
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);//获取链表节点数量


ChainList1.c

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 NULL;
}
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;
h=head;
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)//获取链表节点数量
{
int i=0;
ChainListType *h;
h=head;
while(h){
i++;
h=h->next;
}
return i;
}


ChainListTest.c

#include<stdio.h>
typedef struct{
char key[15];
char name[20];
int age;
}DATA;
#include"ChainList.h"
#include"ChainList1.c"
void ChainListAll(ChainListType *head)//遍历链表
{
ChainListType *h;
h=head;
DATA data;
printf("所有链表如下:\n");
while(h){
data=h->data;
printf("(%s,%s,%d)\n",data.key,data.name,data.age);
h=h->next;
}
}

int main()
{
ChainListType *node,*head=NULL;
DATA data;
char key[15],findkey[15];

printf("input data key,name,age(关键字为0时退出):\n");
do{
fflush(stdin);
scanf("%s",data.key);//关键字为0时退出
if(strcmp(data.key,"0")==0)break;
scanf("%s%d",data.name,&data.age);
head=ChainListAddEnd(head,data);
}while(1);
printf("共有%d个节点\n",ChainListLength(head));
ChainListAll(head);

printf("\n插入节点,输入插入位置关键字:\n");
fflush(stdin);///////////////////error1:删除后scanf跳过执行
scanf("%s",findkey);
printf("输入插入节点的数据(key, name, age):");
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,%d)\n",data.key,data.name,data.age);
}
else{
printf("没找到节点!\n");
}

printf("\n在链表中删除节点,输入删除节点的关键字:");
fflush(stdin);
scanf("%s",key);
ChainListDelete(head,key);
ChainListAll(head);
printf("\n在链表中删除结点,输入要删除的关键字:");
fflush(stdin);//清空输入缓冲区
scanf("%s",key);//输入删除结点关键字
ChainListDelete(head,key); //调用删除结点函数
ChainListAll(head); //显示所有结点
getch();
return 0;
}


例题:链表实现简单通讯录

AddressList.c

#include<stdio.h>
typedef struct{
char key[15];
char addr[20];
char telephone[15];
char mobile[12];
}DATA;
#include"ChainList.h"
#include"ChainList1.c"

void ChainListAll(ChainListType *head)//显示联系人
{
ChainListType *h;
DATA data;
h=head;
printf("all data:\n");
printf("name        address      telephonr        mobile\n");
while(h){
data=h->data;
printf("%s\t     %s\t   %s\t   %s\n",data.key,data.addr,data.telephone,data.mobile);
h=h->next;
}
}

ChainListType *input(ChainListType *head)//添加联系人
{
DATA data;
ChainListType *h=head;
printf("input Contacts:\n");
printf("name:");
scanf("%s",data.key);
printf("address:");
scanf("%s",data.addr);
printf("telephone:");
scanf("%s",data.telephone);
printf("mobile:");
scanf("%s",data.mobile);
return ChainListAddFirst(head,data);
}

void find(ChainListType *head)//查找联系人
{
ChainListType *h;
DATA data;
char name[15];
printf("input name:");
scanf("%s",name);
h=ChainListFind(head,name);
if(h)
{
data=h->data;
printf("%s,%s,%s,%s",data.key,data.addr,data.telephone,data.mobile) ;
}
}

void deletec(ChainListType *head)//删除联系人
{
ChainListType *h=head;
char name[15];
printf("input delete name:");
//  fflush(stdin);
scanf("%s",name);
ChainListDelete(head,name);
}

int main()
{
ChainListType *head=NULL;
int select;
printf("-----------------------------\n");
printf("1.add contacts\n");
printf("2.find contacts\n");
printf("3.delete contacts\n");
printf("4.show all contacts\n");
printf("0.quit\n");
printf("-----------------------------\n");
do{
select=getch();
switch(select)
{
case '1':
printf("\nadd contacts \n");
head=input(head);break;
case '2':
printf("\nfind contacts\n");
find(head);break;
case '3':
printf("\ndelete contacts\n");
deletec(head);break;
case '4':
printf("\nshow all contacts\n");
ChainListAll(head);break;
case '0':
return 0;
}
}while(select!='0');
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息