一个 C++ 中动态的插入、删除数据的例子.

/**
 * C++ 中动态的插入、删除数据.
 *
 * 雲飛揚
 * 2008.4.14
 
*/


#include 
< iostream >
using namespace std;
/*///*/
const   int  maxSize  =   1024 ;
const   int  listMax  =   20 ;
/*///*/
class  DateOperation  //  声明类
{
public:
    typedef struct
    
{
    
public:
        
int data[maxSize];
        
int  length;
    }
dateBase;

    bool  insert(dateBase 
*value, int member, int locat);
    bool  del(dateBase 
*value, int locat);
    dateBase 
*createList();
    
void input(dateBase value);
}
;
/*///*/
bool DateOperation::insert(dateBase 
* value,  int  member,  int  locat)
{
    
if(value->length == maxSize - 1)
    
{
        
return false;
    }


    
if ((locat < 1|| (locat > value->length + 1))
    
{
        
return false;
    }

    
    
int tmp = value->data[locat - 1];

    
for(int j = value->length - 1;j >= locat;--j)
    
{
        value
->data[j + 1= value->data[j];
    }


    value
->data[locat] = tmp;
    value
->data[locat - 1= member;
    value
->length = value->length + 1;

    
return true;
}


bool DateOperation::del(dateBase 
* value,  int  locat)
{
    
if ((locat < 1|| (locat > value->length))
    
{
        
return false;
    }

    
else
    

        
for(int j = locat;j <= value->length - 1;++j)
        
{
            value
->data[j - 1= value->data[j];
        }


        value
->length--;
    }


    
return true;
}


DateOperation::dateBase 
* DateOperation::createList()
{
    
int n,locat;
    
    dateBase 
*value = new dateBase;

    cout 
<<"请输入元素个数:" << endl;
    cin.clear();
    cin 
>> n;
    
if(n <= 0 || n > listMax)
    
{
        cout 
<< "ERROR, 输入的元素个数不正确, 请在 1 - " << listMax << " 之间. ";

        createList();
    }


    
for(locat = 0;locat < n;++locat)
    
{
        cout 
<< "data[" << locat + 1<< "]=" << endl;
        cin 
>> (value->data[locat]);
    }


    value
->length = n;
    cout 
<< " ";
    
return value;
}


void  DateOperation::input(dateBase value)
{
    
for(int index = 0;index != value.length;++index)
    
{
        cout 
<< "data[" << index + 1 << "] = " << value.data[index] << endl;
    }

}

/*///*/
int  main( void )
{
    DateOperation Operation;

    DateOperation::dateBase 
*value;
    
int s = -1;
    
int locat, member;
    value 
= Operation.createList();

    Operation.input(
*value);

    
while(0 != s)
    
{
        cout 
<< "1......插入元素 2......删除元素 0......退出 ";
        cin.clear();
        cin 
>> s;
        
if(s < 0 || s > 2)
        
{
            cout 
<< "ERROR, 输入的选项不正确! ";
            
continue;
        }

        
switch (s)
        
{
            
case 1:
                cout 
<< "请输入插入的元素: ";
                member 
= -1;
                cin.clear();
                cin 
>> member;
                
if(member < 0)
                
{
                    cout 
<< "ERROR, 输入的元素不合法! ";
                    
continue;
                }

                
                cout 
<< "请输入插入位置: ";
                locat 
= -1;
                cin 
>> locat;
                
if(locat < 0)
                
{
                    cout 
<< "ERROR, 输入的位置不合法! ";
                    
continue;
                }


                
if(false == Operation.insert(value, member, locat))
                
{
                    cout 
<< "插入元素失败! ";
                    
continue;
                }


                Operation.input(
*value);
                
break;
            
case 2:
                cout 
<< "请输入删除元素的位置: ";
                locat 
= -1;
                cin.clear();
                cin 
>> locat;
                
if(locat < 0)
                
{
                    cout 
<< "ERROR, 输入的删除位置不合法! ";
                    
continue;
                }

                
if(false == Operation.del(value, locat))
                
{
                    cout 
<< "删除元素失败! ";

                    
continue;
                }


                Operation.input(
*value);
                
break;

            
default:
                ;
        }

    }


    delete value;

    
return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值