C 双向链表

头文件(.h)

#pragma once

typedef struct _NODES{

    int count;
    char name[8];
    struct _NODES* prev;
    struct _NODES* next;

}NODES;

源文件(.c)

#include "LinkList.h"
#include <stdlib.h>
#include <string.h>

NODES* lists = NULL;
//添加到链表最后
int _inserts(const char* name, int count){

    NODES* node = (NODES*)malloc(sizeof(NODES));
    node->count = count;
    strcpy(node->name,name);
    node->next = NULL;
    node->prev = NULL;
    if (lists==NULL)
    {
        lists = node;
        return 1;
    }
    for (NODES* p = lists; ; p=p->next)
    {
        if (p->next==NULL)
        {
            p->next = node;
            node->prev = p;
            return 1;
        }
    }
    return 0;

}
//根据count的大小插入数据
int _inserts2(const char* name, int count){

    NODES* node = (NODES*)malloc(sizeof(NODES));
    node->count = count;
    strcpy(node->name, name);
    node->next = NULL;
    node->prev = NULL;
    if (lists == NULL)
    {
        lists = node;
        return 1;
    }
    if (count < lists->count)
    {
        node->next = lists;
        lists->prev = node;
        lists = node;
        return 1;
    }
    if (count > lists->count && lists->next==NULL)
    {
        lists->next = node;
        node->prev = lists;
        return 1;
    }
    if (count > lists->count && lists->next != NULL && count < lists->next->count)
    {
        node->next = lists->next;
        lists->next->prev = node;
        lists->next = node;
        node->prev = lists;
        return 1;
    }
    for (NODES* p = lists->next; p != NULL; p = p->next)
    {

        if (p->next==NULL)
        {
            p->next = node;
            node->prev = p;
            return 1;
        }
        if (count > p->count && count < p->next->count)
        {
            node->next = p->next;
            p->next->prev = node;
            node->prev = p;
            p->next = node;
            return 1;
        }
    }
    free(node);
    return 0;

}

//删除某节点(name)
int _removes(const char* name){
    for (NODES* p = lists; p !=NULL; p=p->next)
    {
        if (0==strcmp(p->name,name))
        {
            if (p==lists)
            {
                lists = p->next;
                p->next->prev = NULL;
                free(p);
                return 1;
            }
            if (p->next==NULL)
            {
                p->prev->next = NULL;
                free(p);
                return 1;
            }
            p->prev->next = p->next;
            p->next->prev = p->prev;
            free(p);
            return 1;
        }
    }
    return 0;
}

//修改某节点信息
int _modifys(int count, const char* NewName){
    NODES* p = lists;
    int i = 0;
    while (p != NULL)
    {
        if (p->count==count)
        {
            memset(p->name, 0x00, sizeof(p->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、付费专栏及课程。

余额充值