//链表的基本用法代码实现
/************************************************************************/
/* Created: 2014-03-02
/* Author: http://weibo.com/3088919800/profile
/************************************************************************/
#include "stdio.h"
#include "stdlib.h"
struct ListNode
{
int value;
ListNode * next;
};
ListNode* CreateListNode(const int& nNodeValue)
{
ListNode* pNode;
pNode = (ListNode*)malloc(sizeof(ListNode));
if (!pNode)
return NULL;
pNode->value = nNodeValue;
pNode->next = NULL;
return pNode;
}
//we need the head node value.
ListNode * CreateList(const int& nListHeadVal)
{
ListNode * li;
li = CreateListNode(nListHeadVal);
if (!li)
return NULL;
return li;
}
//free all nodes in the list
void FreeList(ListNode *li)
{
ListNode * pListNode, *pTmpNode;
pListNode = li;
while (pListNode)
{
pTmpNode = pListNode;
pListNode = pListNode->next;
free(pTmpNode);
}
li = NULL;
}
ListNode* AppendNode(ListNode* pList, const int &nVal)
{
ListNode* node;
ListNode* header;
node = CreateListNode(nVal);
if (!node)
return pList;
header = pList;
if (!header)
return node;
header = pList;
while(header->next)
header = header->next;
header->next = node;
return pList;
}
//if find return the node, else return null
ListNode* Find(ListNode* pList, const int& nVal)
{
ListNode* pListNode = NULL;
pListNode = pList;
while (pListNode)
{
if (pListNode->value == nVal)
break;
pListNode = pListNode->next;
}
return pListNode;
}
//insert decreasing 增减
ListNode* Insert(ListNode* pList, const int& nVal)
{
ListNode * pNewNode = NULL;
ListNode* pIterNode = NULL;
pNewNode = CreateListNode(nVal);
if (!pNewNode)
return pList;
if (!pList)
return pNewNode;
pIterNode = pList;
while (pIterNode->value > nVal && pIterNode->next && pIterNode->next->value > nVal)
pIterNode = pIterNode->next;
//if true ,it is the first node
if (pIterNode->value <= nVal)
{
pNewNode->next = pIterNode;
return pNewNode;
}
if (pIterNode->next)
pNewNode->next = pIterNode->next;
pIterNode->next = pNewNode;
return pList;
}
//insert Ascending,递增
ListNode* Insert2(ListNode* pList, const int& nVal)
{
ListNode * pNewNode = NULL;
ListNode* pIterNode = NULL;
pNewNode = CreateListNode(nVal);
if (!pNewNode)
return pList;
if (!pList)
return pNewNode;
pIterNode = pList;
while (pIterNode->value < nVal && pIterNode->next && pIterNode->next->value < nVal)
pIterNode= pIterNode->next;
//if true ,it is the first node
if (pIterNode->value > nVal)
{
pNewNode->next = pIterNode;
return pNewNode;
}
// insert the node.
if (pIterNode->next)
pNewNode->next = pIterNode->next;
pIterNode->next = pNewNode;
return pList;
}
void PrintList(ListNode* list)
{
ListNode* pIterNode;
pIterNode = list;
while (pIterNode)
{
printf("%d ", pIterNode->value);
pIterNode = pIterNode->next;
}
}
//链表倒置
ListNode* InvertList(ListNode* list)
{
ListNode *pIterNode, *pNewHead, *pTmpNode;
pTmpNode = list;
if (!pTmpNode || !(pTmpNode->next))
return list;
pNewHead = list;
pIterNode = list;
while(pIterNode->next)
{
pTmpNode = pIterNode->next->next;
pIterNode->next->next = pNewHead;
pNewHead = pIterNode->next;
pIterNode->next = pTmpNode;
}
return pNewHead;
}
int main(int argc, char* argv[])
{
ListNode* list = CreateList(12);
ListNode* pNode;
if (!list)
{
printf("create list error!");
exit(-1);
}
int i = 0;
for (int i = 0; i< 20; i++)
{
list = Insert2(list, i);
}
printf("
After insert list:
");
PrintList(list);
list = InvertList(list);
printf("
After invert the list
");
PrintList(list);
//test find .
pNode = Find(list, 8);
if (pNode)
{
printf("
find it:%d
", pNode->value);
}
else
{
printf("
not find it:%d
", 8);
}
//free all the list node
FreeList(list);
getchar();
return 0;
}