【数据结构·C语言】请高手帮忙检查一个关于【链表归并】的算法是否正确 【数据结构·C语言】请高手帮忙检查一个关于【实现链表的就地逆...
\u3010\u6570\u636e\u7ed3\u6784\u00b7C\u8bed\u8a00\u3011\u8bf7\u9ad8\u624b\u5e2e\u5fd9\u68c0\u67e5\u4e00\u4e2a\u5173\u4e8e\u3010\u94fe\u8868\u7684\u5f52\u5e76\u3011\u7b97\u6cd5\u662f\u5426\u6b63\u786efor(p=&(A.elem[A.length-1]);p>=&(A.elem[i]);p--) *(p+1)=p; \u6700\u540e\u7684p\u524d\u9762\u5c11\u4e00\u4e2a\u661f\u53f7\uff0c\u5e94\u8be5\u6539\u4e3afor(p=&(A.elem[A.length-1]);p>=&(A.elem[i]);p--) *(p+1)=*p;
A.elem[i]=B.elem[k];\u4e4b\u540e\uff0c\u7531\u4e8eB\u7684\u5143\u7d20\u63d2\u5728\u4e86A\u7684i\u4f4d\u7f6e\uff0c\u6240\u4ee5\uff0c\u8fd9\u4e4b\u540ei\u5e94\u8be5\u81ea\u52a01
for (p=&(A.elem[i+1]);p<=&(A.elem[A.length-1]);p++) *(p-1)=*p;\u8fd9\u91cc\u662f\u8981\u8986\u76d6i+1\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u800c\u6839\u636e\u4f60\u81ea\u5df1\u5199\u7684\u521d\u59cb\u6761\u4ef6\uff0cp\u4e00\u5f00\u59cb\u5c31\u5df2\u7ecf\u6307\u5411i+1\u4f4d\u7f6e\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u6539\uff0c\u540c\u65f6\u4f34\u968f\u4fee\u6539\u7ed3\u675f\u6761\u4ef6\uff0c\u6700\u7ec8\u4fee\u6539\u4e3afor (p=&(A.elem[i+1]);p<&(A.elem[A.length-1]);p++) *p=*(p+1);
\u5176\u4ed6\u7684\u90fd\u8fd8\u597d\u3002
PS\uff0c\u5982\u679c\u60f3\u7a0b\u5e8f\u8fd0\u884c\u7b80\u5355\u4e00\u70b9\uff0c\u5efa\u8bae\u4f60\u6bcf\u5f52\u5e76\u4e00\u4e2aB\u7684\u5143\u7d20\u5230A\u5c31\u67e5\u627e\u4e00\u904dA\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u662f\u7a0b\u5e8f\u5c31\u4f1a\u9ebb\u70e6\u4e00\u70b9\uff0c\u8fd8\u6709\uff0c\u5bf9A\u7684\u91cd\u590d\u5143\u7d20\u7684\u81ea\u68c0\uff0c\u53ef\u4ee5\u8003\u8651\u628aif\u5199\u4e3awhile\uff0c\u56e0\u4e3a\u8003\u8651\u5230\u53c8\u53ef\u4ee5\u80fd\u8fde\u7740\u4e09\u4e2a\u4ee5\u4e0a\u7684\u5143\u7d20\u76f8\u7b49\u3002
\u5e0c\u671b\u91c7\u7eb3\uff0c\u671f\u5f85\u5bf9\u4f60\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u8ffd\u95ee^_^
\u6211\u5df2\u7ecf\u628a\u4f60\u7ed9\u7684\u4ee3\u7801\u5728\u4e0b\u5217\u4ee3\u7801\u4e2d\u505a\u4e86\u5177\u4f53\u5b9e\u73b0\uff0c\u5177\u4f53\u95ee\u9898\u5df2\u5728\u4e0b\u5217\u4ee3\u7801\u4e2d\u8bf4\u660e
#include
using namespace std;
#define OK true
#define FALSE false
typedef bool status;
typedef int INT32;
typedef struct tagLinkNode
{
INT32 key;
struct tagLinkNode *next;
tagLinkNode()//construct
{
key = -1;
next = NULL;
}
}LinkNode, *LinkList;
status createLinkList( LinkList &L)
{
INT32 nAccount = 0 ;//number of node in LinkList L
cout<<"input the num of node,please"<<endl;
cin>>nAccount;
//
//the first node is empty
//
L = ( LinkList ) malloc( sizeof( LinkNode) );
L->next = NULL;
LinkList pNow = L;
//create list
cout<<"import data:"<<endl;
for( INT32 i = 0; i<nAccount; i++ )
{
LinkList pTemp = ( LinkList ) malloc( nAccount * sizeof( LinkNode ) );
if( pTemp == NULL ) return FALSE;
pTemp->next = NULL;
INT32 nKey;
cin>>nKey;
pTemp->key = nKey;
pNow->next = pTemp;
pNow = pTemp;
}
return OK;
}
void clearLinkList( LinkList &L )
{
LinkList pNow = L;
while( pNow != NULL )
{
LinkList pTemp = pNow->next;
free( pNow );
pNow = pTemp;
}
L = NULL;
return ;
}
status inverseLinkList( LinkList &L )//\u8fd9\u91cc\u7684status\u8bf4\u660e\u4f60\u7684\u4ee3\u7801\u5e94\u8be5\u662f\u4eff\u7167\u4e66\u6765\u5199\u7684\uff0c\u4e66\u4e0a\u7684\u4ee3\u7801\u4e00\u822c\u662f\u4f2a\u7801
{
LinkList pNow = L,pTemp = L;// \u4f60\u6ca1\u6709\u8d4b\u503c
while( pNow != NULL )//traverse in first loop
{
//
//\u4e0b\u9762\u8fd9\u53e5\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u4f60\u6ca1\u6709\u5224\u65adpTemp->next\u7684\u6709\u6548\u6027
//
/*for( ; pTemp->next->next != NULL; pTemp = pTemp->next );*/
if( pTemp->next == NULL ) break;
if( pTemp->next->next == NULL )// \u907f\u514d\u7b2c\u4e00\u4e2a\u8282\u70b9\u4e22\u5931
{
break;
}
for( ; pTemp->next->next != NULL; pTemp = pTemp->next );//traverse in second loop \u5f88\u663e\u7136\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a0(n^2)
//
//\u4e0b\u9762\u4e09\u884c\u4ee3\u7801\u6709\u95ee\u9898\u7684\uff0c\u4f1a\u4e22\u5931\u539f\u94fe\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9
//
pTemp->next->next = pNow->next;
pNow->next = pTemp->next;
pTemp->next = NULL;
pNow = pNow->next;
pTemp = pNow;
}
return OK;
}
void displayLinkList( LinkList L )
{
LinkList pNow = L->next;
cout<<"print LinkList:"<<endl;
while( pNow != NULL )
{
coutkey <<" ";
pNow = pNow->next;
}
cout<<endl;
return ;
}
int main()
{
LinkList L = NULL;
createLinkList( L );// create LinkList
cout<<"before the operation of inversing"<<endl;
displayLinkList( L );
inverseLinkList( L );
cout<<"after the operation of inversing"<<endl;
displayLinkList( L );
clearLinkList( L );
system("pause");
return 0;
}
2. 您的算法因为AB是递增有序要改为递减有序,您就每次将指针移动到序列的最末端来进行比较和插入,由于是单向链表,这样你的算法会非常低效。
3. 您仅仅需要将两个链表的结点按照递减顺序插入到新的链表中即可。
4. 例如您可以先将A中的结点逆序,然后将B中的结点按照递减顺序一个一个插入到A中的合适位置,最终获得的A链表即为需要的C链表了。
绛旓細鏁版嵁缁撴瀯:涓昏鎺屾彙鍚勭缁撴瀯鍜岀浉搴旂殑绠楁硶銆璇█鐨勫熀鏈娉曟渶濂藉崟鐙姳浜涙椂闂村瀛︺倀ypedef鏄粰绫诲瀷鍙栧埆鍚嶏細typedef int Status; //鐢⊿tatus浠f浛int 绫诲瀷 浠ヤ笂绋嬪簭涓嶅畬鏁存墍浠ヤ笉鍙兘缂栬瘧鎴愬姛锛氬ElemType绫诲瀷娌℃湁瀹氫箟
绛旓細r鎸囧悜閾捐〃鐨勫熬缁撶偣锛宲p娌℃湁鐢ㄥ埌 */ r=head;r->next=NULL; /* 褰撳墠閾捐〃鍙湁涓涓ご缁撶偣锛宺鎸囧悜澶寸粨鐐 */ s=(ListNode *)malloc(sizeof(ListNode)); /*鎸囬拡s鎸囧悜鏂板垎閰嶇殑绌洪棿*/ strcpy(s->data,ch); /*灏鏁版嵁ch澶嶅埗鍒皊鎵鎸囩粨鐐圭殑鏁版嵁鍩*/ r->next=s;r=s;r->next=NULL ...
绛旓細LinkList鏄崟鍚戦摼琛ㄧ殑澶撮儴銆侺istNode鏄摼琛ㄥ唴鐨勫厓绱犮俽鏄寚鍚戦摼琛ㄧ殑鏈鍚庝竴涓厓绱犮侺inkList head=...锛岀敤malloc()鍑芥暟浠庡爢鍐呭瓨涓垎閰嶄竴灏忓潡鍐呭瓨锛屽苟灏嗗叾鍦板潃鏀惧湪head涓俽->next=NULL;锛屽垵濮嬪寲閾捐〃鐨勫熬閮ㄣ俿=...锛岃幏鍙栦竴涓摼琛ㄥ厓绱犵殑鍐呭瓨绌洪棿銆俿trcpy(...);锛屽s鐨勫唴瀹癸紝璧嬪笺俽->next=s;锛屽皢...
绛旓細浠ヤ笅鎵鏈夌▼搴忓潎閫氳繃缂栬瘧涓庤皟璇曪紝搴旇娌¢棶棰 绗竴棰樻槸缁忓吀鐨勫箍搴︿紭鍏堟悳绱㈤棶棰橈紝濡傛灉鎯宠鏁堢巼鏇撮珮涓鐐瑰彲浠ラ噰鐢ㄥ弻鍚戝箍搴︿紭鍏堟悳绱紝涓嶈繃鎬诲叡鎵9!=362880绉嶇姸鎬侊紝娌″お澶у繀瑕侊紝鍋锋噿涓嶅啓浜 include <iostream> define MAXN 362880 const int ftl[]={40320,5040,720,120,24,6,2,1,1};//闃朵箻 const int ...
绛旓細鍚屽浣犲ソ锛屾垜甯綘鐪嬩簡浣犵殑绋嬪簭锛岄噷闈㈡湁涓棶棰橈細浣犲厛闈㈢殑杈撳嚭鐨勫嚱鏁版槸printf() 杩欐槸C鐨勮娉曡鍒欙紝鎵浠ュご鏂囦欢涓簲璇ョ敤锛# include <stdio.h> 淇敼鍦ㄦ敞閲婁腑锛氬笇鏈涜兘閲囩撼 瀹為獙缁撴灉锛//#include <iostream>//灏嗚繖琛屾敞閲婃帀 include <stdio.h>//澧炲姞杩欒浠g爜 锛岃繖鏄爣鍑咰鍐欐硶銆俤efine MaxSize 100 /...
绛旓細鎸夎繖涓▼搴忕殑鎰忔 1...s1[0]搴旇鏄粠1鍒皊1鐨勯暱搴︼紝鎵浠[1..s1[0]]灏变唬琛ㄤ竴涓瓧绗︿覆锛宼[1..s1[0]]=s1[1..s1[0]]; 鏄洿鎺ョ粰瀛楃涓茶祴鍊笺備絾鏄湪C绋嬪簭閲岄潰涓嶈兘鐢ㄨ繖绉嶇畝鐣ュ啓娉曘備綘鐪嬬殑涔﹀ソ璞″お涓嶈鑼冧簡銆傘傘1...s1[0]搴旇鐢╢or(i=1;i<=s1[0];i++)琛ㄧず 鑰屼笖浼间箮寰堝皯鏈夆滀笅鏍...
绛旓細鎻掑叆鎺掑簭锛氳捣濮嬶紙64锛夛紝锛56,23,89,10,75锛夌涓瓒 锛56,64锛夛紝锛23,89,10,75锛夌浜岃稛 锛23,56,64锛夛紝锛89,10,75锛夌涓夎稛 锛23,56,64,89锛夛紝锛10,75锛夌鍥涜稛 锛10,23,56,64,89锛夛紝锛75锛夌浜旇稛 锛10,23,56,64,75,89锛2. 鍐掓场鎺掑簭 璧峰锛50锛38锛77锛26锛45锛69锛夌涓...
绛旓細1.鐢C璇█瀹炵幇绯荤粺;2.鍒╃敤鏁扮粍鎴栫粨鏋勪綋瀹炵幇涓婅堪棰樼洰鐨鏁版嵁缁撴瀯璁捐;3... 鍋氫竴涓鐢熶俊鎭鐞嗙郴缁熷缓绔嬪鐢熶俊鎭暟鎹,鍖呮嫭瀛﹀彿銆佸鍚嶃佹у埆銆佷笁绉戞垚缁┿佸嚭鐢熸棩鏈熴佸勾榫(蹇呴』璁$畻寰楀埌)銆1. 鐢–璇█瀹炵幇绯荤粺;2. 鍒╃敤鏁扮粍鎴栫粨鏋勪綋瀹炵幇涓婅堪棰樼洰鐨勬暟鎹粨鏋勮璁;3. 绯荤粺鍏锋湁鏁版嵁鐨勫鍔犮佷慨鏀广佸垹闄ゃ佹帓搴忋佹煡璇佺粺璁″拰...
绛旓細鐢变簬鏍圭殑鍓嶅簭閬嶅巻鎵浠ユ帹鍑篈鏄牴 銆傦紙ps 濡傛灉鏄夋嫨棰樼殑璇 杩欎竴姝ュ鏋滄渶鍚庝竴涓瓧姣嶄笉鏄疉灏卞彲浠ユ帓闄や簡锛 涓嬮潰鎴戜滑鏉ュ仛闈為夋嫨棰樻濊冩柟娉曪紝 鍥犱负涓簭閬嶅巻 鎵浠ユ帹鍑篋BE 鍦ㄦ牴鐨勫乏杈 FC鍦ㄦ牴鐨勫彸杈 銆備笅闈 鎴戜滑鏉ユ帹瀵糉C鐨勬帓鍒楅『搴忥紝鐢变腑搴廎C 鍓嶅簭CF 鎵浠鏄疌 宸﹀瓙鏍 涓嬮潰鏉ユ帹瀵糄BE 宸﹁竟涓簭 DBE ...
绛旓細i++;} (2) k=0;for (i=1;i<=n; i++) { for (j=i; j<=n; j++) i = 1 2 3 ... n k++; j = n n-1 n-2 ...1 棰戝害涓 1+ 2+3+...+n = (n+1)n/2 } 1.3璇疯鏄庝笅鍒楃畻娉曠殑鏃堕棿澶嶆潅搴︺(1) void sf1 (int n){ i...