首页 > 代码库 > 顺序表(C++)
顺序表(C++)
以下为数据结构中的顺序表实现代码,已测试能够运行。虽然说是C++版的,但是其实应该是C语言班的。C++应该是面向对象,用抽象方法实现,而以下代码是面向过程的,只是把C语言中的输入(scanf)和输出(printf)改为了cin和cout而已。如果想要改为C++版的,可以将各个函数变为类的成员函数,使用方法改为调用类的成员方法而已,没有什么特别的。
#include<iostream>#include<stdlib.h>//清屏操作的头文件using namespace std;//主要操作的函数声明void insert(int A[],int &length,int);int _delete(int A[],int &length,int n);int locate(int A[],int length,int n);int get(int A[],int length,int );void create(int A[],int length);void show(int A[],int length);void main1();int main(){ int action,A[100],length; char c; main1(); while(cin>>action){ if(action==1){ //创建 system("cls"); cout<<"please input the length:"<<endl; cin>>length; create(A,length); system("pause"); system("cls"); main1(); } else if(action==2){ //插入 system("cls"); cout<<"please input the integer:"<<endl; int n; cin>>n; insert(A,length,n); cout<<"OK! the number has been inserted!"<<endl; system("pause"); system("cls"); main1(); } else if(action==3){ //打印 system("cls"); show(A,length); system("pause"); system("cls"); main1(); } else if(action==4){ //获取 system("cls"); cout<<endl<<"please input the number‘s index you want to get:"<<endl; int n; cin>>n; int result=get(A,length,n); if(!result){ cout<<"wrong! can‘t find the number in the Array."<<endl; } else { cout<<"OK! find it! it‘s "<<result<<endl; } system("pause"); system("cls"); main1(); } else if(action==5){ //删除 system("cls"); cout<<endl<<"please input the number you want to delete:"<<endl; int n; cin>>n; int result=_delete(A,length,n); if(!result){ cout<<"wrong! can‘t find the number in the Arrar."<<endl; } else { cout<<"OK! the number has been deleted"<<endl; } system("pause"); system("cls"); main1(); } else if(action==6){ //定位 system("cls"); cout<<endl<<"please input the number you want to locate:"<<endl; int n; cin>>n; int result=locate(A,length,n); if(!result){ cout<<"wrong! can‘t find the number in the Array."<<endl; } else { cout<<"the index of "<<n<<" is "<<result<<endl; } system("pause"); system("cls"); main1(); } else { //退出 exit(0); } }}void insert(int A[],int &length,int n){ int i=0; while(i<length&&A[i]<n){ i++; } int temp=i; i=length; while(i>temp){ A[i]=A[i-1]; i--; } A[temp]=n; length++;}int _delete(int A[],int &length,int n){ //用于删除确定的一个数,可稍加修改,用于删除下表为n的数 int i=0; while(i<length&&A[i]!=n){ i++; } if(i==length){ //如果i等于数组的长度,则表示未查找到n值。 return 0; } else { //如果查找到n值。 while(i<length-1){ A[i]=A[i+1]; i++; } length--; return 1; }}int locate(int A[],int length,int n){ int i=0; while(i<length&&A[i]!=n){ i++; } if(i==length){ return 0; } else { return i; }}int get(int A[],int length,int index){ if(index<0||index>length-1){ //当索引下标小于0或大于长度-1的时候,就超出了数组范围。 return 0; } else { return A[index]; }}void create(int A[],int length){ cout<<"please input the numbers of the Array"<<endl; for(int i=0;i<length;i++){ cin>>A[i]; } cout<<endl<<"OK! the Array have been created!"<<endl;}void show(int A[],int length){ cout<<"current Array is:"<<endl; for(int i=0;i<length;i++){ cout<<A[i]<<‘ ‘; } cout<<endl;}void main1(){ cout<<"--------------------------------------------"<<endl; cout<<"| |"<<endl; cout<<"| welcome to use the SquenticalList |"<<endl; cout<<"| 1->create the list |"<<endl; cout<<"| 2->insert to the list |"<<endl; cout<<"| 3->show the list |"<<endl; cout<<"| 4->get the number |"<<endl; cout<<"| 5->delete the number |"<<endl; cout<<"| 6->locate the numbet |"<<endl; cout<<"| 7->exit |"<<endl; cout<<"| |"<<endl; cout<<"--------------------------------------------"<<endl;}
顺序表(C++)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。