?? linklistmain.cpp
字號:
//LinkListMain.cpp
#include <iostream.h> //引用輸入輸出流庫函數的頭文件
#include "LinkList.cpp" //引用單鏈表的類
void main( )
{
LinkList <int> a;
cout<<"執行插入操作:"<<endl;
try
{
a.Insert(1,4);
a.Insert(2,5);
a.Insert(3,6);
}
catch(char* wrong)
{
cout << wrong; //如失敗提示失敗信息
}
cout<<"已經插入“4,5,6”"<<endl;
cout<<"單鏈表a的長度為:"<<endl;
cout<<a.Length()<<endl; //返回單鏈表長度
cout<<endl;
cout<<"單鏈表a的元素為:"<<endl;
a.PrintList(); //顯示鏈表中所有元素
cout<<endl;
cout<<"按位查找第二個元素:"<<endl;
cout<<"第二個元素為:";
cout<<a.Get(2)<<endl; //查找鏈表中第二個元素
cout<<endl;
cout<<"按值查找5"<<endl;
cout<<"值為5的元素位置為:";
cout<<a.Locate(5)<<endl; //查找元素5,并返回在單鏈表中位置
cout<<endl;
cout<<"執行刪除4的操作"<<endl;
a.Delete(1); //刪除元素4
cout<<"已刪除成功,單鏈表a的長度為";
cout<<a.Length()<<endl;
cout<<endl;
cout<<"鏈表a中的元素為:"<<endl;
a.PrintList();
int r[ ]={1,2,3,4,5};
LinkList <int> b(r,5); //根據數組創建單鏈表
cout<<"執行插入操作前單鏈表b為:"<<endl;
b.PrintList(); //輸出單鏈表所有元素
cout<<"插入前單鏈表b的長度為:";
cout<<b.Length()<<endl;
try
{
b.Insert(3,8);
}
catch(char* wrong)
{
cout << wrong; //如失敗提示失敗信息
}
cout<<"執行插入操作后單鏈表b為:"<<endl;
b.PrintList(); //輸出單鏈表b所有元素
cout<<"插入后單鏈表b的長度為:";
cout<<b.Length()<<endl;
cout<<endl;
try
{
if(a.Length()){
cout<<"執行刪除第一個元素操作:"<<endl;
cout<<endl;
b.Delete(1); //刪除b中第一個元素
cout<<"已刪除成功,單鏈表b的長度為:";
cout<<b.Length()<<endl;
}
else{
cout<<"順序表b長度為0"<<endl;
}
}
catch(char* wrong)
{
cout << wrong; //如失敗提示失敗信息
}
cout<<"執行插入操作后單鏈表b為:"<<endl;
b.PrintList(); //輸出單鏈表所有元素
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -