
01.结构体定义
c
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FASLE 0//定义宏标识判断是否成功
typedef struct Node {
int data;
struct Node* next;
}Node;
02.初始化
c
Node* InitList() {
Node* list = (Node*)malloc(sizeof(Node));
list->data = 0;//创建节点保存data
list->next = list;
return list;
}
04.增加节点
c
void headInsert(Node*list,int data) {//头插
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = list->next;
list->next = node;
list->data++;//记录节点数
}
void tailInsert(Node* list,int data) {//带入头指针,尾插
Node* n = list;//保存list头节点,用n这个指针变量移动进行判断方便判断
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
while (n->next != list) {
n = n->next;
}
node->next = list;
n->next = node;
list->data++;
}
05.删除节点
c
int DeleteList(Node* list,int data) {
Node* prenode = list;
Node* current = list->next;//设置一个指向头街点的node节点
while (current!=list) {
if (current->data == data) {
prenode->next = current->next;
free(current);
list->data--;
return TRUE;
}
else {
prenode = current;
current = current ->next;
}
}
return FASLE;
}
}