C 单链表

#pragma once

//定义一个结构体(.h文件)
typedef struct _NODE{
    int count;
    char name[8];
    struct _NODE* next;
}NODE;
int _insert(const char* name, int count);
int _insert2(const char* name, int count, int con);
int _remove(const char* name);
NODE* _find(const char* name);
int _modify(const char* name, const char* NewName);
void _show()
#include "LinkList.h"
#include <stdlib.h>
#include <string.h>

//定义一个链表
NODE* list=NULL;

//插入到链表的最后面
int _insert(const char* name, int count){

    NODE* node = (NODE*)malloc(sizeof(NODE));
    memset(node,0x00,sizeof(NODE));
    strcpy(node->name,name);
    memcpy(&node->count,&count,sizeof(count));
    node->next = NULL;

    if (list==NULL)
    {
        list = node;
        return 1;
    }
    for (NODE* p = list;; p=p->next)
    {
        if (p->next == NULL)
        {
            p->next = node;
            return 1;
        }
    }
    return 0;
}

//插入到链表的指定位置(con)
int _insert2(const char* name, int count, int con){

    NODE* node = (NODE*)malloc(sizeof(NODE));
    memset(node, 0x00, sizeof(NODE));
    strcpy(node->name, name);
    memcpy(&node->count, &count, sizeof(count));
    node->next = NULL;

    if (con == 0)
    {
        NODE* p = list;
        node->next = p;
        list = node;
        return 1;
    }
    int c = 1;
    for (NODE* p = list;p!=NULL; p = p->next)
    {
        if (c==con)
        {
            node->next = p->next;
            p->next = node;
            return 1;
        }
    }
    free(node);
    return 0;
}
//根据name删除链表中第一个name的节点(name)
int _remove(const char* name){
    NODE* p = list;
    if (0 == strcmp(p->name, name))
    {
        NODE* pp = p->next;
        list = pp;
        free(p);
        return 1;
    }
    while (p!=NULL)
    {
        if (0==strcmp(p->next->name,name))
        {
            NODE* pp = p->next;
            p->next = p->next->next;
            free(pp);
            return 1;
        }
        p = p->next;
    }

    return 0;
}

//根据name查找第一个节点(name)
NODE* _find(const char* name){
    NODE* p = list;
    while (p != NULL)
    {
        if (0 == strcmp(p->name, name))
        {
            return p;
        }
        p = p->next;
    }
    return NULL;
}

//根据name修改成NewName(name,NewName)
int _modify(const char* name, const char* NewName){
    NODE* p = list;
    int i = 0;//表示修改了几个值
    while (p != NULL)
    {
        if (0 == strcmp(p->name, name))
        {
            strcpy(p->name, NewName);
            i++;
        }
        p = p->next;
    }
    return i;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值