#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)//pretend to add to POS is actually the data exchange
{
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;
}
This article is from the "end-of-the-guest" blog, please be sure to keep this source http://zheng2048.blog.51cto.com/10612048/1792554
The C implementation of the list