📄 listlearn.c
字号:
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
#include "stdio.h"
#include "assert.h"
#include "malloc.h"
#define VOS_NULL_PTR 0
#define ERR -1
#define OK 1
#define LIST_LEN_MAX 10
typedef struct LIST_NODE_TAG
{
int data;
struct LIST_NODE_TAG * next;
}LIST_NODE_STRU;
int _list_add_afterhead(LIST_NODE_STRU * pstHead, int data)
{
LIST_NODE_STRU * pstNode = VOS_NULL_PTR;
assert(pstHead);
pstNode = (LIST_NODE_STRU*)malloc(sizeof(LIST_NODE_STRU));
if (VOS_NULL_PTR == pstNode)
{
return ERR;
}
pstNode->data = data;
pstNode->next = pstHead->next;
pstHead->next = pstNode;
return OK;
}
void _list_delete(LIST_NODE_STRU* pre, LIST_NODE_STRU * cur)
{
assert(VOS_NULL_PTR!= pre && VOS_NULL_PTR!= cur);
pre->next = cur->next;
free(cur);
return;
}
void _list_insert_afternode(LIST_NODE_STRU * pre, LIST_NODE_STRU * node)
{
assert(pre && node);
node->next = pre->next;
pre->next = node;
}
LIST_NODE_STRU* _list_find_key(LIST_NODE_STRU * head, int key)
{
LIST_NODE_STRU * pstNode = VOS_NULL_PTR;
assert(head);
pstNode = head->next;
while (pstNode)
{
if (pstNode->data == key)
{
break;
}
pstNode = pstNode->next;
}
return pstNode;
}
LIST_NODE_STRU* _list_find_key_ex(LIST_NODE_STRU * head, int key, LIST_NODE_STRU ** ppre)
{
LIST_NODE_STRU * pstNode = VOS_NULL_PTR;
assert(head && ppre);
pstNode = head->next;
*ppre = head->next;
while (pstNode)
{
if (pstNode->data == key)
{
break;
}
*ppre = pstNode;
pstNode = pstNode->next;
}
return pstNode;
}
void _list_traver(LIST_NODE_STRU * head)
{
LIST_NODE_STRU * pstNode;
assert(head);
pstNode = head->next;
printf("\r\n>>\r\n");
while (VOS_NULL_PTR != pstNode)
{
printf("-%d",pstNode->data);
pstNode = pstNode->next;
}
return;
}
int main(void)
{
LIST_NODE_STRU * pstHead = VOS_NULL_PTR;
LIST_NODE_STRU * pstNode = VOS_NULL_PTR;
LIST_NODE_STRU * preNode = VOS_NULL_PTR;
int i;
int key = 4;
int insertdata = 21;
pstHead = (LIST_NODE_STRU*)malloc(sizeof(LIST_NODE_STRU));
if (VOS_NULL_PTR == pstHead)
{
return ERR;
}
pstHead->next = VOS_NULL_PTR;
/*创建链表*/
for (i = 0; i < LIST_LEN_MAX; i++)
{
_list_add_afterhead(pstHead, i);
}
_list_traver(pstHead);
pstNode = (LIST_NODE_STRU*)malloc(sizeof(LIST_NODE_STRU));
if (VOS_NULL_PTR == pstHead)
{
return ERR;
}
pstNode->data = insertdata;
_list_insert_afternode(_list_find_key(pstHead, key), pstNode);
_list_traver(pstHead);
pstNode = (LIST_NODE_STRU*)malloc(sizeof(LIST_NODE_STRU));
if (VOS_NULL_PTR == pstHead)
{
return ERR;
}
pstNode->data = 99;
_list_insert_afternode(_list_find_key(pstHead, 0), pstNode);
_list_traver(pstHead);
pstNode = _list_find_key_ex(pstHead, 21, &preNode);
_list_delete(preNode, pstNode);
_list_traver(pstHead);
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -