SeqList<ElemType>::SeqList(const SeqList<ElemType> &sa)
{
    int saLength = sa.GetLength();
    ElemType e;
    
    maxLength = sa.maxLength;
    elems = new ElemType[maxLength];
    assert(elems);
    length = 0;

    for (int i = 1; i <= saLength; i++) {
        sa.GetElem(i, e);
        InsertTailElem(e);
    }
}
示例#2
0
int main(int argc, const char * argv[]) {

    char c='*';
    SeqList<int> *la = new SeqList<int>(6);
    int e = 0, i = 0;
    
    Status status;
    
    while (c != '0') {
        cout << endl << "1. 生成线性表.";
        cout << endl << "2. 显示线性表.";
        cout << endl << "3. 取指定元素.";
        cout << endl << "4. 设置元素值.";
        cout << endl << "5. 删除元素.";
        cout << endl << "6. 插入元素.";
        cout << endl << "7. 元素定位.";
        cout << endl << "8. 求线性表长度.";
        cout << endl << "0. 退出";
        cout << endl << "选择功能(0~8):";
        cin >> c;
        
        switch (c) {
            case '1':
            {
                la->Clear();
                status = SUCCESS;
                cout << endl << "输入e( e = 0时退出):";
                cin >> e;
                while (e != 0 && status != OVER_FLOW) {
                    status = la->InsertTailElem(e);
                    if (status == OVER_FLOW) {
                        cout << "线性表已满。" << endl;
                    }else {
                        cin >> e;
                    }
                }
            }
                break;
                
            case '2':
            {
                la->Traverse(Write<int>);
                //system("clear");
            }
                break;
                
            case '3':
            {
                cout << endl << "输入元素位置:";
                cin >> i;
                if (la->GetElem(i, e) == NOT_PRESENT) {
                    cout << "元素不存在" << endl;
                }else {
                    cout << "元素:" << e <<endl;
                }
                
            }
                break;
                
            case '4':
            {
               cout<< endl << "输入位置:";
                cin >> i;
                cout <<endl<<"输入元素值:";
                cin >> e;
                if (la->SetElem(i, e) == RANGE_ERROR) {
                    cout<<"位置范围错误"<<endl;
                }else {
                    cout<<"设置成功"<<endl;
                }
            }
                break;
                
            case '5':
            {
                cout << endl << "输入位置:";
                cin >> i;
                if (la->DeleteElem(i, e) == RANGE_ERROR) {
                    cout << "位置范围错." << endl;
                }else {
                    cout << "被删除元素值:" << e << endl;
                }
            }
                break;
                
            case '6':
            {
                cout << endl << "输入位置:";
                cin >> i;
                cout << endl << "输入元素值:";
                cin >> e;
                status = la->InsertElem(i, e);
                
                
                if (status == RANGE_ERROR) {
                    cout << "位置范围错." << endl;
                } else if (status == OVER_FLOW) {
                    cout << "线性表已满." << endl;
                }else {
                    cout << "插入成功." << endl;
                }
               
            }
                break;
                
            case '7':
            {
                cout << endl << "输入元素值:";
                cin >> e;
                
                i = la->LocateElem(e);
                
                if (i != 0)
                    cout << "元素" << e << "的序号为:" << i << endl;
                else
                    cout << "元素" << e << "不存在。"  << endl;
                break;

            }
                break;
                
            case '8':
            {
                cout << endl << "线性表的长度为:" << la->GetLength() << endl;
            }
                break;
                
                
            default:
                break;
        }

    }
    
    
    
    
    
    
    //system("PAUSE");        		// 调用库函数system(),暂停程序运行
   	return 0;               		// 返回操作系统

}