資料結構 鍊錶PTA習題

2021-10-12 07:41:47 字數 3827 閱讀 9414

6-3 建立學生資訊鍊錶 (20分)

6-4 學生成績煉表處理 (20分)

題號題目答案1

結點的單鏈表中,實現下列哪個操作,其演算法的時間複雜度是o(n)?

遍歷鍊錶和求鍊錶的第i個結點

2對於乙個具有n個結點的單鏈表,在給定值為x的結點後插入乙個新結點的時間複雜度為

o(n)

3線性表若採用鏈式儲存結構時,要求記憶體中可用儲存單元的位址

連續或不連續都可以

4某線性表中最常用的操作是在最後乙個元素之後插入乙個元素和刪除第乙個元素,則採用什麼儲存方式最節省運算時間?

僅有尾指標的單迴圈鍊錶

5若某錶最常用的操作是在最後乙個結點之後插入乙個結點或刪除最後乙個結點。則採用哪種儲存方式最節省運算時間?

帶頭結點的雙迴圈鍊錶

6線性表l在什麼情況下適用於使用鏈式結構實現?

需不斷對l進行刪除插入

7將線性表la和lb頭尾連線,要求時間複雜度為o(1),且占用輔助空間盡量小。應該使用哪種結構?

帶尾指標的單迴圈鍊錶

8鍊錶不具有的特點是:

方便隨機訪問任一元素

9在單鏈表中,若p所指的結點不是最後結點,在p之後插入s所指結點,則執行

s->next=p->next; p->next=s;

10將兩個結點數都為n且都從小到大有序的單向鍊錶合併成乙個從小到大有序的單向鍊錶,那麼可能的最少比較次數是:

n本題要求實現乙個函式,找到並返回鏈式表的第k個元素。

l是給定單鏈表,函式findkth要返回鏈式表的第k個元素。如果該元素不存在,則返回error。

1 3 4 5 2 -1

63 6 1 5 4 2

4 na 1 2 5 3

#include

#include

#define error -1

typedef

int elementtype;

typedef

struct lnode *ptrtolnode;

struct lnode

;typedef ptrtolnode list;

list read()

;/* 細節在此不表 */

elementtype findkth

(list l,

int k)

;int

main()

return0;

}/* 你的**將被嵌在這裡 */

elementtype findkth

(list l,

int k)

if(p&&count == k)

return p-

>data;

else

return error;

}

本題要求實現乙個將輸入的學生成績組織成單向鍊錶的簡單函式。

函式介面定義:

void

input()

;

該函式利用scanf從輸入中獲取學生的資訊,並將其組織成單向鍊錶。鍊錶節點結構定義如下:

struct stud_node 

;

單向鍊錶的頭尾指標儲存在全域性變數head和tail中。

輸入為若干個學生的資訊(學號、姓名、成績),當輸入學號為0時結束。

1 zhang 78

2 wang 80

3 li 75

4 zhao 85

0

1 zhang 78

2 wang 80

3 li 75

4 zhao 85

#include

#include

#include

struct stud_node

;struct stud_node *head,

*tail;

void

input()

;int

main()

/* 你的**將被嵌在這裡 */

void

input()

//tail為開闢節點

if(tail !=

null

)// 把新加的節點賦予tail

tail = pt;

tail-

>next =

null

; pt =

(struct stud_node *

)malloc

(sizeof

(struct stud_node));

scanf

("%d"

,&pt-

>num);}

}

本題要求實現兩個函式,乙個將輸入的學生成績組織成單向鍊錶;另乙個將成績低於某分數線的學生結點從鍊錶中刪除。

函式介面定義:

struct stud_node *

createlist()

;struct stud_node *

deletelist

(struct stud_node *head,

int min_score )

;

函式createlist利用scanf從輸入中獲取學生的資訊,將其組織成單向鍊錶,並返回煉表頭指標。鍊錶節點結構定義如下:

struct stud_node 

;

輸入為若干個學生的資訊(學號、姓名、成績),當輸入學號為0時結束。

函式deletelist從以head為頭指標的鍊錶中刪除成績低於min_score的學生,並返回結果鍊錶的頭指標。

1 zhang 78

2 wang 80

3 li 75

4 zhao 85

080

2 wang 80

4 zhao 85

#include

#include

struct stud_node

;struct stud_node *

createlist()

;struct stud_node *

deletelist

(struct stud_node *head,

int min_score )

;int

main()

/* 你的**將被嵌在這裡 */

struct stud_node *

createlist()

return head;

}struct stud_node *

deletelist

(struct stud_node *head,

int min_score)

// 先檢查頭結點是否小於

while

(head!=

null

&&head-

>score// 如果剛才刪除了頭結點,頭結點還為空直接返回

if(head==

null

) p1 = head;

p2 = p1-

>next;

// 頭結點沒問題了 看頭結點以後

// 後面要是沒有直接跳過迴圈return head就行

while

(p2!=

null

)else

// 讓p2等於下乙個繼續檢驗

p2 = p1-

>next;

}return head;

}

PTA資料結構(鍊錶)

pta資料結構 鍊錶 本題要求實現乙個函式,將兩個鍊錶表示的遞增整數序列合併為乙個非遞減的整數序列。函式介面定義 list merge list l1,list l2 其中list結構定義如下 typedef struct node ptrtonode struct node typedef ptr...

資料結構 鍊錶習題

include includetypedef int elemtype struct student typedef struct student list list createlist else scanf d cur data prev cur printf list created succ...

資料結構 鍊錶習題

1.在單向鍊錶中,頭指標中存放的是頭結點的內容。t f 2.單向鍊錶中的每個結點都需要動態分配記憶體空間。tf 3.通常使用結構的巢狀來定義單向鍊錶結點的資料型別。t f 4.用鍊錶代替陣列進行資料操作時,查詢更加方便。tf 1.以下程式的輸出結果是 struct har h 2 int main ...