算:链表创建,插入,查询,修改,删除,排序,翻转

本文详细介绍了链表的基础操作,包括如何创建链表,进行节点的插入、查询、修改和删除。此外,还探讨了链表的排序算法和翻转技巧,特别强调了处理空链表的情况。
摘要由CSDN通过智能技术生成

菜,

todo:

重写:

1.翻转要重写,

2.查找第k个,查找倒数第k个,

3.排序,

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;//游标指向头节点
	while(lenth--)
	{
		PList* Ptmp=createNewNode();
		Ptmp->mData=arry[i++];

		PCur->mNext=Ptmp;//游标的next指向新创建的节点
		PCur=Ptmp;//移动当前指针到下一个
	}
	printf("\n--------------------------------\n");
	return Phead;
}


int displaylist(struct NodeList* li)
{
	PList* pcurrent=li;
	printf("\nlist: ");
	while(NULL!=pcurrent->mNext)
	{
		pcurrent=pcurrent->mNext;
		printf("%d,",pcurrent->mData);
	}
	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->mNext;//point tail of li1 to head of li2,head of li2->mData=0,
	return li1;
}

struct NodeList* findNode(struct NodeList* li,int value)
{
	NodeList* PCur=NULL;
	PCur=li;
	while(PCur->mData!=value )
	{
		if(!PCur->mNext)
		{
			printf("the value not found in this list!\n");
			break;
		}
		PCur=PCur->mNext;
	}
	printf("\n PCur:%p,PCur->mData:%d,PCur->mNext:%p\n",PCur,PCur->mData,PCur->mNext);
	return PCur;
}

struct NodeList* insertNode(struct NodeList* phead,struct NodeList* pos,int value)
{//head,tail ,inter 
	NodeList* PCur=NULL;
	PCur=phead;

	while(PCur->mNext!=pos)//insert infront of pos 
		//while(PCur!=pos)
	{
		PCur=PCur->mNext;
	}

	if(PCur->mNext==pos)//insert infront of pos 
		//if(PCur==pos)
	{//reach the end of list ,pos not founded
		printf("inserting...\n");

		PCur->mNext=createNewNode();
		PCur=PCur->mNext;//move cousor 
		PCur->mNext=pos;//point the newdoe's next to pos 
		PCur->mData=value;


		/*
		 *PCur=createNewNode();
		 *PCur->mNext=pos->mNext;//point the newdoe's next to pos's next 
		 *PCur->mData=value;
		 *pos->mNext=PCur;//insert PCur after pos  
		 */


		printf("\n (pos):%p,(pos)->mData:%d,pos->mNext:%p\n",
			   pos,pos->mData,pos->mNext);
		printf("\n PCur(new):%p,PCur(new)->mData:%d,PCur(new)->mNext:%p\n",
			   PCur,PCur->mData,PCur->mNext);

	}

	//printf("\n PCur:%p,PCur->mData:%d,PCur->mNext:%p\n",PCur,PCur->mData,PCur->mNext);
	return phead;
}



struct NodeList* modifyNode(struct NodeList* phead,int pos_val,int mod_val)
{//modify the first value
	NodeList* PCur=NULL;
	PCur=phead;

	while(PCur->mData!=pos_val)
	{
		PCur=PCur->mNext;
	}

	if(PCur->mNext) 
	{//reach the end of list ,pos not founded
		printf("value founded changing...\n");
		printf("\n PCur:%p,PCur->mData:%d,PCur->mNext:%p\n",
			   PCur,PCur->mData,PCur->mNext);
		PCur->mData=mod_val;
		printf("\n PCur:%p,PCur->mData:%d,PCur->mNext:%p\n",
			   PCur,PCur->mData,PCur->mNext);
	}
	return phead;
}

struct NodeList* deleteNode(struct NodeList* phead,struct NodeList* pos)
{//
	NodeList* PCur=NULL;
	PCur=phead;

	while(PCur->mNext!=pos)
	{
		PCur=PCur->mNext;
	}

	if(PCur->mNext) 
	{//reach the end of list ,pos not founded
		printf("pos founded,delting...\n");
		printf("\n pos:%p,pos->mData:%d,pos->mNext:%p,PCur->mData:%d,PCur->mNext:%p\n"
			   ,pos,pos->mData,pos->mNext,PCur->mData,PCur->mNext);

		PCur->mNext=pos->mNext;
		free(pos);
		printf("\n PCur:%p,PCur->mData:%d,PCur->mNext:%p\n",PCur,PCur->mData,PCur->mNext);
	}
	return phead;
}

int lenoflist(struct NodeList* phead)
{
	int len=0;
	NodeList* PCur=NULL;
	PCur=phead;
	while(PCur->mNext)
	{
		len++;
		PCur=PCur->mNext;
	}
	return len;

}


struct NodeList* sort(struct NodeList* phead)
{//the most foolish sort 
	NodeList* PCur=NULL;
	NodeList* PNext=NULL;
	int len=lenoflist(phead);

	printf("\nenter sort--------------------------------\n");
	for(int i=0;i<len;i++)
	{//goto the head again
		PCur=phead->mNext;
		PNext=PCur->mNext;

		for(int k=0;k<len;k++)
		{
			if(PCur->mData > PNext->mData)
			{//swap
				//printf("before %d %d ",PCur->mData , PNext->mData);	
				int temp=PCur->mData;
				PCur->mData=PNext->mData;
				PNext->mData=temp;
				//printf("after %d %d \n",PCur->mData , PNext->mData);	
			}			
			if(PNext->mNext)
			{//not reach the end of list
				PCur=PCur->mNext;
				PNext=PNext->mNext;
			}
		}
		displaylist(phead);
	}
	return phead;
}

struct NodeList* reverse(struct NodeList* phead)
{//
	NodeList* PCur=NULL;
	PCur=phead->mNext;
	int len=lenoflist(phead);
	int i=0;
	int temp[len+1]={0};

	printf("reverse:%d \n",len);
	while(PCur)
	{//not reach the end of list
		temp[i++]=PCur->mData;
		PCur=PCur->mNext;
		//printf("%d ",temp[i]);
	}
	
	PCur=phead->mNext;
	while(PCur)
	{//not reach the end of list
		if(i>=0)
		{
			PCur->mData=temp[--i];
			//printf("%d ",temp[i]);
			PCur=PCur->mNext;
		}
	}
	return phead;
}




int main()
{
	//initNodeList();

	int i=0;
	int arry[]={5,8,11,12,19,6,4};
	int arry2[]={6,9,1,4,8,2};

	//create
	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);

	//merge
	PList* pmerge=merge2list(pcurrent,pcurrent2);//get the head of list
	displaylist(pmerge);

	//find the pos of value
	int valuetofind=88;
	PList* node=findNode(pcurrent2,valuetofind);
	printf("\n node:%p,node->mData:%d,node->mNext:%p\n",node,node->mData,node->mNext);


	//insert value 
	int pos_val=11;
	int insert_val=666;
	//find pos_val in pcurrent,then insert insert_val 
	insertNode(pcurrent,findNode(pcurrent,pos_val),insert_val);
	displaylist(pcurrent);

	//modify value 
	modifyNode(pcurrent,pos_val,insert_val);//modify pos_val to insert_val 
	displaylist(pcurrent);

	//delete node
	deleteNode(pcurrent,findNode(pcurrent,19));//delete the node 9
	displaylist(pcurrent);

	//sort
	sort(pcurrent);
	printf("\nsort--------------------------------\n");
	displaylist(pcurrent);
	
	
	//reverse
	printf("\nbefore reverse--------------------------------\n");
	displaylist(pcurrent);
	reverse(pcurrent);
	printf("\nafter reverse--------------------------------\n");
	displaylist(pcurrent);
	return 0;
}


 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值