資料結構 複雜鍊錶的複製

2021-07-23 04:47:36 字數 1079 閱讀 4963

實現複雜鍊錶的複製。

因為複雜鍊錶中每個節點都有乙個指向任意節點的指標。所以在確定這個鍊錶的複製的時候。我們需要進行空間來換取時間上的效率。然後我們可以將鍊錶複製項結合在拆分。

思路就這樣。

我直接給出**:

#pragma once

#include #include #include typedef int datatype;

typedef struct complexnode

complexnode;

void creatcomplexnode(complexnode* &phead,datatype x);

complexnode* copycomplexnode(complexnode* phead);

//建立複雜鍊錶。

void creatcomplexnode(complexnode* &phead,datatype x)

endnode = phead;

ret = endnode;

while(endnode->_next)

endnode->_next = (complexnode*)malloc(sizeof(complexnode));

endnode = endnode->_next;

endnode->_next = null;

endnode->_data = x;

endnode->_random = ret;

}//解決複雜鍊錶的複製。可以把其中的操作分為3個步驟:

//合併。

//複製隨機指標值。

//拆分。

complexnode *copycomplexnode(complexnode *phead)

pnode = phead;

while(pnode != null)

pnode = copynode->_next;

} pnode = phead;

newlink = pnode->_next;

while(pnode != null)

return newlink;

}

鍊錶 複雜鍊錶的複製

問題描述 請實現函式complexlistnode clone complexlistnode phead 複製乙個複雜鍊錶。在複雜鍊錶中,每個結點除了有乙個next指標指向下乙個結點之外,還有乙個random指向鍊錶中的任意結點或者null。結點的定義如下 struct randomlistnod...

複雜鍊錶複製

複雜鍊錶複製的標頭檔案mlist.h ifndef mlist h define mlist h include include includetypedef int datatype typedef struct node node,pnode,plist pnode crealist datat...

複製複雜鍊錶

題目 輸入乙個複雜鍊錶 每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點 返回結果為複製後複雜鍊錶的head。注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空 解題思路 首先有三種解法 第一種就是中規中矩的解法,首先複製next指標的節點,之後...