比较难理解,菜才要自己动手写啦,莫慌,,,,
版本1,
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//define
typedef struct NodeList{
int mData;
struct NodeList* mNext;
}PList;
void initNL()
{
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
Ptmp->mData=0;
Ptmp->mNext=NULL;
printf("NL init\n mData:%d,mNext:%p",Ptmp->mData,Ptmp->mNext);
}
struct NodeList* createNewNode()
{
int data=0;
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
Ptmp->mData=data;
Ptmp->mNext=NULL;
printf("NL init\n mData:%d,mNext:%p\n",Ptmp->mData,Ptmp->mNext);
return Ptmp;
}
struct NodeList* NewNodeMore(int count)
{
int data=0;
PList* Phead=NULL;
PList* PCur=(PList*)malloc(sizeof(NodeList));
Phead=PCur;
//PCur=Phead;
//printf("\n mData:%d,mNext:%p\n",PCur->mData,PCur->mNext);
Phead->mNext=PCur;
printf("\n--------------------------------\n");
printf("\n Phead->mData:%d,Phead:%p\n",Phead->mData,Phead);
printf("\n Phead->mData:%d,Phead->mNext:%p\n",Phead->mData,Phead->mNext);
PCur->mData=data;
PCur->mNext=NULL;
printf("\n--------------------------------\n");
while(count--)
{
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
PCur->mNext=Ptmp;
Ptmp->mData=data++;
Ptmp->mNext=NULL;
PCur=Ptmp;//移动当前指针到下一个
printf("\n PCur:%p, Ptmp:%p,PCur->mData:%d,PCur->mNext:%p\n",PCur,Ptmp,PCur->mData,PCur->mNext);
}
return Phead;
}
int main()
{
//initNL();
PList* pcurrent=NewNodeMore(9);
printf("\n--------------------------------\n");
while(NULL!=pcurrent->mNext)
//for(int i=0;i<5;i++)
{
//printf("\n----------------enter----------------\n");
pcurrent=pcurrent->mNext;
printf("\n pcurrent:%p,pcurrent->mData:%d,pcurrent->mNext:%p\n",pcurrent,pcurrent->mData,pcurrent->mNext);
//printf("\n-----------------finish---------------\n");
}
printf("\n--------------------------------\n");
return 0;
}
链表的创建与赋值,合并,
很乱,,,
版本2,
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//define
typedef struct NodeList{
int mData;
struct NodeList* mNext;
}PList;
void initNL()
{
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
Ptmp->mData=0;
Ptmp->mNext=NULL;
printf("NL init\n mData:%d,mNext:%p",Ptmp->mData,Ptmp->mNext);
}
struct NodeList* createNewNode()
{
//int data=0;
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
//Ptmp->mData=data;
Ptmp->mNext=NULL;
printf("NL init\n mData:%d,mNext:%p\n",Ptmp->mData,Ptmp->mNext);
return Ptmp;
}
struct NodeList* NewNodeMore(int arry[],int lenth)
{//can set value at the time of create
int i=0;
PList* Phead=NULL;
PList* PCur=(PList*)malloc(sizeof(NodeList));
PCur->mNext=NULL;
Phead=PCur;
Phead->mNext=PCur;
printf("\n--------------------------------\n");
printf("\n Phead->mData:%d,Phead:%p\n",Phead->mData,Phead);
printf("\n Phead->mData:%d,Phead->mNext:%p\n",Phead->mData,Phead->mNext);
printf("\n--------------------------------\n");
while(lenth--)
{
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
PCur->mNext=Ptmp;
Ptmp->mData=arry[i++];
Ptmp->mNext=NULL;
PCur=Ptmp;//移动当前指针到下一个
//printf("\n PCur:%p, Ptmp:%p,PCur->mData:%d,PCur->mNext:%p\n",PCur,Ptmp,PCur->mData,PCur->mNext);
}
return Phead;
}
int putvalue(NodeList* phead,int arry[],int lenth)
{
PList* pcurrent=phead;//get the head of list
int i=0;
printf("\n--------------------------------\n");
//printf("\n----------------enter----------------\n");
while(NULL!=pcurrent->mNext)
{
pcurrent=pcurrent->mNext;
pcurrent->mData=arry[i++];//顺序不能反,不然会都mdata=0
printf("\n pcurrent:%p,pcurrent->mData:%d,pcurrent->mNext:%p\n",pcurrent,pcurrent->mData,pcurrent->mNext);
}
//printf("\n-----------------finish---------------\n");
printf("\n--------------------------------\n");
return 0;
}
int displaylist(struct NodeList* li)
{
PList* pcurrent=li;
printf("\n--------------------------------\n");
printf("\nlist: ");
//printf("\n----------------enter----------------\n");
while(NULL!=pcurrent->mNext)
{
pcurrent=pcurrent->mNext;
printf("%d,",pcurrent->mData);
//pcurrent->mData=arry[i++];//顺序不能反,不然会都mdata=0
//printf("\n pcurrent:%p,pcurrent->mData:%d,pcurrent->mNext:%p\n",pcurrent,pcurrent->mData,pcurrent->mNext);
}
//printf("\n-----------------finish---------------\n");
printf("\n--------------------------------\n");
return 0;
}
struct NodeList* merge2list(struct NodeList* li1,struct NodeList* li2)
{
printf("\n--------------------------------merge enter \n");
struct NodeList* PCur=NULL;
PCur=li1;
while(NULL!=PCur->mNext)
{
PCur=PCur->mNext;//move the cursor
}//reach the tail of list
PCur->mNext=li2;//point tail of li1 to head of li2
return li1;
}
int main()
{
//initNL();
int i=0;
int arry[]={5,8,11,2,9,6,4};
int arry2[]={6,9,1,4,8,2};
PList* pcurrent=NewNodeMore(arry,sizeof(arry)/sizeof(arry[0]));//get the head of list
printf("\n\n\n\n--------------------------------\n");
PList* pcurrent2=NewNodeMore(arry2,sizeof(arry2)/sizeof(arry2[0]));//get the head of list
displaylist(pcurrent);
displaylist(pcurrent2);
PList* pmerge=merge2list(pcurrent,pcurrent2);//get the head of list
displaylist(pmerge);
return 0;
}
继续优化:
版本3,要清晰,我的哥,,,,
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//define
typedef struct NodeList{
int mData;
struct NodeList* mNext;
}PList;
void initNodeList()
{
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
Ptmp->mData=0;
Ptmp->mNext=NULL;
printf("NL init\n mData:%d,mNext:%p",Ptmp->mData,Ptmp->mNext);
}
struct NodeList* createNewNode()
{
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
Ptmp->mNext=NULL;
return Ptmp;
}
struct NodeList* NewNodeMore(int arry[],int lenth)
{//can set value at the time of create
int i=0;
PList* PCur=NULL;//游标指针
PList* Phead=(PList*)malloc(sizeof(NodeList));//创建头节点
Phead->mNext=NULL;
PCur=Phead;//游标指向头节点
/*
* printf("\n--------------------------------\n");
* printf("\n Phead->mData:%d,Phead:%p\n",Phead->mData,Phead);
* printf("\n Phead->mData:%d,Phead->mNext:%p\n",Phead->mData,Phead->mNext);
*
*/
while(lenth--)
{
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
Ptmp->mNext=NULL;
Ptmp->mData=arry[i++];
PCur->mNext=Ptmp;//游标的next指向新创建的节点
PCur=Ptmp;//移动当前指针到下一个
//printf("\n PCur:%p, Ptmp:%p,PCur->mData:%d,PCur->mNext:%p\n",PCur,Ptmp,PCur->mData,PCur->mNext);
}
printf("\n--------------------------------\n");
return Phead;
}
int displaylist(struct NodeList* li)
{
PList* pcurrent=li;
printf("\n--------------------------------\n");
printf("\nlist: ");
//printf("\n----------------enter----------------\n");
while(NULL!=pcurrent->mNext)
{
pcurrent=pcurrent->mNext;
printf("%d,",pcurrent->mData);
//printf("\n pcurrent:%p,pcurrent->mData:%d,pcurrent->mNext:%p\n",pcurrent,pcurrent->mData,pcurrent->mNext);
}
//printf("\n-----------------finish---------------\n");
printf("\n--------------------------------\n");
return 0;
}
struct NodeList* merge2list(struct NodeList* li1,struct NodeList* li2)
{
printf("\n--------------------------------merge enter \n");
struct NodeList* PCur=NULL;
PCur=li1;
while(NULL!=PCur->mNext)
{
PCur=PCur->mNext;//move the cursor
}//reach the tail of list
PCur->mNext=li2;//point tail of li1 to head of li2
return li1;
}
int main()
{
//initNL();
int i=0;
int arry[]={5,8,11,2,9,6,4};
int arry2[]={6,9,1,4,8,2};
PList* pcurrent=NewNodeMore(arry,sizeof(arry)/sizeof(arry[0]));//get the head of list
printf("\n\n\n\n--------------------------------\n");
PList* pcurrent2=NewNodeMore(arry2,sizeof(arry2)/sizeof(arry2[0]));//get the head of list
displaylist(pcurrent);
displaylist(pcurrent2);
PList* pmerge=merge2list(pcurrent,pcurrent2);//get the head of list
displaylist(pmerge);
return 0;
}
版本4,
再改
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//define
typedef struct NodeList{
int mData;
struct NodeList* mNext;
}PList;
void initNodeList()
{
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
Ptmp->mData=0;
Ptmp->mNext=NULL;
printf("NL init\n mData:%d,mNext:%p",Ptmp->mData,Ptmp->mNext);
}
struct NodeList* createNewNode()
{
PList* Ptmp=(PList*)malloc(sizeof(NodeList));
Ptmp->mNext=NULL;
return Ptmp;
}
struct NodeList* NewNodeMore(int arry[],int lenth)
{//can set value at the time of create
int i=0;
PList* PCur=NULL;//游标指针
PList* Phead=createNewNode();//创建头节点
PCur=Phead;//游标指向头节点
/*
* printf("\n--------------------------------\n");
* printf("\n Phead->mData:%d,Phead:%p\n",Phead->mData,Phead);
* printf("\n Phead->mData:%d,Phead->mNext:%p\n",Phead->mData,Phead->mNext);
*
*/
while(lenth--)
{
PList* Ptmp=createNewNode();
Ptmp->mData=arry[i++];
PCur->mNext=Ptmp;//游标的next指向新创建的节点
PCur=Ptmp;//移动当前指针到下一个
//printf("\n PCur:%p, Ptmp:%p,PCur->mData:%d,PCur->mNext:%p\n",PCur,Ptmp,PCur->mData,PCur->mNext);
}
printf("\n--------------------------------\n");
return Phead;
}
int displaylist(struct NodeList* li)
{
PList* pcurrent=li;
printf("\nlist: ");
//printf("\n----------------enter----------------\n");
while(NULL!=pcurrent->mNext)
{
pcurrent=pcurrent->mNext;
printf("%d,",pcurrent->mData);
//printf("\n pcurrent:%p,pcurrent->mData:%d,pcurrent->mNext:%p\n",pcurrent,pcurrent->mData,pcurrent->mNext);
}
//printf("\n-----------------finish---------------\n");
printf("\n--------------------------------\n");
return 0;
}
struct NodeList* merge2list(struct NodeList* li1,struct NodeList* li2)
{
printf("\n--------------------------------merge enter \n");
struct NodeList* PCur=NULL;
PCur=li1;
while(NULL!=PCur->mNext)
{
PCur=PCur->mNext;//move the cursor
}//reach the tail of list
PCur->mNext=li2;//point tail of li1 to head of li2
return li1;
}
int main()
{
//initNodeList();
int i=0;
int arry[]={5,8,11,2,9,6,4};
int arry2[]={6,9,1,4,8,2};
PList* pcurrent=NewNodeMore(arry,sizeof(arry)/sizeof(arry[0]));//get the head of list
printf("\n\n--------------------------------\n");
PList* pcurrent2=NewNodeMore(arry2,sizeof(arry2)/sizeof(arry2[0]));//get the head of list
displaylist(pcurrent);
displaylist(pcurrent2);
PList* pmerge=merge2list(pcurrent,pcurrent2);//get the head of list
displaylist(pmerge);
return 0;
}