資料結構線性表答案

2021-05-31 15:54:54 字數 3139 閱讀 6846

a. 在p結點後插入s結點的語句序列是

b. 在p結點前插入s結點的語句序列是

c. 在表首插入s結點的語句序列是

d. 在表尾插入s結點的語句序列是

(1) p->next=s;

(2) p->next=p->next->next;

(3) p->next=s->next;

(4) s->next=p->next;

(5) s->next=l;

(6) s->next=null;

(7) q=p;

(8) while(p->next!=q) p=p->next;

(9) while(p->next!=null) p=p->next;

(10) p=q;

(11) p=l;

(12) l=s;

(13) l=p;

解:a. (4) (1)

b. (7) (11) (8) (4) (1)

c. (5) (12)

d. (9) (1) (6)

2.7 已知l是帶表頭結點的非空單鏈表,且p結點既不是首元結點,也不是尾元結點,試從下列提供的答案中選擇合適的語句序列。

a. 刪除p結點的直接後繼結點的語句序列是

b. 刪除p結點的直接前驅結點的語句序列是

c. 刪除p結點的語句序列是

d. 刪除首元結點的語句序列是

e. 刪除尾元結點的語句序列是

(1) p=p->next;

(2) p->next=p;

(3) p->next=p->next->next;

(4) p=p->next->next;

(5) while(p!=null) p=p->next;

(6) while(q->next!=null)

(7) while(p->next!=q) p=p->next;

(8) while(p->next->next!=q) p=p->next;

(9) while(p->next->next!=null) p=p->next;

(10) q=p;

(11) q=p->next;

(12) p=l;

(13) l=l->next;

(14) free(q);

解:a. (11) (3) (14)

b. (10) (12) (8) (3) (14)

c. (10) (12) (7) (3) (14)

d. (12) (11) (3) (14)

e. (9) (11) (3) (14)

2.8 已知p結點是某雙向鍊錶的中間結點,試從下列提供的答案中選擇合適的語句序列。

a. 在p結點後插入s結點的語句序列是

b. 在p結點前插入s結點的語句序列是

c. 刪除p結點的直接後繼結點的語句序列是

d. 刪除p結點的直接前驅結點的語句序列是

e. 刪除p結點的語句序列是

(1) p->next=p->next->next;

(2) p->priou=p->priou->priou;

(3) p->next=s;

(4) p->priou=s;

(5) s->next=p;

(6) s->priou=p;

(7) s->next=p->next;

(8) s->priou=p->priou;

(9) p->priou->next=p->next;

(10) p->priou->next=p;

(11) p->next->priou=p;

(12) p->next->priou=s;

(13) p->priou->next=s;

(14) p->next->priou=p->priou;

(15) q=p->next;

(16) q=p->priou;

(17) free(p);

(18) free(q);

解:a. (7) (3) (6) (12)

b. (8) (4) (5) (13)

c. (15) (1) (11) (18)

d. (16) (2) (10) (18)

e. (14) (9) (17)

2.9 簡述以下演算法的功能。

(1) status a(linkedlist l)

(2) void bb(lnode *s, lnode *q)

void aa(lnode *pa, lnode *pb)

解:(1) 如果l的長度不小於2,將l的首元結點變成尾元結點。

(2) 將單迴圈鍊錶拆成兩個單迴圈鍊錶。

2.10 指出以下演算法中的錯誤和低效之處,並將它改寫為乙個既正確又高效的演算法。

status deletek(sqlist &a,int i,int k)

return ok;}解:

status deletek(sqlist &a,int i,int k)

2.11 設順序表va中的資料元素遞增有序。試寫一演算法,將x插入到順序表的適當位置上,以保持該錶的有序性。

解:status insertorderlist(sqlist &va,elemtype x)

2.12 設和均為順序表,和分別為和中除去最大共同字首後的子表。若空表,則;若=空表,而空表,或者兩者均不為空表,且的首元小於的首元,則;否則。試寫乙個比較,大小的演算法。

解:status ***pareorderlist(sqlist &a,sqlist &b)

if(a.length>k) j=1;

if(b.length>k) j=-1;

if(a.length==b.length) j=0;

return j;

}2.13 試寫一演算法在帶頭結點的單鏈表結構上實現線性表操作locate(l,x);

解:int locateelem_l(linklist &l,elemtype x)

if(!p) return 0;

else return i;

}2.14 試寫一演算法在帶頭結點的單鏈表結構上實現線性表操作length(l)。

解://返回單鏈表的長度

int listlength_l(linklist &l)

{ int i=0;

linklist p=l;

if(p) p=p-next;

資料結構線性表作業

void main主函式 檔案中所定義的全域性變數的變數名和主要功能 暫無 檔案中用到的他處定義的全域性變數及其出處 暫無 與其他檔案的依賴關係 暫無 和資料的出處 資料結構 清華大學出版社 include include include include sleep 用到 define list i...

資料結構第2章線性表答案

第2章自測卷答案姓名班級 一 填空 每空1分,共13分 1.嚴題集2.2 在順序表中插入或刪除乙個元素,需要平均移動表中一半元素,具體移動的元素個數與表長和該元素在表中的位置有關。2.線性表中結點的集合是有限的,結點間的關係是一對一的。3.向乙個長度為n的向量的第i個元素 1 i n 1 之前插入乙...

資料結構實驗線性表

實驗報告 電腦科學與技術學院 2015 年 10 月 8 日 實驗專案名稱線性表的基本操作 1 實驗目的 1 掌握上機除錯線性表的基本方法 2 掌握線性表的一些基本操作 二 實驗內容 問題描述 實現線性表的建立 修改元素 插入 刪除等線性表的基本操作。基本要求 1 依次從鍵盤讀入資料,建立線性表 3...