网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

list的c实现

#pragma once

成都创新互联自2013年创立以来,先为北戴河等服务建站,北戴河等地企业,进行企业商务咨询服务。为北戴河企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

#include

#include

#include

typedef struct ListNode

{

int _data;

struct ListNode* _next;

}ListNode;

void InitList(ListNode** pHead)

{

*pHead = NULL;

}

void DestoryList(ListNode** pHead)

{

ListNode* tmp = *pHead;

while (tmp)

{

free(tmp);

tmp = tmp->_next;

}

*pHead = NULL;

}

ListNode* BuyNode(int data)

{

ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));

assert(newNode);

newNode->_data = data;

newNode->_next = NULL;

return newNode;

}

void PushBack(ListNode** pHead, int data)

{

assert(pHead);

if (*pHead == NULL)

{

*pHead = BuyNode(data);

return;

}

ListNode* tmp = *pHead;

while (tmp)

{

tmp->_next;

}

tmp = BuyNode(data);

}

void PrintList(ListNode* pHead)

{

assert(pHead);

ListNode* tmp = pHead;

while (tmp)

{

printf("%d->", tmp->_data);

tmp = tmp->_next;

}

printf("%p\n", tmp);

}

void PushFront(ListNode** pHead, int data)

{

assert(pHead);

ListNode* tmp = *pHead;

*pHead = BuyNode(data);

(*pHead)->_next = tmp;

}

void PopBack(ListNode** pHead)

{

assert(pHead);

if (*pHead == NULL)

return;

ListNode* tmp = *pHead;

while (tmp->_next != NULL)

{

tmp = tmp->_next;

}

free(tmp);

tmp = NULL;

}

void PopFront(ListNode** pHead)

{

assert(pHead);

if (*pHead == NULL)

return;

ListNode* del = *pHead;

*pHead = (*pHead)->_next;

free(del);

}

ListNode* FindNode(ListNode** pHead, int data)

{

assert(pHead);

if (*pHead == NULL)

return NULL;

ListNode* tmp = *pHead;

while (tmp->_next != NULL)

{

if (tmp->_data == data)

return tmp;

tmp = tmp->_next;

}

return NULL;

}

void Insert(ListNode* pos, int data)

{

assert(pos);

ListNode*newnode = BuyNode(data);

newnode->_next = pos->_next;

pos->_next = newnode;

}

void Erase(ListNode** pHead, ListNode*pos)

{

assert(pHead);

assert(pos);

if (*pHead == NULL)

return;

ListNode* tmp = *pHead;

while (tmp->_next != NULL)

{

if (tmp->_next == pos)

{

tmp->_next = pos->_next;

free(pos);

break;

}

tmp = tmp->_next;

}

}

void DelNonTailNode(ListNode* pos)

{

assert(pos&&pos->_next);

ListNode* next = pos->_next->_next;

pos->_data = pos->_next->_data;

free(pos->_next);

pos->_next = next;

}

void remove(ListNode** pHead, int data)

{

assert(pHead);

if (*pHead == NULL)

return;

ListNode* del = FindNode(pHead,data);

if (del != NULL)

{

Erase(pHead, del);

}

return;

}

void Reverse(ListNode** pHead)

{

ListNode* cur = *pHead;

ListNode* head = NULL;

ListNode* tmp;

while (cur)

{

tmp = cur;

cur = cur->_next;

tmp->_next = head;

head = tmp;

}

*pHead = head;

}

void InsertFrontNode(ListNode* pos,int data)//假装加到pos前面其实就是data交换

{

assert(pos);

ListNode* newnode = BuyNode(pos->_data);

newnode->_next = pos->_next;

pos->_next = newnode;

pos->_data = data;

}

ListNode* FindMidNode(ListNode** pHead)

{

assert(pHead);

if (*pHead == NULL)

{

return NULL;

}

ListNode* slow = *pHead;

ListNode* fast = *pHead;

while (fast->_next&&fast)

{

slow = slow->_next;

fast = fast->_next->_next;

}

return slow;

}


分享文章:list的c实现
文章源于:http://bjjierui.cn/article/jjhpgd.html

其他资讯