数据结构顺序表的基本操作

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
if(list.elem[i]==j)
cout<<"已找到,该元素位于第"<<i+1<<"位, ";
else cout<<"sorry, ";
cout<<"共比较了"<<i+1<<"次";
cout<<endl;
slow();
}
//屏幕提示后,从键盘输入一个元素值,在线性表list中删除这个元素
//屏幕显示成功与否的信息,并显示比较次数和移动次数
void display(SqList &list);
void insert(SqList &list);
void search(SqList &list);
voiddel(SqList &list);
void simpleSort(SqList &list);
void quickSort(SqList &list);
}
//编程实现一个顺序表的就地逆置,即利用原表的储存空间将顺序表逆置
void nzlist(SqList &list)
{
int t,n=list.length;
for(int i=0;i<n/2;i++)
t=list.elem[n-1-i];
list.elem[n-1-i]=list.elem[i];
cout << "\t\t 9:逆置线性表\n";
cout << "\n";
cout << "\t\t 0:退出\n";
cout << "\n";
cout << "\t\t请选择:" << flush;
choice = getch();
system("cls");
switch(choice)
{
case '1':
cout << endl;
}
while (1)
{
cout << "输入随机数种子(0-32767):" << flush;
cin >> i;
if (i >= 0 && i <= 32767)
break;
cout << endl;
}
srand(i); //指定随机数种子,相同的种子将产生相同的数据序列
//屏幕显示查找结果,并显示比较次数
void binarySearch(SqList &list)
{
int i,low,high,mid,m=0,n=0;cout<<"请输入要查找的值:";cin>>i;
low=0,high=list.length-1;
while(low<=high){
mid=(low+high)/2;
int x;
int n;
int i;
cin>>x;
cin>>n;
if (list.length>=0 && list.length<=max)
{for (i=list.length-1;i>n;i--)
{
list.elem[i+1]=list.elem[i];
}
list.elem[n]=x;
list.length++;}
voiddel(SqList &list)
{
int i=0,j;
cout<<"请输入要删除的值:";
cin>>j;
while(i<list.length&&list.elem[i]!=j)i++;
if(list.elem[i]==j)
{for(int m=i;m<list.length-1;m++)
list.elem[i]=t;
}
//格式应便于观察
//如果需要制定输出的宽度,可以使用cout << setw(W) << X其中X是输出的数值,W是占据的列数
void display(SqList &list)
{
cout<<"共有"<<list.length<<"个元素"<<"分别是:";
for(int i=0;i<list.length;i++)
{
int pivotloc=0;
if(low<high)
{
pivotloc=Partition(list,low,high);
QSort(list,low,pivotloc-1);
QSort(list,pivotloc+1,high);
}
}
//屏幕提示后,从键盘输入一个元素值,对经过排序的线性表list进行折半查找
if(i==list.elem[mid])break;
else if(i>list.elem[mid]){low=mid+1;m++;}
else {high=mid-1;n++;}
}
if(i!=list.elem[mid])cout<<"查找失败"<<endl;
else coutFra Baidu bibliotek<"位于第"<<mid+1<<"位"<<",比较"<<m+n+1<<"次"<<endl;
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
else
{
cout<<"插入失败"<<endl;
}
}
//屏幕提示后,从键盘输入一个元素值,在线性表list中搜索这个元素
//屏幕显示搜索结果和搜索过程的比较次数
void search(SqList &list)
{
int i=0,j;cout<<"请输入要搜索的数据:";
cin>>j;
while(i<list.length&&list.elem[i]!=j)i++;
cout << "\t\t 2:显示\n";
cout << "\t\t 3:单个插入\n";
cout << "\t\t 4:查找\n";
cout << "\t\t 5:删除\n";
cout << "\t\t 6:简单选择排序\n";
cout << "\t\t 7:快速排序\n";
cout << "\t\t 8:折半查找\n";
rand();
for (i = 0; i < list.length; i++)
{
list.elem[i] = rand() % 10000;
}
for (i = list.length; i < max; i++)
list.elem[i] = 0;
}
//在屏幕上依次显示线性表list中的元素个数和全部元素
break;
case '7':
quickSort(list);
break;
case '8':
binarySearch(list);
break;
case '9':
nzlist(list);
break;
case '0':
exit(0);
}
}
}
//屏幕提示后,从键盘输入线性表长度和随机数种子,生成指定长度的线性表list
{int CPnum=0,
QSnum=0;
int pivotkey=list.elem[low];
while(low<high)
{
while(low<high&&list.elem[high]>=pivotkey) { --high;++CPnum;}
list.elem[low]=list.elem[high]; ++QSnum;
list.elem[m]=list.elem[m+1];list.length--;cout<<"completed"<<endl;}
else cout<<"failed"<<endl;
cout<<"比较次数:"<<i+1<<",移动次数:"<<list.length-i<<endl;
slow();
void init(SqList &list)
{
int i;
while (1)
{
cout << "输入元素个数(0-"<< max << "):"<< flush;
cin >> list.length;
if (list.length >= 0 && list.length <= max)
break;
void quickSort(SqList &list)
{
int CPnum=0,
QSnum=0;
QSort(list,0,list.length-1);
cout<<"比较次数:"<<CPnum<<endl;
cout<<"移动次数:"<<QSnum<<endl;
slow();
}
int Partition(SqList &list,int low,int high)
#include <fstream.h>
const max=10000;
struct SqList
{
int elem[max]; //存放元素的数组
int length; //当前长度
};
void slow()
{
cout << "\n\n请按任意键继续" << flush;
getch();
}
void init(SqList &list);
cout<<list.elem[i]<<",";
}
//屏幕提示后,从键盘输入一个元素值和一个插入位置值,然后把这个新元素插到线性表list的指定位置处
//应有溢出判断和报告
void insert(SqList &list)
{ cout<<"请输入要插入的值:"<<endl;
cout<<"请输入要插入的位置"<<endl;
while (1)
{
system("cls");
cout << "\n\n\n\n";
cout << "\t\t顺序表操作\n";
cout << "\t\t======================================";
cout << "\n\n";
cout << "\t\t 1:初始化\n";
}
//对线性表list进行简单排序选择
//屏幕显示比较次数和移动次数
void simpleSort(SqList &list)
{
int i,j,k,c=0,d=0;
for(i=0;i<list.length-1;i++)
for(j=i;j<list.length;j++)
{ d++;
if(list.elem[j]<list.elem[i])
void quicksort_cnt(void);
void binarySearch(SqList &list);
void nzlist(SqList &list);
SqList list;
int Partion(SqList &list,int ,int );
void main()
{
char choice;
{c++;k=list.elem[j];list.elem[j]=list.elem[i];list.elem[i]=k;}}
cout<<"比较次数:"<<d<<",移动次数:"<<c<<endl;
slow();
}
//对线性表list进行快速排序
//屏幕显示比较次数和移动次数
void QSort (SqList &list,int low,int high);
while(low<high&&list.elem[low]<=pivotkey) {++low;++CPnum;}
list.elem[high]=list.elem[low]; ++QSnum;
}
list.elem[low]=pivotkey;
return low;
}
void QSort (SqList &list,int low,int high)
init(list);
break;
case '2':
display(list);
break;
case '3':
insert(list);
break;
case '4':
search(list);
break;
case '5':
del(list);
break;
case '6':
simpleSort(list);
相关文档
最新文档