链表的基本操作

来源:互联网 发布:gps测试软件 编辑:程序博客网 时间:2024/06/16 21:42

来写博客了,休息了一段时间先来看看

休息一天,继续奋斗。。。。。。。。。
基本步骤与顺序表基本差不多,首先说下该链表的头结点是存储数据的。
建立
ChainList.h
代码如下:

#include "stdio.h" typedef struct Node {   DATA data;   struct Node *next;     } ChainListType;//定义的结点类型,其中包括该节点数据和下个结点的地址 ,同时也代表了整个链表 ,也可以分开定义书上就是分开定义的   ChainListType *ChainListAddEnd(ChainListType *head,DATA data);//在链表尾部插入结点data  ChainListType *ChainListAddFirst(ChainListType *head,DATA data);// 在链表首部插入结点data  ChainListType *ChainListFind(ChainListType *head,char *key);//按关键字查找返回当前结点  ChainListType *ChainListInsert(ChainListType *head ,char *key,DATA data);//按关键字查找后在指定位置插入结点  int ChainListAll(ChainListType *head);//查找链表长度 




接着是实现这些方法:
建立ChainList.c
//下面进行链表的基本操作的具体步骤
#include "stdio.h"ChainListType *ChainListAddEnd(ChainListType *head,DATA data)//在链表尾部插入结点data{   ChainListType *h,*node;    if(!(node =(ChainListType *)malloc(sizeof(ChainListType))))    {       printf("分配内存失败");       returnNULL;       }   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;    returnhead;}//在链表首部插入结点dataChainListType *ChainListAddFirst(ChainListType *head,DATA data)//在链表尾部插入结点data{   ChainListType *node;    if(!(node =(ChainListType *)malloc(sizeof(ChainListType))))    {       printf("分配内存失败");       returnNULL;       }   node->data = data;   node->next = head;    head =node;    returnhead;  }//按关键字查找返回当前结点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 *key,DATAdata){    ChainListType *node ,*node1;    if(!(node =(ChainListType *)malloc(sizeof(ChainListType))))    {        printf("分配内存失败");        return 0;    }     node->data = data;    node1 = ChainListFind(head,key);    printf("%s",node1->data.key);    if(node1)    {       node->next = node1->next;       node1->next = node;    }        else    {        free(node);         printf("没查找到要插入的区域");    }    return head;    }//查找链表长度int ChainListAll(ChainListType *head){    ChainListType *h;    int i=0;    h = head;    while(h)    {        i++;       h= h->next;    }        return i;}




最后一个测试文件

#include "stdio.h"#include "string.h"typedef struct {    char key[15];    char name[15];    int age;}DATA;#include "ChainList.h"#include "ChainList.c"//遍历整个链表、void ChainListFindAll(ChainListType *head){    ChainListType *h;    DATA data;    h = head;    printf("链表所包含的内容如下");     while(h)    {        data = h->data;        printf("<%s %s %d>\n",data.key,data.name,data.age);        h = h->next;    }    return;} int main(){    ChainListType *head=NULL;     DATA data;     char findkey[15],key[15];    printf("插入数据,包括编号,姓名,年龄然后关键字为0退出\n");   while(1)    {      fflush(stdin);        scanf("%s",data.key);        if(strcmp(data.key,"0")==0) {break;}        scanf("%s%d",data.name,&data.age);        head = ChainListAddEnd(head,data);    }      printf("该链表共有%d个结点\n",ChainListAll(head));    ChainListFindAll(head);    printf("插入数据的位置关键字输入");    scanf("%s",&findkey);     printf("输入要插入的数据");    scanf("%s%s%d",data.key,data.name,&data.age);   head= ChainListInsert(head,findkey,data);   ChainListFindAll(head);       while(1);    return 0;}

运行结果如下:

0 0
原创粉丝点击