#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node{
int date;
struct node* pnext;
}strNode;
strNode *pHead = NULL;
void CreateList(int date)
{
strNode *pNode,*Head;
Head = pHead;
pNode = (strNode*)malloc(sizeof(strNode));
if(NULL == pNode)
{
printf("not enough memory!!");
return;
}
memset(pNode,0,sizeof(strNode));
pNode->date = date;
printf("Insert Data into linklist, date = %d.\n", date);
if(Head == NULL)
{
Head = pNode;
pNode->pnext = NULL;
}
else
{
pNode->pnext = Head->pnext;
Head->pnext = pNode;
}
pHead = Head;
}
void printLinklist()
{
printf("========================List===========================\n");
strNode* plocallhead = pHead;
while(plocallhead != NULL)
{
printf("输出链表的值:date = %d\n",plocallhead->date);
plocallhead = plocallhead->pnext;
}
}
void destoryLinklist()
{
strNode* plocallhead = pHead;
while(plocallhead != NULL)
{ printf("释放顺序:date = %d\n",plocallhead->date);
pHead = pHead->pnext;
free(plocallhead);
plocallhead = pHead;
}
}
void addnodelist()
{
}
int deleteNode(int k)
{
strNode* plocallhead = pHead;
strNode* plasthead = pHead;
while(plocallhead != NULL)
{
if(plocallhead->date == k)
{
if(plasthead == plocallhead)
{
plocallhead = plocallhead->pnext;
free(pHead);
pHead = plocallhead;
return 0;
}
else
{
plasthead->pnext = plocallhead->pnext;
free(plocallhead);
return 0;
}
}
plasthead = plocallhead;
plocallhead = plocallhead->pnext;
return -1;
}
}
int modifyNode(int k,int t)
{
strNode* plocallhead = pHead;
if(pHead == NULL) return -1;
while(plocallhead != NULL)
{
if(plocallhead->date == k)
{
plocallhead->date = t;
return 0;
}
plocallhead = plocallhead->pnext;
}
return -1;
}
strNode* searchNode(int k)
{
strNode* plocallhead = pHead;
if(pHead == NULL) return NULL;
while(plocallhead != NULL)
{
if(plocallhead->date == k) return plocallhead;
plocallhead = plocallhead->pnext;
}
return NULL;
}
int NodeNumber()
{
strNode* plocallhead = pHead;
int count = 0;
if(pHead == NULL) return 0;
while(plocallhead != NULL)
{
count+=1;
plocallhead = plocallhead->pnext;
}
return count;
}
void OrderList()
{
strNode* plocallhead = pHead->pnext;
strNode *p,*plasthead,*plasthead1;
for(;plocallhead->pnext;plasthead = plocallhead,plocallhead = plocallhead->pnext)
{
for(p = pHead; p->pnext!=plasthead->pnext; plasthead1 = p,p = p->pnext)
{
if(p->date>plocallhead->date)
{
if(p == pHead)
{
plasthead->pnext = plocallhead->pnext;
plocallhead->pnext = pHead;
pHead->pnext = plocallhead;
}
else
{
plasthead->pnext = plocallhead->pnext;
plocallhead->pnext = p;
p->pnext = plocallhead;
}
}
else break;
}
}
}
int main(void)
{ int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i,j,p;
int count;
strNode *ptemp;
for(i=0;i<10;i++)
{
CreateList(a[i]);
}
printLinklist();
CreateList(12);
printf("======================增加=========================\n");
printLinklist();
p = deleteNode(0);
printf("===========================删除=============================\n");
if(p == 0)
printf("=======================删除成功==================\n");
if(p == -1)
printf("=======================删除失败=====================\n");
printLinklist();
j = modifyNode(7,15);
if(j == 0)
printf("=======================修改成功==================\n");
if(j == -1)
printf("=======================修改失败=====================\n");
printf("=========================修改============================\n");
printLinklist();
printf("========================查询============================\n");
ptemp = searchNode(9);
printf("查询值得地址:0x%x, 值为:%d\n",(int)&ptemp->date,ptemp->date);
printLinklist();
count = NodeNumber();
printf("查询到的节点数量:%d\n",count);
printf("***************************************************************************");
OrderList();
printLinklist();
destoryLinklist();
return 0;
}