百戰c++(7)

40. 鏈表題:一個鏈表的結點結構

struct Node

{

int data ;

Node *next ;

};

typedef struct Node Node ;

(1)已知鏈表的頭結點head,寫一個函數把這個鏈表逆序 ( Intel)

Node * ReverseList(Node *head) //鏈表逆序

{

if ( head == NULL || head->next == NULL )

return head;

Node *p1 = head ;

Node *p2 = p1->next ;

Node *p3 = p2->next ;

p1->next = NULL ;

while ( p3 != NULL )

{

p2->next = p1 ;

p1 = p2 ;

p2 = p3 ;

p3 = p3->next ;

}

p2->next = p1 ;

head = p2 ;

return head ;

}

(2)已知兩個鏈表head1 和head2 各自有序,請把它們合并成一個鏈表依然有序。(保留所有結點,即便大小相同)

Node * Merge(Node *head1 , Node *head2)

{

if ( head1 == NULL)

return head2 ;

if ( head2 == NULL)

return head1 ;

Node *head = NULL ;

Node *p1 = NULL;

Node *p2 = NULL;

if ( head1->data < head2->data )

{

head = head1 ;

p1 = head1->next;

p2 = head2 ;

}

else

{

head = head2 ;

p2 = head2->next ;

p1 = head1 ;

}

Node *pcurrent = head ;

while ( p1 != NULL && p2 != NULL)

{

if ( p1->data <= p2->data )

{

pcurrent->next = p1 ;

pcurrent = p1 ;

p1 = p1->next ;

}

else

{

pcurrent->next = p2 ;

pcurrent = p2 ;

p2 = p2->next ;

}

}

if ( p1 != NULL )

pcurrent->next = p1 ;

if ( p2 != NULL )

pcurrent->next = p2 ;

return head ;

}

(3)已知兩個鏈表head1 和head2 各自有序,請把它們合并成一個鏈表依然有序,這次要求用遞歸方法進行。 (Autodesk)

答案:

Node * MergeRecursive(Node *head1 , Node *head2)

{

if ( head1 == NULL )

return head2 ;

if ( head2 == NULL)

return head1 ;

Node *head = NULL ;

if ( head1->data < head2->data )

{

head = head1 ;

head->next = MergeRecursive(head1->next,head2);

}

else

{

head = head2 ;

head->next = MergeRecursive(head1,head2->next);

}

return head ;

}

41. 分析一下這段程序的輸出 (Autodesk)

class B

{

public:

B()

{

cout<<"default constructor"<<endl;

}

~B()

{

cout<<"destructed"<<endl;

}

B(int i):data(i) //B(int) works as a converter ( int -> instance of B)

{

cout<<"constructed by parameter " << data <<endl;

}

private:

int data;

};

B Play( B b)

{

return b ;

}

(1) results:

int main(int argc, char* argv[]) constructed by parameter 5

{ destructed B(5)形參析構

B t1 = Play(5); B t2 = Play(t1);   destructed t1形參析構

return 0;               destructed t2 注意順序!

} destructed t1

(2) results:

int main(int argc, char* argv[]) constructed by parameter 5

{ destructed B(5)形參析構

B t1 = Play(5); B t2 = Play(10);   constructed by parameter 10

return 0;               destructed B(10)形參析構

} destructed t2 注意順序!

destructed t1

42. 寫一個函數找出一個整數數組中,第二大的數 (microsoft)

答案:

const int MINNUMBER = -32767 ;

int find_sec_max( int data[] , int count)

{

int maxnumber = data[0] ;

int sec_max = MINNUMBER ;

for ( int i = 1 ; i < count ; i++)

{

if ( data[i] > maxnumber )

{

sec_max = maxnumber ;

maxnumber = data[i] ;

}

else

{

if ( data[i] > sec_max )

sec_max = data[i] ;

}

}

return sec_max ;

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/443878.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/443878.shtml
英文地址,請注明出處:http://en.pswp.cn/news/443878.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數組的轉置和軸對稱

文章目錄T屬性transpose()方法swapaxes()方法T屬性 import numpy as np # Numpy工具包data np.arange(12).reshape(3, 4) # 創建一個3行4列的數組 print(data)# 數組的轉置和軸對稱 data1 data.T print(data1)print(data) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] print(dat…

百戰c++(8)

43. 寫一個在一個字符串(n)中尋找一個子串(m)第一個位置的函數。 KMP算法效率最好&#xff0c;時間復雜度是&#xff2f;(nm)。 44. 多重繼承的內存分配問題&#xff1a; 比如有class A : public class B, public class C {} 那么A的內存結構大致是怎么樣的&#xff1f; 這…

管道實現父子進程的信息傳遞(一)【fork函數、pipe函數、write/read操作、wait函數】

文章目錄題目描述代碼實現關于pipe函數關于讀寫操作關于讀寫端口關于wait函數功能&#xff1a;注意&#xff1a;關于fork函數題目描述 編寫一個程序&#xff0c;利用管道實現父子進程的通信&#xff0c;父進程向子進程發送信息&#xff0c;由子進程輸出顯示。 代碼實現 #inclu…

基礎的shell編程問題(一)

文章目錄題目一題目描述代碼實現關于$#的有關內容實測本程序的作用題目二題目描述代碼實現注釋關于argc、argv關于read函數關于文件描述符關于write函數本程序的作用題目三題目描述代碼實現實測關于grep命令關于read命令題目四題目描述代碼實現關于test命令實測題目一 題目描述…

百戰c++(9)

12 . 下面的代碼輸出是什么&#xff0c;為什么&#xff1f; void foo(void) { unsigned int a 6; int b -20; (ab > 6) puts("> 6") : puts("< 6"); } 這個問題測試你是否懂得C語言中的整數自動轉換原則&#xff0c;我發現有些開發者懂得…

基礎的shell編程問題(二)

文章目錄題目一題目描述代碼實現結果驗證關于本題題目二題目描述代碼實現結果測試題目三題目描述代碼實現及結果測試題目四題目描述代碼實現及結果測試題目五題目描述代碼實現及結果測試題目一 題目描述 輸入的命令行參數必須是hello&#xff0c;才會正確顯示&#xff1b;否則…

百戰c++(10)

1.多態類中的虛函數表是Compile-Time&#xff0c;還是Run-Time時建立的? 2.將一個 1M -10M 的文件&#xff0c;逆序存儲到另一個文件&#xff0c;就是前一個文件的最后一個 字符存到新文件的第一個字符&#xff0c;以此類推。 3.main主函數執行完畢后&#xff0c;是否可能會…

Numpy實現酒鬼漫步問題【以及randint()、where()、cumsum()、argmax()的用法詳解】

文章目錄題目描述代碼實現關于本題涉及到的幾個函數randint()where()cumsum()題目拓展題目描述代碼實現題目拓展題目描述代碼實現argmax()題目描述 從前有一個酒鬼&#xff0c;喝醉了行走在一條直線上&#xff0c;每走一步方向是不確定的&#xff08;向前或者向后&#xff09;…

百戰c++(11)

31.找錯 Void test1() { char string[10]; char* str1"0123456789"; strcpy(string, str1); } Void test2() { char string[10], str1[10]; for(I0; I<10;I) { str1[i] a; } strcpy(string, str1); } Void test3(char* str1) { char string[10]; if(st…

搞清axis的含義,這一篇就夠了!

文章目錄axis的含義旁門左道式理解二維數組中的axis三維數組中的axis正規理解axis的含義 在自己分析之前先擺上官方關于多維數組中axis的值的定義&#xff1a; axis 0&#xff0c;表示第一個維度 axis 1&#xff0c;表示第二個維度 axis -1&#xff0c;表示最后一個維度…

百戰c++(12)

36. 定義 int **a[3][4], 則變量占有的內存空間為&#xff1a;_____ 37. 編寫一個函數&#xff0c;要求輸入年月日時分秒&#xff0c;輸出該年月日時分秒的下一秒。如輸入2004年12月31日23時59分59秒&#xff0c;則輸出2005年1月1日0時0分0秒。 38.寫一個函數&#xff0c;判…

Struts2.3.5+Hibernate3+Spring3.1基于注解實現的多文件上傳,下載

Struts2.3.5Hibernate3Spring3.1基于注解實現的的多文件上傳&#xff0c;下載,這里是上傳文件到數據庫中&#xff0c;上傳控件可以增加和刪除&#xff0c;有需要的朋友可以看看。 以下是源碼下載地址&#xff1a;http://www.zuidaima.com/share/1639672872438784.htm jar包的下…

Pandas索引操作及高級索引——reindex()方法

文章目錄索引對象多個數據結構之間共享index類對象is與的區別重置索引——reindex()索引操作Series的索引操作切片不連續索引布爾型索引DataFrame的索引操作獲取不連續的Series對象切片Pandas庫中的操作索引方法索引對象 Index類對象&#xff0c;該對象不可以進行修改&#xf…

【精品計劃1】動態規劃入門到熟悉,看不懂來打我啊

持續更新。。。。。。 2.1斐波那契系列問題 2.2矩陣系列問題 2.3跳躍系列問題 3.1 01背包 3.2 完全背包 3.3多重背包 3.4 一些變形選講 2.1斐波那契系列問題 在數學上&#xff0c;斐波納契數列以如下被以遞歸的方法定義&#xff1a;F(0)0&#xff0c;F(1)1, F(n)F(n-1)…

Pandas數據排序——【按索引排序sort_index()方法、按值排序sort_value()方法】

文章目錄按索引排序——sort_index()對Series排序對DataFrame排序按值排序——sort_value()對Series進行排序對DataFrame進行排序按索引排序——sort_index() sort_index(axis0, levelNone, ascendingTrue, inplaceFalse, kind‘quicksort’, na_position‘last’,sort_remaini…

【大總結2】大學兩年,寫了這篇幾十萬字的干貨總結

本文是我大學兩年知識的總結。涵蓋數據結構、算法、語言基礎、操作系統、關系數據庫、NOSQL、網絡/前端/項目基礎知識、安全和測試、框架的學習、中間件和工具、設計模式和框架原理、我推薦的資料、我的建議 本篇文章應該算是Java后端開發技術棧的&#xff0c;但是大部分是基礎…

Pandas對象的層次化索引——【from_tuples()、from_arrays()、from_product()、swaplevel()、sort_index()、sort_values()】

文章目錄層次化索引的概念層次化索引的創建使用嵌套列表的方式構造層次化索引對象Series對象DataFrame對象通過MultiIndex類的方法構建層次化索引通過from_tuples()方法創建MultiIndex對象通過from_arrays()方法創建MultiIndex對象通過from_product()方法創建MultiIndex對象層次…

《這是全網最硬核redis總結,誰贊成,誰反對?》六萬字大合集

我攤牌了&#xff0c;這篇文章&#xff0c;值得99%的人收藏 此文后續會改為粉絲可見&#xff0c;所以喜歡的請提前關注和收藏&#xff0c;不迷路。 最近有五本我喜歡的redis實體新書&#xff0c;想要的去評論&#xff0c;我寫個隨機數抽獎包郵送給你。 那么&#xff0c;準備好…

Python數據預處理之異常值的處理——【自定義的three_sigma()函數、boxplot()方法】

文章目錄基于3σ原則檢測異常值代碼實現測試基于箱型圖檢測異常值異常值的處理基于3σ原則檢測異常值 3σ原則&#xff0c;又稱拉依達準則。是指假設一組檢測數據只含有隨機誤差。對其進行計算處理得到標準偏差&#xff0c;按一定概率確定一個區間&#xff0c;凡是超過這個區間…

那個谷歌的網紅扔雞蛋的題,來看看教科書式的回答

leetcode頂級難題&#xff0c;谷歌面試天天問&#xff0c;來看看吧&#xff0c;帶你來一步一步達到最優解。 谷歌不知道問了多少遍&#xff0c;藍橋杯也出現過&#xff0c;leetcode上是頂級難題&#xff0c;到底是什么題能如此頻繁地出現&#xff1f;我們一探究竟吧。 原題描述…