山東大學 資料結構 實驗3 線性表操作

2021-09-01 09:25:28 字數 3658 閱讀 7383

一、 要求完成時間

實驗開始後的第四周之前完成

二、 實驗目的

掌握線性表的基本操作:插入、刪除、查詢。

掌握鍊錶遍歷器的使用方法。

三、 實驗內容

1、 輸入n個不為零的整數作為節點元素值,遇到0代表輸入結束(不建立元素值為0的節點),建立有序鍊錶。輸出整個鍊錶。

2、 輸入乙個整數,將該數插入到有序表相應位置。輸出整個鍊錶。

3、 輸入乙個整數,在鍊錶中進行搜尋,輸出其在鍊錶中的位置。如果不存在輸出0。

4、 再一次輸入乙個整數,在鍊錶中進行搜尋,輸出其在鍊錶中的位置。如果不存在輸出0。

5、 再一次輸入n個不為零的整數作為節點元素值,遇到0代表輸入結束(不建立元素值為0的節點),建立有序鍊錶。輸出整個鍊錶。

6、 使用鍊錶遍歷器實現上面兩個有序鍊錶的合併,輸出合併後的鍊錶。

8、輸入輸出請嚴格按下面要求的格式實現

template class chain//建構函式

~chain();//析構函式,就是刪掉鍊錶中的每乙個節點

bool isempty()const

int length()const;//返回鍊錶的長度

bool find(int k,t& x)const;//尋找第k個元素並將其放到x中

void erase();//刪除鍊錶中的所有節點

void output(ostream& out) const;//輸出鍊錶

int search(const t& x)const;//尋找是否存在x這個值

chain& delete(int k,t& x);//刪除節點

chain& insert(int k,const t& x);//插入節點

chain& autoinsert(const t& x);//自動尋找位置並插入節點

chain& reverse1();//反序1 

chain& reverse2(chain& a,chain& b);//反序2

chain& merge(chain& a,chain& b);

//private: 不用遍歷輸出的時候,這個地方設定為private,如果是使用遍歷輸出鍊錶就需要設定為public

chainnode*last;

chainnode*first;

};//析構函式

template chain::~chain()

//返回鍊錶的長度

template int chain::length()const

return len;

}//將第k個元素放在x中

template bool chain::find(int k, t& x)const

chainnode*current = first;

int index=1;

while (index < k)

if (current)

return false;

}//第幾個元素是x元素

template int chain::search(const t& x)const

if (current)

return 0;

}//刪除第k個節點,並將第k個節點的值,放在x中

template chain& chain::delete(int k, t& x)

chainnode*p = first;

if (k==1) else

if (!q || !q->link)

p=q->link;

q->link=p->link;

}x=p->data;

delete p;

return *this;

}//輸出鍊錶

templatevoid chain::output(ostream& out)const

//插入操作

template chain& chain::insert(int k, const t& x)

if (k>0 && !p)

chainnode*y = new chainnode();

y->data=x;

if(k)else

return *this;

}//自動插入操作

template chain& chain::autoinsert(const t& x)else

//中間 

while(p->link)

p = p->link;

}//尾部 

p->link = y;

}     return *this;

}template void chain::erase()

}    chainnode*y = new chainnode();

y->data = x;

y->link=0;

if (first) else

return *this;

}//反序 1

template chain& chain::reverse1()

chainnode*temp = last;

last = first;

first = temp;

return *this; 

}//反序 2

template chain& chain::reverse2(chain& a,chain& b)

} //c=a+b

template chain& chain::merge(chain& a,chain& b)

else

if(pc == 0)

else

}if(pa) pc->link = pa;

else pc->link = pb;

a.first = 0;

b.first = 0;

return *this;

}//遍歷器

template class chainiterator

return 0;

}t* next()

private:

chainnode*location;  };

//建立乙個新的有序線性表c,該表中包含了a和b的所有元素。 

template void merge(const chain& a,const chain& b,chain& c)

else

}if(dataa)

while(dataa)

else

while(datab)

}int main()

cout

a.autoinsert(input_number);

cout

cout

cout

if(input_number != 0) 

b.autoinsert(input_number);

}cout<

推送自己的一些學習筆記,實驗源**等,

山東大學《資料結構》實驗二 排序演算法

掌握各種排序方法的實現思想。1 輸入 2 20 個不為零的正整數,遇到 0 代表輸入結束,0 不參與排序。2 數字選擇排序方法,1 氣泡排序,2 插入排序,3 基數排序 3 基數排序能夠僅僅實現小於 10 的正整數的排序。有大於 9 的輸入時,直接輸出 0。4 使用所選排序方法的排序,結果輸出所用方...

山東大學《資料結構》實驗四 堆疊的應用

掌握堆疊的使用。1 輸入乙個數學表示式 假定表示式輸入格式合法 計算表示式結果 並輸出。2 數學表示式由單個數字和運算子 構成,例如 2 3 4 5 6 4。3 變數 輸出採用整數,只舍不入。本題測試一定要多找幾個測試用例,特別是複雜用例 邊界用例。否 則,很容易造成宕機或者沒有返回結果。假設輸入的...

山東大學資料庫系統實驗三

宣告 所有sql語句均在實驗平台驗證通過,實驗細節可能隨時間推移老師會進行修改。在此僅提供解答思路,畢竟我的方法肯定不是最優,而且實驗平台有查重功能,不要一昧的複製哦!1.刪除表中的學號不全是數字的那些錯誤資料,學號應該是數字組成,不能夠包含字母空格等非數字字元。create table test3...