C 双向链表

来源:互联网 发布:获取东方财富k线数据 编辑:程序博客网 时间:2024/05/16 00:50

头文件(.h)

#pragma oncetypedef 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;}
原创粉丝点击