数据结构1_顺序表程序实现

 
由于线性表中每个数据元素的类型相同,可以用CC++语言中的一维数组来实现顺序表。
# include <iostream >
using  namespace std;
# define LIST_INIT_SIZE  50
class SqList
{
int  *elem; //存′?储′¢空?间?基ù址·
int length; //当μ±前°长3¤度è
int listsize; //当μ±前°分·?配?的μ?存′?储′¢容èY量á?
public :
    SqList( int leng);
     int LocateElem( int e); //定¨位?
     void ListInsert( int i, int e); //插2?入è?
     void ListDelete( int i); //删é?除3y
     void show(); //显?示ê?顺3序Dò表±í元a素?
     ~SqList()
    {
         delete elem;
        cout << "destruct called" <<endl;
    }
};
SqList : :SqList( int leng)
{
    elem = new  int[LIST_INIT_SIZE];
     if( !elem)cout << "overflow" <<endl;
     if(leng >LIST_INIT_SIZE)
        cout << "error";
     else
    {    length =leng;
         int i = 0;
         int  *p =elem;
         for (i;i <leng;i ++)
        {
            cout << "input" <<i + 1 << "num:";
            cin >> *(p +i);
        }
    }
    listsize =LIST_INIT_SIZE;
    cout << "the sqlist is constructed" <<endl;
    show();
}
int SqList : :LocateElem( int e)
{
     int i = 1;
     int  *p =elem;
     while(i < =length &&( *p ++ !=e))
         ++i;
     if(i < =length)  return i;
     else  return  0;
}
void SqList : :show()
{
     int i = 1;
     int  *p =elem;
     for(i;i < =length;i ++)
    {
        cout << "the " <<i << "th content is" << *(p +i - 1) <<endl;
    }
}
void SqList : :ListInsert( int i, int e)
{
     int  *q = &elem[i - 1]; //指?示ê?插2?入è?位?置?
     int  *p;
     for(p = &elem[length - 1];p > =q; --p) //插2?入è?位?置?及°之?后oó的μ?元a素?右óò移ò?
         *(p + 1) = *p;
     *q =e; //插2?入è?e
     ++length;
    cout << "insertthe" <<i << " successful" <<endl;
        show();
    cout << "the lengtn of SqList is" <<length <<endl;
}
void SqList : :ListDelete( int i)
{
     if((i < 1) ||(i >length)) cout << "delete error";
     int  *p = &elem[i - 1];
     int e = *p;
     int  *q =elem +length - 1;
     for( ++p;p < =q; ++p)
         *(p - 1) = *p;
     --length;
    cout << "delete " <<i << " the " <<e <<  "successful" <<endl;
    show();
}
int main()
{
    SqList a( 10);
     int i =a.LocateElem( 7);
     if(i)
    cout << "locate is " <<i <<endl;
    a.ListInsert( 5, 10);
    a.ListDelete( 3);
    a. ~SqList();
     return  0;
}
 
 





转载于:https://www.cnblogs.com/zhuangwy-cv/p/3729815.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值