菜,
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;
}