链表的基本操作
//struct.cpp:定义控制台应用程序的入口点。//#include"stdafx.h"#include"stdio.h"#include"stdlib.h"#include"string.h"#defineRETURN_OK0#defineRETURN_ERROR1#defineMAX_LEN11#defineMY_RAND_MAX26typedefstructtag_Info_S{inta;intb;intc;charname[MAX_LEN];}Info_S;typedefstructtag_MyList_S{Info_SstInfo;structtag_MyList_S*next;}MyList_S;MyList_S*g_pHead;intAllocOneNode(MyList_S**ppNewNode);voidInitOneNode(MyList_S*pNode);intInitList(MyList_S**ppHead);intDelList(MyList_S**ppHead);intAllocOneNode(MyList_S**ppNewNode){MyList_S*pTemp=(MyList_S*)malloc(sizeof(MyList_S));if(NULL==pTemp){*ppNewNode=NULL;returnRETURN_ERROR;}*ppNewNode=pTemp;returnRETURN_OK;}voidInitOneNode(MyList_S*pNode){inti=0;intlen=0;pNode->stInfo.a=rand()%MY_RAND_MAX;//rand的使用不规范pNode->stInfo.b=rand()%MY_RAND_MAX;pNode->stInfo.c=rand()%MY_RAND_MAX;len=(rand()%(MAX_LEN-2))+1;//1---10for(i=0;i<len;i++){pNode->stInfo.name[i]=rand()%MY_RAND_MAX+'A';}pNode->stInfo.name[len]='\0';pNode->next=NULL;return;}intInitList(MyList_S**ppHead){intret=RETURN_ERROR;MyList_S*pNew=NULL;if(NULL!=*ppHead)//需要先销毁{DelList(ppHead);}ret=AllocOneNode(&pNew);if(RETURN_OK!=ret){returnRETURN_ERROR;}memset(pNew,0,sizeof(MyList_S));pNew->next=NULL;*ppHead=pNew;returnRETURN_OK;}intDelList(MyList_S**ppHead){MyList_S*pFreeNode=NULL;MyList_S*pTmpNode=NULL;if(NULL==*ppHead){returnRETURN_ERROR;}pTmpNode=*ppHead;while(pTmpNode){pFreeNode=pTmpNode;pTmpNode=pTmpNode->next;free(pFreeNode);}*ppHead=NULL;returnRETURN_OK;}intGetListLen(MyList_S*pHead,int*ListLen){intlen=0;if(NULL==pHead){returnRETURN_ERROR;}pHead=pHead->next;while(pHead){len++;pHead=pHead->next;}*ListLen=len;returnRETURN_OK;}intAddNodeAtListTrail(MyList_S**ppHead,MyList_S*pNewNode){MyList_S*pTemp=NULL;if(NULL==*ppHead){returnRETURN_ERROR;}//当前链表为空时,头节点的后面插入if(NULL==(*ppHead)->next){(*ppHead)->next=pNewNode;returnRETURN_OK;}pTemp=(*ppHead)->next;while(pTemp->next){pTemp=pTemp->next;}pTemp->next=pNewNode;pNewNode->next=NULL;returnRETURN_OK;}intAddNodeAtListHead(MyList_S**ppHead,MyList_S*pNewNode){if(NULL==*ppHead){returnRETURN_ERROR;}pNewNode->next=(*ppHead)->next;(*ppHead)->next=pNewNode;returnRETURN_OK;}//链表从0开始编号,pNewNode插入后在第iLocal位置intAddNodeAtListLocal(MyList_S**ppHead,MyList_S*pNewNode,intiLocal){intlen=0;inti=0;MyList_S*pTemp=NULL;if(NULL==*ppHead){returnRETURN_ERROR;}if(iLocal==0){AddNodeAtListHead(ppHead,pNewNode);returnRETURN_OK;}GetListLen(*ppHead,&len);if(iLocal>len)//由于从0开始编号,所以iLocal最大为len{returnRETURN_ERROR;}//查找第编号为ilocal-1的有效节点的位置pTemp=(*ppHead);while(i<iLocal){pTemp=pTemp->next;i++;}pNewNode->next=pTemp->next;pTemp->next=pNewNode;returnRETURN_OK;}intDelOneNodeAtListTrail(MyList_S**ppHead,MyList_S*pDelNode){MyList_S*pPre=NULL;MyList_S*pCur=NULL;if(NULL==*ppHead||NULL==(*ppHead)->next){returnRETURN_ERROR;}pCur=(*ppHead)->next;if(NULL==pCur->next)//如果只有一个节点,可以使用DelOneNodeAtListHead删除{memcpy_s(&(pDelNode->stInfo),sizeof(Info_S),&(pCur->stInfo),sizeof(Info_S));(*ppHead)->next=NULL;free(pCur);pCur=NULL;}while(pCur->next)//pCur->next=NULL,pCur为最后一个有效节点{pPre=pCur;pCur=pCur->next;}pPre->next=NULL;memcpy_s(&(pDelNode->stInfo),sizeof(Info_S),&(pCur->stInfo),sizeof(Info_S));free(pCur);pCur=NULL;returnRETURN_OK;}intDelOneNodeAtListHead(MyList_S**ppHead,MyList_S*pDelNode){MyList_S*pCur=NULL;if(NULL==*ppHead||NULL==(*ppHead)->next){returnRETURN_ERROR;}pCur=(*ppHead)->next;(*ppHead)->next=pCur->next;memcpy_s(&(pDelNode->stInfo),sizeof(Info_S),&(pCur->stInfo),sizeof(Info_S));free(pCur);returnRETURN_OK;}intDelNodeAtListLocal(MyList_S**ppHead,MyList_S*pDelNode,intiLocal){inti=0;intlen=0;MyList_S*pTemp=NULL;MyList_S*pFreeNode=NULL;if(NULL==*ppHead||NULL==(*ppHead)->next){returnRETURN_ERROR;}if(iLocal==0){DelOneNodeAtListHead(ppHead,pDelNode);returnRETURN_OK;}(void)GetListLen(*ppHead,&len);//不用判断返回值了,此处是内部调用,前面的判断可以保证此处可以获取到长度if(iLocal>=len){returnRETURN_ERROR;//最大到len-1}pTemp=*ppHead;while(i<iLocal){pTemp=pTemp->next;i++;}pFreeNode=pTemp->next;memcpy_s(&(pDelNode->stInfo),sizeof(Info_S),&(pFreeNode->stInfo),sizeof(Info_S));pTemp->next=pFreeNode->next;free(pFreeNode);pFreeNode=NULL;returnRETURN_OK;}intRevertList(MyList_S*pHead)//头指针的指向不发生变化,变化的是pHead->next{MyList_S*pPre=NULL;MyList_S*pCur=NULL;if(NULL==pHead||NULL==pHead->next){returnRETURN_ERROR;}pPre=pHead->next;pHead->next=NULL;while(pPre){pCur=pPre->next;pPre->next=NULL;AddNodeAtListHead(&pHead,pPre);pPre=pCur;}returnRETURN_OK;}/*排序,先按a排序,a相同按照b排序,b相同按照c排序,c相同按照name排序*/intSrcCopyInfoToDst(void*dst,void*src){if(NULL==dst||NULL==src){returnRETURN_ERROR;}memcpy_s(dst,sizeof(Info_S),src,sizeof(Info_S));returnRETURN_OK;}intsort(MyList_S*pHead){MyList_S*pCur=NULL;MyList_S*pNext=NULL;Info_SstInfo={0};if(NULL==pHead||NULL==pHead->next){returnRETURN_ERROR;}pCur=pHead->next;if(NULL==pCur){returnRETURN_OK;}//排序,比较搓考虑优化while(pCur){pNext=pCur;while(pNext){if(pNext->stInfo.a>pCur->stInfo.a){SrcCopyInfoToDst(&stInfo,&(pCur->stInfo));SrcCopyInfoToDst(&(pCur->stInfo),&(pNext->stInfo));SrcCopyInfoToDst(&(pNext->stInfo),&stInfo);}elseif(pNext->stInfo.a==pCur->stInfo.a){if(pNext->stInfo.b>pCur->stInfo.b){SrcCopyInfoToDst(&stInfo,&(pCur->stInfo));SrcCopyInfoToDst(&(pCur->stInfo),&(pNext->stInfo));SrcCopyInfoToDst(&(pNext->stInfo),&stInfo);}elseif(pNext->stInfo.b==pCur->stInfo.b){if(pNext->stInfo.c>pCur->stInfo.c){SrcCopyInfoToDst(&stInfo,&(pCur->stInfo));SrcCopyInfoToDst(&(pCur->stInfo),&(pNext->stInfo));SrcCopyInfoToDst(&(pNext->stInfo),&stInfo);}elseif(pNext->stInfo.c==pCur->stInfo.c){if(strcmp(pCur->stInfo.name,pNext->stInfo.name)>0){SrcCopyInfoToDst(&stInfo,&(pCur->stInfo));SrcCopyInfoToDst(&(pCur->stInfo),&(pNext->stInfo));SrcCopyInfoToDst(&(pNext->stInfo),&stInfo);}}}}pNext=pNext->next;}pCur=pCur->next;}returnRETURN_OK;}voidprintList(MyList_S*pHead){if(NULL==pHead){return;}pHead=pHead->next;while(pHead){printf("a=%d,b=%d,c=%d,name=%s\n",pHead->stInfo.a,pHead->stInfo.b,pHead->stInfo.c,pHead->stInfo.name);pHead=pHead->next;}printf("-----------------------------------------------------------------------------------\n");return;}int_tmain(intargc,_TCHAR*argv[]){inti=0;intlen=0;intret=RETURN_ERROR;MyList_S*pTemp=NULL;InitList(&g_pHead);for(i=0;i<10;i++){ret=AllocOneNode(&pTemp);if(RETURN_OK!=ret){printf("allocnodefailed!\n");returnRETURN_ERROR;}InitOneNode(pTemp);if(i%2){AddNodeAtListTrail(&g_pHead,pTemp);}else{AddNodeAtListHead(&g_pHead,pTemp);}}GetListLen(g_pHead,&len);printf("len=%d\n",len);printList(g_pHead);ret=AllocOneNode(&pTemp);if(RETURN_OK!=ret){printf("allocnodefailed!\n");returnRETURN_ERROR;}InitOneNode(pTemp);AddNodeAtListLocal(&g_pHead,pTemp,10);GetListLen(g_pHead,&len);printf("len=%d\n",len);printList(g_pHead);DelNodeAtListLocal(&g_pHead,pTemp,10);GetListLen(g_pHead,&len);printf("len=%d\n",len);printList(g_pHead);RevertList(g_pHead);printList(g_pHead);sort(g_pHead);printList(g_pHead);return0;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。