資料結構與演算法 鍊錶

2021-07-10 12:11:31 字數 3477 閱讀 6528

**

題目:合併兩個已經排序好的鍊錶(非遞迴和遞迴兩種)

方法1:

[cpp]view plain

copy

print

?"color:#000000;">// 合併鍊錶.cpp : 定義控制台應用程式的入口點。

//#include "stdafx.h"

#include 

using

namespace std;  

struct listnode  

};  

listnode* mergelist2(listnode* head1,listnode* head2)  

else

if(head2 == null)  

listnode* mergehead = null;  

if (head1->m_data m_data)  

else

listnode* tmpnode = mergehead;  

while (head1&&head2)  

else

mergehead = mergehead->m_pnext;  

}  if (head1)  

if (head2)  

return tmpnode;  

}  int _tmain(int argc, _tchar* argv)  

listnode* phead2 = new listnode(2);  

pcur = phead2;  

for (int j = 4; j 

listnode* head = mergelist2(phead1,phead2);  

while (head)  

getchar();  

return 0;  }

方法2:/*

我們分析兩個鍊錶的過程,首先從合併兩個鍊錶的頭結點開始,鍊錶1的頭節點的值小於鍊錶2的頭結點的值,因此鍊錶1的頭結點

就是合併鍊錶的頭節點,繼續合併剩下的鍊錶,在兩個鍊錶中剩餘的節點仍然是排序的,因此合併兩個鍊錶的步驟是一樣的,我們還是比較兩個頭結點的

值,此時鍊錶2的頭結點的值小於鍊錶1的頭結點的值,因此鍊錶2的頭結點是合併剩餘鍊錶的頭結點,我們把這個節點和前面合併鍊錶時得到的鍊錶的尾巴節點

鏈結起來

按照上面的分析可知:每次合併的步驟都是一樣的,由此我們想到了遞迴。 */

[cpp]view plain

copy

print

?// 合併鍊錶.cpp : 定義控制台應用程式的入口點。

//#include "stdafx.h"

#include 

using

namespace std;  

struct listnode  

};  

listnode* mergelist(listnode* phead1,listnode* phead2)  

else

if (phead2 == null)  

listnode* pmergehead = null;  

if (phead1->m_data m_data)  

else

return pmergehead;  

}  int _tmain(int argc, _tchar* argv)  

listnode* phead2 = new listnode(2);  

pcur = phead2;  

for (int j = 4; j 

listnode* head = mergelist2(phead1,phead2);  

while (head)  

getchar();  

return 0;  

單向鍊錶反轉

**這次介紹經常在面試中被問到的單向鍊錶的反轉問題,問題的解決方法有多種

最普通的是從頭到尾掃瞄鍊錶,然後對鍊錶進行反轉。

使用單個引數的遞迴方法;使用單個引數是相當於不斷的往鍊錶後部深入,並且在每次深入的遞迴中儲存了下乙個節點和當前節點的資訊,再呼叫遞迴後處理當前節點和下乙個節點的關係;其中比較有特點的處理過程是返回值的處理,每次遞迴後返回的指標為同乙個;

使用兩個引數的遞迴方法,外加乙個當前煉表頭節點的前乙個結點的指標,在進一步遞迴之前處理好頭節點和其前乙個節點的關係。

1 #include 2 #include 

3using

namespace

std;

4struct

node;89

//使用非遞迴的鍊錶反轉方法 //賦初值,留後手,做反轉,向前走,返新頭。

10 node* reverselist1(node *ptr)

1121

return

pre;22}

23//

乙個引數的遞迴反轉方法

24//

將當前節點的下乙個節點儲存好,並將當前節點從鍊錶中取出,再遞迴處理以後的鍊錶

25//

同時reverserest沒有改變,一直儲存了鍊錶的最後乙個節點的指標

26 node* reverselist2(node *ptr)

3738

//遞迴實現2:兩個引數的遞迴呼叫方法

39//

傳遞乙個當前煉表頭節點的前乙個節點的指標,在函式中將當前頭節點的next改為其前乙個節點,並遞迴呼叫

40 node* reverselist3(node *ptr, node *pre)

45else50}

5152

53void printlist(node *ptr)

59 cout <

6162

intmain()

71 cout << "

the lsit:

"<

72printlist(head);

7374 head =reverselist1(head);

75 cout << "

after reverselist1():

"<

76printlist(head);

7778

79 head =reverselist2(head);

80 cout << "

after reverselist2():

"<

81printlist(head);

8283 node * tmp =null;

84 head =reverselist3(head, tmp);

85 cout << "

after reverselist3():

"<

86printlist(head);

8788

return1;

8990 }

資料結構與演算法 鍊錶

在講述鍊錶之前讓我們對資料結構進行乙個簡單的回顧 我們知道,資料結構指的是描述實際問題中各個資料項節點之間的前後邏輯結構關係,即要麼是線性結構 即某一資料項的前繼節點和後繼節點有且只有乙個 要麼是非線性結構 即某一資料節點的前驅或者後繼節點不止乙個 在確定了實際資料項的資料結構之後,我們要採用某種儲...

資料結構與演算法 鍊錶

反轉鍊錶 def reverse head q none p heap while p temp p.next p.next q q pp temp return p判斷鍊錶環 def meetingnode head if not head return slow head fast head.n...

資料結構與演算法 鍊錶

鍊錶 優點 插入刪除快 缺點 不支援隨機訪問 messagequeue 插入 enqueuemessage 按照時間順序插入 刪除 next mahjong.class author csy created by csy on 2019 1 28.public class mahjong overr...