typedef struct node_s {
int val;
struct node_s* next;
}Node_t;
typedef struct linkedList_s {
Node_t* head;
//int size;
}LinkedList_t;
//创建链表
LinkedList_t* create()
{
return calloc(1, sizeof(LinkedList_t));
}
//头插法函数
bool headadd(LinkedList_t* list,int val)
{
Node_t* newCode = malloc(sizeof(Node_t));
if (NULL == newCode)
{
return false;
}
newCode->val = val;
if (list->head == NULL)
{
newCode->next = NULL;
list->head = newCode;
//list->size++;
}
else
{
newCode->next = list->head;
list->head = newCode;
}
return true;
}
//删除结点
bool removenode(LinkedList_t* list, int val)
{
Node_t* prev = NULL;
Node_t* curr = list->head;
while (curr != NULL && curr->val != val)
{
prev = curr;
curr = curr->next;
}
if (curr == NULL)
{
return false;
}
//删除头节点
if (prev == NULL)
{
list->head = curr->next;
}
else
{
prev->next = curr->next;
}
free(curr);
return true;
}
//回收链表内存
void destroy(LinkedList_t* list)
{
while (list->head)
{
Node_t* p = list->head;
list->head = list->head->next;
free(p);
}
free(list);
}