-
大小: 16.33MB文件類型: .rar金幣: 1下載: 0 次發布日期: 2023-07-22
- 語言: 其他
- 標簽: OnlineJudge??
資源簡介
上海交通大學,2015級IEEE試點班數據結構作業與Quiz題目,可供參考
代碼片段和文件信息
#include?
using?namespace?std;
class?noSpace?{};
class?outOfBound?{};
template?
class?seqList?{
????private:
????????elemType?*elem;?????//?順序表存儲數組,存放實際的數據結點。
??? int?len;????????????//?順序表中的結點個數,亦稱表的長度。
????????int?maxSize; ????//?順序表的的最大可能的長度。
????public:
? seqList?(?int?initSize?); //?構造函數
??? ~seqList?(?); ????//?析構函數
? bool?isEmpty?(?)?const?{?return?(?len?==?0?);?}
????????????//表為空返回TRUE否則返回FALSE。
????????bool?isFull?(?)?const?{?return?(len?==?maxSize);?}
????????????//?表是否已經滿,滿則返回TRUE,否則FALSE。
????????int?length?(?)?const;??//?表的長度
????????elemType?get?(?int?i?)?const;? ?//?返回第i個結點的值
????????int?find?(const?elemType?&e?);??//?返回值等于e的結點的序號,無則返回0
????????int insert?(int?i?const?elemType?&?e?);
????????????//?在第i個位置上插入新的結點(值為e),并
????????????//?使原來的第i個結點至最后一個結點的序號依次加1。
????????????//?插入成功返回1,否則為0
????????int??remove?(?int?i?elemType?&?e?);
????????????//?若第i個結點存在,刪除并將其值放入e
//?若i?非法,則刪除失敗,返回0。
????????void?clear(?);???? ????//?清空順序表
??? };
int?main()
{
??? seqList?chlist(21);
?????//定義一個SeqList類的對象,其中ElemType定義為char,
?????//空間大小為21,即除下標為0的數組元素外,最多可以
?????//放20個結點。
char?ctemp;
int?i?n?result;
?????????//?輸入一個正整數,并輸入?Enter?作為結束標志。
????cout?<“number?of?the?elements:“<????cin?>>?n;
????cin.get(ctemp); //??將?Enter?拋棄。
cout?<“input?the?elements:\n“?<
????try?{
//?將字符逐個輸入到表chlist中,并依次插入到表尾。
for?(?i=1;?i<=n;?i++)
{?cin.get?(?ctemp);??chlist.insert(?i?ctemp);?}
????????result=chlist.remove(1ctemp);?//刪除并獲得首結點.
????????result=chlist.insert(chlist.length(?)/2+1?ctemp);
????????//?將刪除的首結點插入到中間。
????????cout?<“output?the?elements:\n“?<
????? for?(?i=1;?i<=chlist.length(?);?i++)
cout.put(chlist.get(i));
//?讀取第i個結點的值并輸出。
?????cout?<‘\n‘;
????}?catch?(noSpace)?{cout<<“no?space?in?the?memory“< ??????catch?(outOfBound)?{cout<<“index?is?out?of?the?bounds“<
return?0;
}
template?
seqList::seqList?(?int?initSize?)
{ //?構造函數
????if?(?initSize?>?0?)
????{
????????elem?=?new?elemType[?initSize]; //?申請連續空間,返回空間首地址。
????????if?(!elem)?throw?noSpace();??//?若申請空間失敗,則程序中斷。
????????len=0;
????????maxSize?=?initSize-1;
????}
}
template?
seqList::~seqList?(?)
{ //?析構函數
????delete?[?]?elem;?//釋放占用的連續空間
}
template?
int?seqList::length?(?)?const
{ //?返回表的長度。
?? return?len;
}
template?
elemType?seqList::get?(?int?i?)?const
{ //?讀出第i個結點的值并返回。
????if(?(?i?1)?||?(?i?>?len?)?)?throw?outOfBound();?//?越界、非法。
?? return?elem[i];
}
template?
int?seqList::find?(?const?elemType?&e?)
{
????//?按照下標從大到小順序查找值為e的數組結點的下標并將其返回。
????//?elem[0]做哨兵用保證即使查找失敗,也可以在哨兵位上能找到值e。
????elem[0]=?e;?int?i?=?len;???//?初始位置為尾結點所在下標
????while?(elem[i]?!=?e?)?i--; //?不等時繼續向前比較,找到返回結點下標,否則返回0。
????return?i;
}
template?
int?seqList
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1047??2017-03-25?20:14??homework\1010\1010.cbp
?????文件????????132??2017-03-25?20:30??homework\1010\1010.depend
?????文件????????240??2017-03-27?09:38??homework\1010\1010.layout
?????文件?????936038??2017-03-25?20:31??homework\1010\bin\Debug\1010.exe
?????文件????????405??2017-03-25?20:31??homework\1010\main.cpp
?????文件??????30202??2017-03-25?20:31??homework\1010\obj\Debug\main.o
?????文件???????1047??2017-03-15?22:28??homework\1015\1015.cbp
?????文件????????240??2017-03-16?12:36??homework\1015\1015.layout
?????文件????????201??2017-03-16?12:36??homework\1015\main.cpp
?????文件???????1047??2017-03-25?19:59??homework\1021\1021.cbp
?????文件????????240??2017-03-27?09:38??homework\1021\1021.layout
?????文件????????201??2017-03-27?09:38??homework\1021\main.cpp
?????文件???????1050??2017-03-13?22:53??homework\10221\10221.cbp
?????文件????????133??2017-03-13?22:56??homework\10221\10221.depend
?????文件????????139??2017-03-16?12:36??homework\10221\10221.layout
?????文件?????939552??2017-03-13?22:56??homework\10221\bin\Debug\10221.exe
?????文件????????259??2017-03-13?22:56??homework\10221\main.cpp
?????文件??????29894??2017-03-13?22:56??homework\10221\obj\Debug\main.o
?????文件???????1047??2017-05-11?19:40??homework\1031\1031.cbp
?????文件????????191??2017-05-12?00:25??homework\1031\1031.depend
?????文件????????239??2017-05-13?17:20??homework\1031\1031.layout
?????文件?????999989??2017-05-12?17:05??homework\1031\bin\Debug\1031.exe
?????文件???????2305??2017-05-13?17:20??homework\1031\main.cpp
?????文件?????124466??2017-05-12?17:05??homework\1031\obj\Debug\main.o
?????文件???????1050??2017-05-11?23:39??homework\1031_\1031_.cbp
?????文件????????131??2017-05-11?23:48??homework\1031_\1031_.depend
?????文件????????241??2017-05-13?17:20??homework\1031_\1031_.layout
?????文件?????111511??2017-05-11?23:48??homework\1031_\bin\Debug\1031_.exe
?????文件???????1461??2017-05-11?23:48??homework\1031_\main.cpp
?????文件??????87610??2017-05-11?23:48??homework\1031_\obj\Debug\main.o
............此處省略1011個文件信息
- 上一篇:藍牙打印二維碼
- 下一篇:基于weka的文本分類器
評論
共有 條評論