📄 1.h
字号:
#include <iostream.h>
#include <iomanip.h>
const int max = 5;
template <typename list_type>
class list{
public:
list();
~list();
int length() const; //求长度
void enter(int i , list_type x); //输入线性表
list_type get( int i ) ; //按序号取元素
int locate(list_type x); //按值查询
void insert(int i , list_type x); //插入元素
void del (int i); //删除元素
void print(); //打印线性表的元素
private:
int count;
list_type data[max];
};
template <typename list_type>
list<list_type>::list()
{
count = 0;
}
template <typename list_type>
list<list_type>::~list()
{}
template <typename list_type>
void list<list_type>::enter( int i , list_type x )
{
data[i] = x;
count++;
}
template <typename list_type>
int list<list_type>::length() const
{
return count;
}
template <typename list_type>
list_type list<list_type>::get( int i )
{
list_type x;
if( i<1 || i>count )
cout<<"unsuccess"<<endl;
else
x=data[i-1];
return x;
}
template <typename list_type>
int list <list_type>::locate (list_type x)
{
for( int j=1 ; j<=count ; j++ )
if( x == data[j-1] )
return j;
if( x != data[j-1] )
cout<<"表中无此元素"<<endl;
return 0;
}
template <typename list_type>
void list<list_type>::insert ( int i , list_type x )
{
if( count > max )
cout<<"overflow"<<endl;
if(i<1||i>count+1)
cout<<"unsuccess"<<endl;
for( int j=count-1 ; j>=i-1 ; j-- )
data[j+1]=data[j];
data[i-1]=x;
count++;
cout<<"insert success!"<<endl;
}
template <typename list_type>
void list<list_type>::del( int i )
{
if( count == 0 )
cout<<"underflow"<<endl;
else if ( i < 1 || i > count )
cout<<"unsuccess"<<endl;
else
{
for(int j = i ; j <= count-1 ; j++ )
data[ j-1 ] = data [ j ];
count--;
cout<<"success"<<endl;
}
}
template <typename list_type>
void list<list_type>::print()
{
for(int i = 0 ; i < count ; i++ )
cout<<data[i]<<setw(5);
cout<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -