单项链表cpp
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include "listtest.h"
UINT32 CreatList(NodeList *L)
{
NodeList *p =NULL;
NodeList *temp =L;
if (NULL == L)
{
return ERROR;
}
L->pNodeNext=NULL;
p =(NodeList *)malloc(sizeof(NodeList));
if (NULL == p)
{
return NULL;
}
scanf("%d",&p->Value);
while(0!=p->Value)
{
p->pNodeNext=temp->pNodeNext;
temp->pNodeNext=p;
temp=temp->pNodeNext;
p=(NodeList *)malloc(sizeof(NodeList));
scanf("%d",&p->Value);
}
return OK;
}
UINT32 GetListLen(NodeList *L)
{
int len =0 ;
NodeList *temp;
if (NULL==L)
{
return ERROR;
}
temp=L;
while (temp->pNodeNext)
{
temp=temp->pNodeNext;
len++;
}
return len;
}
UINT32 AddList(NodeList *L,ElemType value,int Location)
{
int len =GetListLen(L);
if(NULL == L)
{
return ERROR;
}
if(Location >len)
{
return ERROR;
}
NodeList *temp =NULL;
temp =(NodeList *)malloc(sizeof(NodeList));
temp->Value=value;
NodeList *temp2 = NULL;
temp2 = L;
while (Location)
{
temp2=temp2->pNodeNext;
Location--;
}
temp->pNodeNext=temp2->pNodeNext;
temp2->pNodeNext=temp;
return OK;
}
void ShowList(NodeList *L)
{
if (NULL == L)
{
return ;
}
NodeList *temp =L;
while(NULL != temp->pNodeNext)
{
temp=temp->pNodeNext;
printf("%d\r\n",temp->Value);
}
return;
}
UINT32 DelListNode(NodeList *L,int Location)
{
if (NULL == L)
{
return ERROR;
}
NodeList *temp =L;
NodeList *temp2 =L;
if(Location > GetListLen(L))
{
return ERROR;
}
while(Location)
{
temp =temp->pNodeNext;
Location--;
}
temp2=temp->pNodeNext;
temp->pNodeNext=temp2->pNodeNext;
free(temp2);
return ERROR;
}
UINT32 ReverseList(NodeList *L)
{
//单链表逆置
/*L变空尾部插法*/
NodeList *p,*q;
p=L->pNodeNext;
L->pNodeNext =NULL;
while(p)
{
q=p;
p=p->pNodeNext;
q->pNodeNext=L->pNodeNext;
L->pNodeNext=q;
}
return OK;
}
NodeList * MergeList(NodeList *L1,NodeList *L2)
{
NodeList *L3;
NodeList *pa,*pb,*pc;
pa=L1->pNodeNext;
pb=L2->pNodeNext;
L3=pc=L1;
while(pa&&pb)
{
if (pa->Value<=pb->Value)
{
pc->pNodeNext=pa;
pc=pa;
pa=pa->pNodeNext;
}
else
{
pc->pNodeNext=pb;
}
}
pc->pNodeNext=pa?pa:pb;
return L3;
}
void SortList(NodeList *L)
{
ElemType tmp;
if((NULL==L)||(L->pNodeNext ==NULL))
{
return ;
}
NodeList *temp1=L->pNodeNext;
NodeList *temp2=temp1->pNodeNext;
for (;temp1!=NULL;temp1=temp1->pNodeNext)
{
for (temp2=temp1->pNodeNext;temp2!=NULL;temp2=temp2->pNodeNext)
{
if (temp1->Value>temp2->Value)
{
tmp=temp1->Value;
temp1->Value=temp2->Value;
temp2->Value=tmp;
}
}
}
}
int main()
{
NodeList *ExList=NULL;
ExList=(NodeList *)malloc(sizeof(NodeList));
if (NULL == ExList)
{
return ERROR;
}
NodeList *ExList2=NULL;
ExList2=(NodeList *)malloc(sizeof(NodeList));
if (NULL == ExList2)
{
return ERROR;
}
NodeList *ExList3=NULL;
ExList->pNodeNext =NULL;
ExList2->pNodeNext =NULL;
/*CreatList(ExList);
ShowList(ExList);
AddList(ExList,128,3);
ShowList(ExList);
DelListNode(ExList,3);
ShowList(ExList);
printf("===========\r\n");
ReverseList(ExList);
ShowList(ExList);
CreatList(ExList2);
ShowList(ExList2);
ExList3=MergeList(ExList,ExList2);
ShowList(ExList3);*/
CreatList(ExList);
ShowList(ExList);
printf("sort=============\r\n");
SortList(ExList);
ShowList(ExList);
printf("len= %d",GetListLen(ExList));
return OK;
}