算:链表创建与查询

比较难理解,菜才要自己动手写啦,莫慌,,,,

 

版本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;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值