=========================================================*/
/*ºÏ²¢ÓÐÐòµ¥Á´±í*/
Node* MergeList(Node* h1,Node* h2)
{
if(NULL== h1|| NULL== h2)
return h1;
if(NULL== h1->next )
return h2;
if(NULL== h2->next)
return h1;
Node* curr1,*curr2,*prev1,*temp;
prev1=h1;/*Á´±í1µÄÇ°Çý½Úµã*/
curr1=h1->next;/*Á´±í1µÄµ±Ç°½Úµã*/
curr2=h2->next;/*Á´±í2µÄµ±Ç°½Úµã*/
temp=h2;
while(curr2)
{
while(curr1&& curr1->elem< curr2->elem)/*Á´±í1Ö¸ÕëÒƶ¯ÖÁ´ó»òµÈÓÚÁ´±í2µ±Ç°ÔªËصÄλÖÃ*/
prev1=curr1,curr1=curr1->next;
/*ÔÚÁ´±í1ÖвåÈëÁ´±í2µÄµ±Ç°ÔªËØ*/
temp=curr2->next;/*ÔÝ´æÁ´±í2µÄÏÂÒ»¸ö½Úµã*/
prev1->next=curr2;
curr2->next=curr1;
/*Á´±í1Òƶ¯ÖÁнڵã*/
curr1=curr2;
/*Á´±í2Òƶ¯ÖÁÏÂÒ»¸ö½Úµã*/
curr2=temp;
}
return h1;
}
/*=============================================================================*/
/*´´½¨Ë«Á´±í*/
DNode* DoubleList(DNode*head)
{
if(NULL== head)//·ÖÅäÍ·½Úµã¿Õ¼ä
head=(DNode*)malloc(sizeof(DNode)) , head->prev=NULL , head->next=NULL;
DNode*current=head ,*temp;
char ch;
while(1)
{
cout<<¡±\n input elem:¡±;
cin>>ch;
if(¡®#¡¯ == ch)/*#½áÊøÊäÈë*/
break;
temp=(DNode*) malloc (sizeof(DNode) );
temp->elem=ch;
temp->next=NULL;
current->next=temp;/*µ±Ç°½ÚµãµÄºóÇýÖ¸Ïòнڵã*/
temp->prev=current;/*нڵãµÄÇ°ÇýÖ¸Ïòµ±Ç°½Úµã*/
current=temp;/*µ±Ç°½ÚµãΪÁ´±íβ½Úµã*/
}
return head;
}
/*=============================================================================*/
/*Êä³öË«Á´±í*/
void PrintDoubleList(DNode*head)
{
if(NULL== head)
return;
DNode* p;
p=head;
cout<<¡±\n DoubleList are:¡±;
while(p->next)
{
p=p->next;
if(p->elem)
cout<elem;
}
cout<<¡±\n DoubleList are:¡±;
while(p->prev)
{
if(p->elem)
cout<elem;
p=p->prev;
}
}
/*=============================================================================*/
/*´´½¨Ñ»·Á´±í*/
Node* CycleList(Node*head)
{
if(NULL== head)/*·ÖÅäÍ·½Úµã¿Õ¼ä*/
head=(Node*)malloc(sizeof(Node)),head->next=NULL;
Node*current=head ,*temp;
char ch;
while(1)
{
cout<<¡±\n input elem:¡±;
cin>>ch;
if(¡®#¡¯ == ch)/*#½áÊøÊäÈë*/
break;
temp=(Node*) malloc (sizeof(Node) );
temp->elem=ch;
temp->next=NULL;
current->next=temp;/*µ±Ç°½ÚµãµÄºóÇýÖ¸Ïòнڵã*/
current=temp;/*µ±Ç°½ÚµãΪÁ´±íβ½Úµã*/
}
current->next=head;/*β½ÚµãÖ¸ÏòÍ·½Úµã*/
return head;
}
/*=============================================================================*/
/*ÅжÏÁ´±íÊÇ·ñÓл·(Ñ»·Á´±í)*/
bool IsCycleList(Node*head)
{
if(NULL== head)
return false;
if(NULL== head->next)
return false;
Node*current=head->next;
while(current)
{
if(head== current->next)
return true;
current=current->next;
}
return false;
}
int main()
{
Node* head,*p;
Node* head2,*head3;
DNode* dHead;
char ch;
head= NULL;
head2=NULL;
head3=NULL;
dHead=NULL;
//head=(Node*) malloc ( sizeof( Node) );
//head->next = NULL;
//´´½¨µ¥Á´±í
head=CreateList(head);
PrintList(head);
head2=CreateList(head2);
PrintList(head2);
//²åÈë½Úµã
cout<<¡±\n input elem to insert:¡±;
cin>>ch;
InsertNode(head,ch);
PrintList(head);
//ɾ³ý½Úµã
cout<<¡±\n input elem to delete:¡±;
cin>>ch;
DeleteNode(head,ch);
PrintList(head);
//µ¥Á´±íÄæÖÃ
head=ReverseList(head);
cout<<¡±\n Reversed !¡±;
PrintList(head);
//ÇóÖмä½Úµã
p=MiddleNode(head);
cout<<¡±\n Middle Node is:¡±;
cout<elem<
//ºÏ²¢ÓÐÐòµ¥Á´±í
MergeList(head,head2);
cout<<¡±\n Merged!¡±;
PrintList(head);
//´´½¨Ë«Á´±í
dHead=DoubleList(dHead);
PrintDoubleList(dHead);
/*´´½¨Ñ»·Á´±í²¢ÅжÏÊÇ·ñÓл·*/
head3=CycleList(head3);
cout< return 0;
}