查询后,在位置前后后插入,
#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;
}
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;
}
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);
//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;
insertNode(pcurrent,findNode(pcurrent,pos_val),insert_val);
//find pos_val in pcurrent,then insert insert_val
printf("\ninsertnode--------------------------------\n");
displaylist(pcurrent);
return 0;
}
在值为11的位置前插入一个新的单元值为666,
删除节点:
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;
}
删除19的节点,