#ifndef __LIST_H__
#define __LIST_H__
#include "stdbool.h"
typedef struct studentData {
char *name;
int age;
bool isMale;
int grade;
} listData_t;
typedef struct node {
listData_t data;
struct node *next;
} node_t, *list;
void add_node_by_tail(list *tail, list node);
void add_node_by_head(list head, list node);
int get_list_node_num(list head);
void add_node_at_before(list head ,list node);
void delete_node(list head, int index);
void delete_list(list *node);
void print_list(list head);
#endif
#include "my_list.h"
#include "stdio.h"
#include "stdlib.h"
void add_node_by_tail(list *tail, list node)
{
if (NULL == tail || NULL == *tail || node == NULL) {
return;
}
(*tail)->next = node;
*tail = node;
return;
}
void add_node_by_head(list head, list node)
{
if (NULL == head || node == NULL) {
return;
}
node->next = head->next;
head->next = node;
return;
}
int get_list_node_num(list head)
{
if (head == NULL) {
return 0;
}
int num = 0;
list tmp = head->next;
while (tmp != NULL) {
num++;
tmp = tmp->next;
}
return num;
}
void add_node_at_before(list head ,list node)
{
if (node == NULL || head == NULL) {
printf("add_node_at_before null");
return;
}
static int mu = 0;
list tmpBef = head;
list tmpBeh = head->next;
if (tmpBeh == NULL) {
head->next = node;
return;
} else {
while (tmpBeh != NULL) {
printf("--%d--\n", ++mu);
if (node->data.age < tmpBeh->data.age) {
tmpBef->next = node;
node->next = tmpBeh;
return;
} else {
tmpBef = tmpBeh;
tmpBeh = tmpBeh->next;
}
}
}
tmpBef->next = node;
return;
}
void delete_node(list head, int index)
{
if (NULL == head || head->next == NULL) {
return;
}
list tmpBef = head;
list tmpBeh = head->next;
while (tmpBeh != NULL) {
if (tmpBeh->data.age == index) {
tmpBef->next = tmpBeh->next;
free(tmpBeh);
tmpBeh = tmpBef->next;
} else {
tmpBef = tmpBeh;
tmpBeh = tmpBeh->next;
}
}
return;
}
void delete_list(list *node)
{
list *addr = (list *)node;
if(addr == NULL || *addr == NULL) {
return;
}
list head = *addr;
list tmp = head;
while(head != NULL) {
tmp = head;
head = head->next;
printf("Delete %d\n", tmp->data.age);
free(tmp);
}
*addr = NULL;
return;
}
void print_list(list head)
{
if (head == NULL || head->next == NULL) {
return;
}
head = head->next;
while(head != NULL) {
printf("list age is %d, grade is %d, name is %s\n", head->data.age, head->data.grade, head->data.name);
head = head->next;
}
return;
}
```c
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "common_def.h"
#include "my_list.h"
#define MAGICNUM (0xff)
typedef struct list {
int nodeNum;
int magicNm;
list head;
list tail;
} list_t;
list_t g_myList = { 0 };
void init_head_list(list_t *myList)
{
if (NULL == myList) {
return;
}
list emptyNode = (list)malloc(sizeof(node_t));
if (NULL == emptyNode) {
return;
}
memset(emptyNode, 0x00, sizeof(node_t));
emptyNode->data.age = 99;
emptyNode->next= NULL;
myList->magicNm = MAGICNUM;
myList->nodeNum = 0;
myList->head = emptyNode;
myList->tail = emptyNode;
return;
}
list create_node(int age, int grade, bool isMale, char *name)
{
list node = (list)malloc(sizeof(node_t));
if (NULL == node) {
return NULL;
}
node->data.age = age;
node->data.grade = grade;
node->data.isMale = isMale;
node->data.name = (char *)malloc(strlen(name) + 1);
memset(node->data.name, 0x00, strlen(name) + 1);
memcpy(node->data.name, name, strlen(name));
node->next = NULL;
return node;
}
int main()
{
list node = NULL;
init_head_list(&g_myList);
for (int i = 1 ; i <= 20; ++i) {
node = create_node(i+2, i+10, true, "aaa");
add_node_by_tail(&(g_myList.tail), node);
g_myList.nodeNum++;
}
delete_node(g_myList.head, 1);
delete_node(g_myList.head, 2);
print_list(g_myList.head);
printf("***********************************\n");
node = create_node(122, 100, false, "jam");
add_node_at_before(g_myList.head, node);
print_list(g_myList.head);
delete_list(&g_myList.head);
printf("node num = %d, num = %d\n", get_list_node_num(g_myList.head), g_myList.nodeNum);
printf("list magic num 0x%02x\n", g_myList.magicNm);
return 0;
}