头文件(.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;
}