typedef struct Node
{
int data;
Node* next;
}Node,*List;
//链表结构体定义
void InitList(List plist)//单链表初始化
{
assert(plist!=NULL);
if(plist==NULL)
{
return;
}
plist->next=NULL;
}
static Node* GetNode(int val)
{
Node *pGet=(Node*)malloc(sizeof(Node));
assert(pGet!=NULL);
pGet->data=val;
pGet->next=NULL;
return pGet;
}
bool Insert_head(List plist, int val)//头插
{
assert(plist!=NULL);
if(plist==NULL)
{
return false;
}
Node* pGet=GetNode(val);
pGet->next=plist->next;
plist->next=pGet;
return true;
}
bool Insert_tail(List plist, int val)//尾插
{
assert(plist!=NULL);
if(plist==NULL)
{
return false;
}
Node* pGet=GetNode(val);
Node* p=plist;
while(p->next!=NULL)
{
p=p->next;
}
p->next=pGet;
return true;
}
Node* Search_pre(List plist, int key)//查找key的前驱
{
Node* p = plist;
for (; p->next != NULL; p = p->next)
{
if (p->next->data == key)
{
return p;
}
}
return false;
}
bool Delete(List plist, int key)//删除
{
Node* p = Search_pre(plist, key);
if (p == NULL)
{
return false;
}
Node* pDel = p->next;
p->next = pDel->next;
free(pDel);
pDel = NULL;
return true;
}
bool IsEmpty(List plist)//判空
{
if (plist->next == NULL)
{
return true;
}
return false;
}
void Destroy(List plist)//销毁
{
Node* pDel;
while (plist->next != NULL)
{
pDel = plist->next;
plist->next = pDel->next;
free(pDel);
}
pDel == NULL;
}
int GetLength(List plist)//长度
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
int count = 0;
Node* p = plist;
while (p->next != NULL)
{
p = p->next;
count++;
}
return count;
}
void Reverse(List plist)//逆置
{
Node* p = plist->next;
Node* p_pre = NULL;
Node* p_next = NULL;
while (p != NULL)
{
p_next = p->next;
p->next = p_pre;
p_pre = p;
p = p_next;
}
plist->next = p_pre;
}
单链表各种操作
最新推荐文章于 2020-04-15 17:19:04 发布