#pragma once//定义一个结构体(.h文件)
typedef struct _NODE{
int count;
char name[8];
struct _NODE* next;
}NODE;
int _insert(constchar* name, int count);
int _insert2(constchar* name, int count, int con);
int _remove(constchar* name);
NODE* _find(constchar* name);
int _modify(constchar* name, constchar* NewName);
void _show()
#include "LinkList.h"#include <stdlib.h>#include <string.h>//定义一个链表
NODE* list=NULL;
//插入到链表的最后面int _insert(constchar* 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;
return1;
}
for (NODE* p = list;; p=p->next)
{
if (p->next == NULL)
{
p->next = node;
return1;
}
}
return0;
}
//插入到链表的指定位置(con)int _insert2(constchar* 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;
return1;
}
int c = 1;
for (NODE* p = list;p!=NULL; p = p->next)
{
if (c==con)
{
node->next = p->next;
p->next = node;
return1;
}
}
free(node);
return0;
}
//根据name删除链表中第一个name的节点(name)int _remove(constchar* name){
NODE* p = list;
if (0 == strcmp(p->name, name))
{
NODE* pp = p->next;
list = pp;
free(p);
return1;
}
while (p!=NULL)
{
if (0==strcmp(p->next->name,name))
{
NODE* pp = p->next;
p->next = p->next->next;
free(pp);
return1;
}
p = p->next;
}
return0;
}
//根据name查找第一个节点(name)
NODE* _find(constchar* 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(constchar* name, constchar* 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;
}