温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

list的c实现

发布时间:2020-03-29 16:50:44 来源:网络 阅读:511 作者:zheng_feng 栏目:编程语言

#pragma once


#include<malloc.h>

#include<assert.h>

#include<stdio.h>


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;

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI