實驗內容:
1、隨機產生或鍵盤輸入一組元素,建立乙個帶頭結點的單向鍊錶(無序)。
2、遍歷單向鍊錶
(顯示)。3、
把單向鍊錶中元素逆置(不允許申請新的結點空間)。
4、在單向鍊錶中刪除所有的偶數元素
(值為偶數)
結點。5、
編寫在非遞減有序鍊錶中插入乙個元素使鍊錶元素仍有序的函式,並利用該函式建立乙個非遞減有序單向鍊錶。
6、利用演算法5建立兩個非遞減有序單向鍊錶,然後合併成乙個非遞增鍊錶。 7、
利用演算法5建立兩個非遞減有序單向鍊錶,然後合併成乙個非遞減鍊錶。 8、
編寫乙個主函式,除錯上述演算法。
**:#include
#include
#include
#include
using namespace std;
typedef struct nodelnode,*linklist;
linklist createfromhead()
return head;
linklist sort(linklist head)
linklist temp1,temp2,q,p;
for(q=head;q->next!=null;q=q->next)
for(p=q->next;p->next!=null;p=p->next)
if(p->next->datanext->data)
if(q->next==p)
temp1=p->next;
p->next=p->next->next;
temp1->next=q->next;
q->next=temp1;
p=temp1;
else
temp1=p->next;
temp2=q->next;
p->next=p->next->next;
q->next=q->next->next;
temp1->next=q->next;
q->next=temp1;
temp2->next=p->next;
p->next=temp2;
return head;
linklist hecheng(linklist head1,linklist head2){
linklist p;
p=head2;
while(p->next!=null)
p=p->next;
charu(head1,p->data);
return head1;
linklist sort_(linklist head)
linklist temp1,temp2,q,p;
for(q=head;q->next!=null;q=q->next)
for(p=q->next;p->next!=null;p=p->next)
if(p->next->data>q->next->data)
if(q->next==p)
temp1=p->next;
p->next=p->next->next;
temp1->next=q->next;
q->next=temp1;
p=temp1;
else
temp1=p->next;
temp2=q->next;
p->next=p->next->next;
q->next=q->next->next;
temp1->next=q->next;
q->next=temp1;
temp2->next=p->next;
p->next=temp2;
return head;
int main()
linklist head=null,p=null,head1=null,head2=null;
int x;
head=createfromhead();
cout<
show(p);
cout<
p=daozhi(p);
show(p);
cout<
p=shanchu(p);
show(p);
cout<
sort(p);
show(p);
cout<
cin>>x;
cout<
charu(p,x);
show(p);
cout<
p=head1;
sort(p);
head2=createfromhead_();
p=head2;
sort(p);
cout<
show(p);
cout<
show(p);
return 0;
測試截圖:
資料結構實驗一 線性表的有關操作
實驗學時 2學時 背景知識 順序表 鍊錶的插入 刪除及應用。目的要求 1 掌握線性表的基本運算在兩種儲存結構 順序結構和鏈式結構 上的實現 2 掌握線性表的各種操作 建立 插入 刪除等 的實現演算法 實驗內容 1 隨機產生或鍵盤輸入一組元素,建立乙個帶頭結點的單向鍊錶 無序 2 遍歷單向鍊錶。3 把...
資料結構實驗一 線性表的相關操作
題目內容 將單鏈表每個元素的值改為該結點值加上後繼結點值,若沒有後繼元素則保持不變。例如,已知單鏈表為 2 4 10 6 則修改後為 6 14 16 6 輸入輸出說明 輸入 6 單鏈表元素個數 10 30 20 60 80 91 正序輸入單鏈表元素值 輸出 40 50 80 140 171 91 修...
C 資料結構實驗一線性表
實驗內容 1.建立乙個順序表,隨機產生 10 個 100 以內的整數,並按要求完成 1 編寫顯示函式,在螢幕上顯示順序表中的 10 個整數 2 編寫查詢函式,從鍵盤輸入任一整數在順序表中查詢,若找到,返回該元素在順序表中的位置,否則提示無此元素 3 編寫插入函式,從鍵盤輸入待插入元素及插入位置,將完...