åµåµ.æè¿éå好æåäºå¤©ç»åå¦åçä¸ä¸ª.çä¸ç»æå§.. 代ç æç¹å¤å¦.:
#include<iostream>
#include<stdlib.h>using namespace std;
typedef struct _ListNode // æ¤å¤ä¿®æ¹ä¸ºèªå®ä¹æ°æ®ç±»?
{
int data; // ç»ç¹æ°æ®
_ListNode *link; // æåä¸ä¸ªç»ç¹ _ListNode() // æ¤å¤æ·»å é»è®¤ç"æé å½æ°"
{
data = 0;
link = NULL;
}
}ListNode, LinkList;
/******************************å½æ°å®ä¹æ³¨é头***********************************
* å½æ°å : void CreateLinkList(LinkList *head, int length)
* å¼åæ¶é´ : 2010-3-19 11:00:15
* å½æ°åè½ : å建ä¸ä¸ªé¾è¡¨
* å
¨å±åéåç»æ : æ
* è°ç¨ç主è¦å½æ° : æ
* è¿åå¼è¯´æ : è¿åé¾è¡¨å¤´ç»ç¹æé
* åæ°è¡¨ : head : é¾è¡¨å¤´ç»ç¹
length : é¾è¡¨é¿åº¦
******************************å½æ°ä½å®ä¹**************************************/
LinkList * CreateLinkList(LinkList *head, int length)
{ LinkList *temp = NULL;
int predata = 0, curdata = 0;
temp = head;
for (int i = 1; i <= length; i++)
{
cout << "è¾å
¥ç¬¬" << i <<"个ç»ç¹data:";
head->link = new ListNode();
head = head->link; if (i == 1)
{
cin >> head->data; // è¾å
¥ç¬¬ä¸ä¸ªç»ç¹çdata
predata = head->data; // ä¿å第ä¸ä¸ªç»ç¹çdata
}
else
{
while (1)
{
//cout<<"è¾å
¥ç¬¬"<<i<<"个ç»ç¹çdata:";
cin>>curdata;
if (curdata >= predata) // ç°å¨è¾å
¥çæ°å°äºåä¸ä¸ªæ°
{
head->data = curdata;
predata = curdata; // ä¿®æ¹æ°æ®
break; // ç»æ¢è¾å
¥
}
else // å¦å继ç»è¾å
¥
{
cout<<"error!!current data:"<<curdata<<"å°äºprevious data:"
<<predata<<"ä¸æ»¡è¶³é¾è¡¨æ¡ä»¶,éæ°è¾å
¥!"<<endl;
continue;
} // if (curdata >= predata)
} // while (1)
}// if (i == 1)
}// for (int i = 1; i <= length; i++) head = temp; return(head);
}
/******************************å½æ°å®ä¹æ³¨é头***********************************
* å½æ°å : void Connect(const LinkList *listA,
const LinkList *listB,
LinkList *listC)
* å¼åæ¶é´ : 2010-3-19 11:05:38
* å½æ°åè½ : è¿è¡ä¸¤ä¸ªé¾è¡¨çé¾æ¥
* å
¨å±åéåç»æ : æ
* è°ç¨ç主è¦å½æ° : æ
* è¿åå¼è¯´æ : æ
* åæ°è¡¨ : listA : 第ä¸ä¸ªé¾è¡¨
listB : 第äºä¸ªé¾è¡¨
listC : é¾æ¥åçé¾è¡¨å¤´ç»ç¹
******************************å½æ°ä½å®ä¹**************************************/
void Connect(const LinkList *listA, const LinkList *listB, LinkList *listC)
{
const ListNode *pointer1 = NULL;
const ListNode *pointer2 = NULL;
ListNode *pointer3 = NULL;
pointer1 = listA->link;
pointer2 = listB->link; pointer3 = listC;
while ((pointer1 != NULL) && (pointer2 != NULL))
{
pointer3->link = new ListNode();
pointer3 = pointer3->link;
if (pointer1->data <= pointer2->data)
{
pointer3->data = pointer1->data;
pointer1 = pointer1->link; // ä¿®æ¹æé
}
else
{
pointer3->data = pointer2->data;
pointer2 = pointer2->link; // ä¿®æ¹æé
}
} // å°å©ä½çæ°æ®æ·»å å°é¾æ¥åçé¾è¡¨ä¸
if (pointer1 == NULL)
{
while (pointer2 != NULL)
{
pointer3->link = new ListNode();
pointer3 = pointer3->link; pointer3->data = pointer2->data;
pointer2 = pointer2->link;
}
}
if (pointer2 == NULL)
{
while (pointer1 != NULL)
{
pointer3->link = new ListNode();
pointer3 = pointer3->link; pointer3->data = pointer1->data;
pointer1 = pointer1->link;
}
}
}/******************************å½æ°å®ä¹æ³¨é头***********************************
* å½æ°å : void Display(const LinkList *list)
* å¼åæ¶é´ : 2010-3-19 13:18:15
* å½æ°åè½ : è¾åºä¸ä¸ªé¾è¡¨ä¸çæ°æ®
* å
¨å±åéåç»æ : æ
* è°ç¨ç主è¦å½æ° : æ
* è¿åå¼è¯´æ : æ
* åæ°è¡¨ : list : é¾è¡¨å¤´ç»ç¹
******************************å½æ°ä½å®ä¹**************************************/
void Display(const LinkList *list)
{
const ListNode *p = NULL;
p = list->link; cout<<endl<<"è¿ä¸ªé¾è¡¨ä¸çæ°æ®ä¸º:"<<endl;
while (p != NULL)
{
cout<<p->data<<" ";
p = p->link;
} cout<<endl;
}
/******************************å½æ°å®ä¹æ³¨é头***********************************
* å½æ°å : void Destroy(LinkList *list)
* å¼åæ¶é´ : 2010-3-19 13:20:23
* å½æ°åè½ : éæ¯ä¸ä¸ªé¾è¡¨
* å
¨å±åéåç»æ : æ
* è°ç¨ç主è¦å½æ° : æ
* è¿åå¼è¯´æ : æ
* åæ°è¡¨ : é¾è¡¨å¤´ç»ç¹
******************************å½æ°ä½å®ä¹**************************************/
void Destroy(LinkList *list)
{
ListNode *p = NULL;
ListNode *temp = NULL;
p = temp = list;
while(p != NULL)
{
temp = p->link;
delete p;
p = temp;
} list = NULL;
}/******************************å½æ°å®ä¹æ³¨é头***********************************
* å½æ°å : void main()
* å¼åæ¶é´ : 2010-3-19 13:02:47
* å½æ°åè½ :
* å
¨å±åéåç»æ :
* è°ç¨ç主è¦å½æ° :
* è¿åå¼è¯´æ :
* åæ°è¡¨ :
******************************å½æ°ä½å®ä¹**************************************/
void main()
{
ListNode *h_first = NULL;
ListNode *h_second = NULL; // åå«ä¸ºç¬¬ä¸ä¸ªå第äºä¸ªé¾è¡¨ç头ç»ç¹
ListNode *h_result = NULL; // é¾æ¥åçé¾è¡¨å¤´ç»ç¹
int length = 0; // é¾è¡¨é¿åº¦
h_first = new ListNode();
h_second = new ListNode(); // åé
ç©ºé´ cout << "请è¾å
¥ç¬¬ä¸ä¸ªé¾è¡¨çé¿åº¦ï¼";
cin >> length;
h_first = CreateLinkList(h_first, length);
cout << "请è¾å
¥ç¬¬äºä¸ªé¾è¡¨çé¿åº¦ï¼";
cin >> length;
h_second = CreateLinkList(h_second, length); Display(h_first);
Display(h_second); h_result = new ListNode();
Connect(h_first, h_second, h_result);
Display(h_result);
Destroy(h_first);
Destroy(h_second);
Destroy(h_result);
追é®è¦æ±ç¨cè¯è¨å®ç°ï¼ä¸æ¯c++ï¼ä¸è¿è¿æ¯è°¢è°¢äº