码迷,mamicode.com
首页 > 编程语言 > 详细

顺序表(C++版)

时间:2016-09-08 00:30:43      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

课程实践

#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++版)

标签:

原文地址:http://www.cnblogs.com/-beyond/p/5851421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!