線形リストのリンクリストの追加、削除、チェック、変更などの操作
#include<stdio.h>
typedef int LDateType;
typedef struct listNode
{
LDateType _date;
struct listNode* _next;
}listNode;
typedef struct list
{
listNode* _head;
}list;
void InistList(list* lst)
{
if (lst == NULL)
return;
lst->_head = NULL;
}
listNode* creatNode(LDateType val)
{
listNode* node = (listNode*)malloc(sizeof(listNode));
node->_date = val;
node->_next = NULL;
return node;
}
void listPushBack(list* lst, LDateType val)
{
if (lst == NULL)
return;
if (lst->_head==NULL)
{
lst->_head = creatNode(val);
}
else
{
listNode* tail = lst->_head;
while (tail->_next != NULL)
{
tail = tail->_next;
}
tail->_next = creatNode(val);
}
}
void listPopBack(list* lst)
{
if (lst == NULL||lst->_head==NULL)return;
struct listNode* tail = lst->_head;
struct listNode* prev = NULL;
while (tail->_next != NULL)
{
prev = tail->_next;
tail = tail->_next;
}
free(tail);
if (prev == NULL)
lst->_head = NULL;
else
prev->_next = NULL;
}
void listPushFront(list* lst, LDateType val)
{
if (lst == NULL)return;
if (lst->_head == NULL)
lst->_head = creatNode(val);
else
{
listNode* node = creatNode(val);
listNode* next = lst->_head;
lst->_head = node;
node->_next = next;
}
}
void listPopFront(list *lst)
{
if (lst == NULL || lst->_head == NULL)return;
listNode* next = lst->_head->_next;
free(lst->_head);
lst->_head = next;
}
void listInsertAfter(listNode* cur, LDateType val)
{
listNode* node = creatNode(val);
listNode* next = cur->_next;
cur->_next = node;
node->_next = next;
}
void listEraseAfter(listNode* cur)
{
listNode* next = cur->_next;
if (next == NULL)return;
listNode* nextnext = next->_next;
free(next);
cur->_next=nextnext;
}
listNode* listFind(list* lst,LDateType val)
{
if (lst == NULL || lst->_head == NULL)return;
listNode* cur = lst->_head;
while (cur)
{
if (cur->_date == val)
return cur;
cur = cur->_next;
}
return NULL;
}
int listSize(list* lst)
{
if (lst == NULL)return;
int count = 0;
listNode* next = lst->_head;
while (next)
{
next = next->_next;
count++;
}
return count;
}
void listDestroy(list* lst)
{
if (lst == NULL||lst->_head==NULL)return;
listNode* ps = lst->_head,*next = NULL;
while (ps)
{
next = ps->_next;
free(ps);
ps = next;
}
lst->_head = NULL;
}