一,先上头文件:linklist.h
需要在头文件中声明 “链表操作函数”。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define LEN sizeof(struct Node)
#define OK 0
#define ERROR 1
typedef struct Node
{
uint8_t id;
uint32_t mac;
struct Node *next;
}Node;
typedef struct Node *LinkList;
LinkList CreateEmptyLinkList(void);
LinkList AddNode(LinkList , int);
int DelNodeByID(LinkList, uint8_t, uint32_t);
int UpdateNodeByID(LinkList, uint8_t, uint32_t);
int FindNodeByID(LinkList, uint8_t, uint32_t);
int PrintList(LinkList);
二,
1,链表操作程序文件:linklist.c
#include "linklist.h"
int n; //number of all nodes
/* create empty linklist */
LinkList CreateEmptyLinkList()
{
LinkList head;
n = 0;
head = (LinkList)malloc(LEN);
if(head==NULL)
{
perror("Create linklist failed! try again");
return NULL;
}
head->id = 0xf;
head->mac = 0;
head->next = NULL;
return head;
}
/* add node to the head of linklist */
LinkList AddNode(LinkList L, int m) //add m nodes
{
LinkList p;
int flag = 0;
int i;
uint8_t id;
uint32_t mac;
for(i=0; i<m; i++)
{
LinkList q = L;
p = (LinkList)malloc(LEN);
printf("Input ID:\n");
scanf("%hhu",&id);
getchar();
printf("check ok\n");
printf("Input MAC:\n");
scanf("%u",&mac);
printf("check ok\n");
do{
if((id == q->id) || (mac == q->mac))
flag++;
q = q->next;
}while(q != NULL);
if(!