= = 实现了链表的CRUD和一些其他的函数比如应用所有元素于函数、初始化、销毁....
Code:
main.c
//#include "list_test.c"
#include "link_list_test.c"
int main() {
// list_test();
link_list_test();
return 0;
}
LinkList.h
#ifndef _LINK_LIST_H_
#define _LINK_LIST_H_
#include <stdbool.h>
struct Node{
int key;
int value;
struct Node * next;
struct Node * previous;
};
typedef struct linkList{
int length;
struct Node * item;
}LinkList;
//初始化链表
void initLinkList(LinkList * pLinkList);
//销毁链表
void destroyLinkList(LinkList * pLinkList);
//在链表尾部添加节点
bool addNode(LinkList * pLinkList,struct Node * node);
//在指定位置插入节点
bool insertNode(LinkList * pLinkList,struct Node * node,int pos);
//通过key移除指定节点
bool removeById(LinkList * pLinkList,int key);
//通过key修改指定节点的value
bool updateById(LinkList * pLinkList,int key,int value);
//通过key查找指定节点
struct Node * findByKey(LinkList * pLinkList,int key);
//将每个函数应用于节点
void Traverse(LinkList * pLinkList,void (*pfun)(struct Node * node));
//判断位置是否在链表范围内
bool isObtains(LinkList * pLinkList,int pos);
//key所在节点是否存在
bool isExistKey(LinkList * pLinkList,int key);
#endif
LinkList.c
#include <stdbool.h>
#include <stdlib.h>
#include "LinkList.h"
//初始化链表
void initLinkList(LinkList * pLinkList)
{
pLinkList->length = 0;
pLinkList->item = NULL;
}
//销毁链表
void destroyLinkList(LinkList * pLinkList)
{
pLinkList->length = 0;
struct Node * node = pLinkList->item;
struct Node * nTemp;
while(node)
{
nTemp = node;
node = node->next;
free(nTemp);
}
pLinkList->item = NULL;
}
//在链表尾部添加节点
bool addNode(LinkList * pLinkList, struct Node * node)
{
if(pLinkList->item && node)
{
struct Node * n = pLinkList->item;
while(n->next)
{
n = n->next;
}
n->next = node;
pLinkList->length++;
return true;
}
else if(!pLinkList->item && node)
{
pLinkList->item = node;
pLinkList->length++;
return true;
}
return false;
}
//在指定位置插入节点
bool insertNode(LinkList * pLinkList,struct Node * node,int pos)
{
if(isObtains(pLinkList,pos))
{
int j=0;
struct Node * n = pLinkList->item;
while(n && j<pos)
{
j++;
n = n->next;
}
if(n)
{
node->previous = n;
node->next = n->next;
n->next = node;
pLinkList->length++;
return true;
}
}
return false;
}
//通过key移除指定节点
bool removeById(LinkList * pLinkList,int key)
{
struct Node * node = pLinkList->item;
if(node->key == key)
{
pLinkList->item = node->next;
free(node);
return true;
}
struct Node * pNTemp;
while(node->next)
{
if(node->next->key == key)
{
pNTemp = node->next;
node->next = node->next->next;
free(pNTemp);
return true;
}
node = node->next;
}
return false;
}
//通过key修改指定节点的value
bool updateById(LinkList * pLinkList,int key,int value)
{
struct Node * node = findByKey(pLinkList,key);
if(node)
{
node->value = value;
return true;
}
return false;
}
//通过key查找指定节点
struct Node * findByKey(LinkList * pLinkList,int key)
{
struct Node * node = pLinkList->item;
while(node)
{
if(node->key == key)
{
return node;
}
node = node->next;
}
return NULL;
}
//将每个函数应用于节点
void Traverse(LinkList * pLinkList,void (*pfun)(struct Node * node))
{
struct Node * node = pLinkList->item;
while(node)
{
(*pfun)(node);
node = node->next;
}
}
bool isObtains(LinkList * pLinkList,int pos)
{
if(pLinkList->item && pLinkList->length>0 && pos>=0 && pLinkList->length>pos)
return true;
else
return false;
}
bool isExistKey(LinkList *pLinkList, int key) {
struct Node * node = pLinkList->item;
while(node)
{
if(node->key == key)
{
return true;
}
node = node->next;
}
return false;
}
link_list_test.c
#include <stdlib.h>
#include "utils.c"
#include "LinkList.h"
void printf_info();
void link_list_test();
void choose_option();
void DestroyLinkList(LinkList *pList);
void InitLinkList(LinkList *pList);
void addElement(LinkList *pList);
void delElement(LinkList *pList);
void updateElementByKey(LinkList *pList);
void findElementByKey(LinkList *pList);
void viewAllElement(LinkList *pList);
void viewElement(struct Node * node);
void link_list_test()
{
printf_info();
choose_option();
}
void choose_option() {
int d = -1;
LinkList link_list;
do{
scanf("%d",&d);
eatline();
switch(d)
{
case 0://销毁链表
DestroyLinkList(&link_list);
break;
case 1://初始化链表
InitLinkList(&link_list);
break;
case 2://添加元素
addElement(&link_list);
break;
case 3://删除指定元素通过key
delElement(&link_list);
break;
case 4://修改指定元素通过key
updateElementByKey(&link_list);
break;
case 5://查找指定元素通过key
findElementByKey(&link_list);
break;
case 6://输出元素列表
viewAllElement(&link_list);
break;
case 7://退出
break;
default:
break;
}
printf("->");
}while(d!=7);
}
void viewAllElement(LinkList *pList) {
Traverse(pList,viewElement);
}
void viewElement(struct Node * node)
{
printf("%d:%d\n",node->key,node->value);
}
void findElementByKey(LinkList *pList) {
int key,value;
printf("请输入要查找的key:");
if(scanf("%d",&key) == 1)
{
struct Node * node = findByKey(pList,key);
printf("key:%d,value:%d\n",key,node->value);
}
eatline();
}
void updateElementByKey(LinkList *pList) {
int key,value;
printf("请输入要修改元素的key:");
if(scanf("%d",&key)==1 && !isExistKey(pList,key))
{
printf("输入元素不存在!");
eatline();
return;
}
eatline();
printf("请输入要修改的value:");
scanf("%d",&value);
eatline();
updateById(pList,key,value);
}
void delElement(LinkList *pList) {
int key;
printf("请输入要删除的元素的key:");
scanf("%d",&key);
eatline();
removeById(pList,key);
}
void addElement(LinkList *pList) {
int key = 0,
value = 0;
printf("请输入key:");
while(scanf("%d",&key)!=1 || isExistKey(pList,key)){
printf("key已经存在或者key格式不正确!\n");
printf("请重新输入key:");
eatline();
}
eatline();
printf("请输入value:");
while(scanf("%d",&value) != 1)
{
printf("value格式不正确!\n");
printf("请重新输入value:");
eatline();
}
eatline();
struct Node * node = (struct Node * )malloc(sizeof(struct Node));
node->key = key;
node->value = value;
addNode(pList,node);
}
void InitLinkList(LinkList *pList) {
initLinkList(pList);
printf("初始化完毕!\n");
}
void DestroyLinkList(LinkList *pList) {
destroyLinkList(pList);
}
void printf_info() {
printf("双向链表--------------------------\n");
printf("0.销毁链表.\n");
printf("1.初始化链表.\n");
printf("2.添加元素.\n");
printf("3.删除指定元素通过key.\n");
printf("4.修改指定元素通过key.\n");
printf("5.查找指定元素通过key.\n");
printf("6.输出元素列表.\n");
printf("7.退出.\n");
printf("---------------------------------\n");
}