📄 seqlist.cpp
字号:
// SeqList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "seqlist.h"
template <class Type> SeqList<Type>::SeqList(int sz){
if(sz>0) {
MaxSize = sz ;last= -1;
data=new Type[MaxSize];
}
}
template <class Type> int SeqList<Type>:: Find(Type & x)const
{
int i=0;
while (i<=last&&data[i]!=x) i++;
if(i>last) return -1;
else return i;
}
template <class Type> int SeqList<Type>:: IsIn(Type & x)
{
int i=0,found=0;
while(i<=last&&!found)
if(data[i]!=x) i++;
else found =1;
return found;
}
template <class Type> int SeqList<Type>:: Insert(Type & x,int i)
{ int j;
if (i<0||i>last+1||last==MaxSize-1) return 0;
else {
last++;
for(int i=last;j>i;j--) data[j]=data[j-1];
data[i]=x; return 1;
}
}
template <class Type> int SeqList<Type>:: Remove(Type & x)
{
int i=Find(x);
if(i>=0){
last--;
for(int j=i;j<=last;j++) data[j]=data[j+1];
return 1;
}
return 0;
}
template <class Type> int SeqList<Type>:: Next(Type & x)
{
int i=Find(x);
if(i>=0&&i<last) return i+1;
else return -1;
}
template <class Type> int SeqList<Type>::Prior(Type & x)
{
int i=Find(x);
if(i>0&&i<=last) return i-1;
else return -1;
}
template <class Type> void SeqList<Type>::print()
{
for (int i=0;i<=last;i++)
cout<<data[i]<<" ";
}
template <class Type> void SeqList<Type>::sort() //冒泡排序法
{ Type m;
for(int i=0;i<last;i++)
for(int j=0;j<last-i;j++)
{
if (data[j]>data[j+1])
{ m=data[j];
data[j]=data[j+1];
data[j+1]=m;}
}
}
template <class Type> Type SeqList<Type>::Maximum()
{sort();
return data[last];
}
void main(int argc, char* argv[]){
int num,n=0;
SeqList<int> obj1,obj2;
cout<<"please input some integer(<10),end with 0:";
cin>>num;
while (num!=0&&n<10)
{obj1.Insert(num,n);
cin>>num;
n++;}
cout<<"These number exist in this seqlist:";
obj1.print();
cout<<endl;
cout<<"start to sort(from small to big)";
obj1.sort();
obj1.print();
cout<<endl;
cout<<"the biggest one is "<< obj1.Maximum();
cout<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -