数据结构常用函数(一)

数据结构中,我们经常用到了一些操作,比如删除,插入等等,有API函数直接调用,当然是比较好的,但是我们可以尝试一下自己写一下代码。
以简单的录入个人信息为例,首先来看一下这些函数。

#include <stdlib.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 *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); 
void ChainlistPrintAll(ChainListType *head); 

现在我们就来一步一步的实现每一个函数
1.遍历链表

void ChainlistPrintAll(ChainListType *head)
{
    ChainListType *h;
    DATA data;
    h=head;
    printf("list as fllowing:\n");
    while(h){//处理每一个节点
        data=h->data;
        printf("(%s,%s,%d)\n",data.key,data.name,data.age);
        h=h->next;//准备处理下一个节点
    }
    return ;
}

2.链表元素个数

int ChainListLength(ChainListType *head)
{
    ChainListType *h;
    int i=0;
    h=head;
    while(h){
        i++;
        h=h->next;//遍历计数
    }
    return i;
}

3.删除某个节点

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;
}

4.插入某个元素

ChainListType *ChainListInsert(ChainListType *head,char *findkey,DATA data)
{
    ChainListType *node,*node1;
    if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
    {
        printf("node's application fail!\n");
        return 0;
    }
    node->data=data;
    node1=ChainListFind(head,findkey);
    if(node1){
        node->next=node1->next;
        node1->next=node;
    }else{
        free(node);
        printf("do not find the place!\n");
    }
}

5.查找某个元素

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;
}

6.添加头节点

ChainListType *ChainListAddFirst(ChainListType *head,DATA data)
{
    ChainListType *node,*h;
    if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
    {
        printf("node's application fail!\n");
        return NULL;
    }
    node->data=data;
    node->next=head;
    head=node;
    return head;
}

7.添加尾节点

ChainListType *ChainListAddEnd(ChainListType *head,DATA data)
{
    ChainListType *node,*h;
    if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
    {
        printf("node's application fail!\n");
        return NULL;
    }
    node->data=data;
    node->next=NULL;
    if(head==NULL){
        head=NULL;
        return head;
    }
    h=head;
    while(h->next!=NULL)
        h=h->next;
    h-next=node;
    return head;
}

我们可以来写一个函数来测试一下

#include <iostream>
#include <string.h>
#include "ChainList_API.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

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

    printf("Enter the data in the linked list, including keywords, names, age:\n");

    do{
        fflush(stdin);
        scanf("%s",data.key);
        if(strcmp(data.key,"0")==0) break;
        scanf("%%d",data.name,data.age);
        head=ChainListAddEnd(head,data);
    }while(1);

    printf("The number of nodes in the linked list is:%d\n",ChainListLength(head));
    ChainlistPrintAll(head);

    printf("\nEnter the insertion node's keyword:\n");
    scanf("%s",&findkey);
    printf("Enter the data in the linked list, including keywords, names, age:\n");
    scanf("%s%s%d",data.key,data.name,data.age);
    head=ChainListInsert(head,findkey,data);

    ChainlistPrintAll(head);

    printf("Enter search keywords:\n");
    fflush(stdin);
    scanf("%s",key);
    node=ChainListFind(head,key);
    if(node){
        data=node->data;
        printf("The keyword %s corresponding node is:(%s,%s,%d)",key,data.key,data.name,data.age);
    }else
        printf("No corresponding node found!\n");

    printf("Enter keywords to delete:\n");
    fflush(stdin);
    scanf("%s",key);
    ChainListDelete(head,key);

    ChainlistPrintAll(head);

    return 0;
}

这里是文件,这个是ChainList_API.c文件,这个是ChainList_API.h文件。

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值