您的位置:首页 > 理论基础 > 数据结构算法

数据结构——顺序表

2012-09-10 18:16 183 查看
#include <iostream>
using namespace std;
int a[9]={1,2,3,4,5};
int i;
void insert();
void display();
void deleted();
void search();
void change();
int main()
{
cout<<"you should not input ZERO"<<endl;
display();
insert();
display();
deleted();
display();
search();
change();
display();
return 0;
}

void insert()//you can use this method to insert a date
{
int date;
int number;
cout<<"the number you wanna insert:"<<endl;
cin>>date;
cout<<endl;
cout<<"the date's location: "<<endl;
cin>>number;
for(int k=0;k<=(i-number+1);k++)
{
a[i+1-k]=a[i-k];
}
a[number-1]=date;
}

void display()//you can use this method to display all the date
{
for(i=0;a[i]!=NULL;i++)
{
cout<<a[i]<<endl;
}
}

void search()//you can use this method to search a date
{
int locationNUM;
cout<<"the date's location"<<endl;
cin>>locationNUM;
cout<<a[locationNUM-1]<<endl;
if(locationNUM>i)
cout<<"Data overflow"<<endl;
}

void change()//you can use this method to change a date
{
int date,location;
cout<<"change date"<<endl;
cin>>date;
cout<<"the date's location"<<endl;
cin>>location;
if(location-1>i)
cout<<"Data overflow"<<endl;
else
a[location-1]=date;
}

void deleted()//you can use this method to delete a date
{
int date;
int number;
cout<<"the number you wanna delete:"<<endl;
cin>>number;
for(;number<i;number++)
{
a[number-1]=a[number];
}
a[i-1]=NULL;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: