顺序表

#include "sq_list.h"    // 顺序表类
#define DEFAULT_SIZE 3
#include <iostream>
#include <cctype> 
using namespace std;
template<class ElemType>
SqList<ElemType>::SqList(int size){    //操作结果:构造一个最大元素个数为size的空顺序表 
    maxSize = size;
    elems = new ElemType[maxSize] ;
    count = 0;
}

template <class ElemType>
bool SqList<ElemType>:: CreateList(int num)
  //创建长度为num的顺序表
{   if (num>maxSize) return false; //参数非法
  cout<<"建立长度为"<<num<<"的顺序表
";
  cout<<"请依次输入"<<num<<"个元素值:
";
    for(int i=0;i<num;i++)
          cin>>elems[i];
     count=num;
    return true;
  } 


template <class ElemType>
int SqList<ElemType>::Length(){
    cout << count;
    return count;
}

template <class ElemType>
bool SqList<ElemType>::Empty(){
    if(count==0){
        cout << "is empty
";
        return true;
    }else{
        cout << "not empty
";
        return false;
    }
}

template <class ElemType>
bool SqList<ElemType>::Full(){
    if(count == maxSize){
        cout << "is full
";
        return true;
    }else{
        cout << "not full
";
        return false;
    }
}

template <class ElemType>
//bool SetElem(int position, ElemType e);// 设置指定位置的元素值
bool SqList<ElemType>::SetElem(int position, int tag){
    ElemType e;
    int insert = 1;
    int deleta = 2;
    //int position;
    if(tag ==0){            //direct use
        cout << "please input the position of the element you want to change:";
        cin >> position;
        cout << "please input the value of the element you want to change:";
        cin >> e;    
        if(position < 1 || position > Length()){
        cout << "wrong size
set operation failed.";
        return false;
        }else{
        elems[position-1] = e;
        return true;
        }
    }else if(tag == insert){                        // insert
        if(position < 1 || position > Length()+1){
            cout << "wrong size
set operation failed.";
            return false;
        }else{
            //elems[position-1] = e;
            //e = elems[position-1];
            elems[position-1] = elems[position-2];
            return true;
        }
    }else{
        if(position < 1 || position > Length()+1){
            cout << "wrong size
set operation failed.";
            return false;
        }else{
            //elems[position-1] = e;
            //e = elems[position-1];
            elems[position-1] = elems[position];
            return true;
        }
    }
}

//bool GetElem(int position, ElemType &e) ;    // 求指定位置的元素
template <class ElemType>
bool SqList<ElemType>::GetElem(){
    int position;
    cout << "input the position of the element:";
    cin >> position;
    if(position < 1 || position > Length()){
        cout << "area error
";
        return false;
    }else{
        //e = elems[position-1];
        cout << "the element is: " << elems[position-1];
        return true;
    }
}

template <class ElemType>
ElemType SqList<ElemType>::GetElemValue(int position){
    if(position < 0 || position > Length()-1){
        cout << "area error
";
        return elems[0];
    }else{
        return elems[position];
    }
}

template <class ElemType>
bool SqList<ElemType>:: Locate (){
 //从顺序表中查找具有给定值e的第一个元素,并返回该元素的逻辑位序,若找不到返回0。 
         ElemType e;
         cout << "input the value of an element: ";
         cin >> e;
         int i;
        for( i=0; i<count; i++)
       if(elems[i]==e){
               cout << "find it
";
            //return  i<count?i+1:0 ;            
       } 
       if(i < count){
           return true;
       }else{
           return false;
       }
       //return  i<count?true:false ;
}

//          bool Insert(int position,  ElemType e);// 插入元素
template <class ElemType>
bool SqList<ElemType>::Insert(){
    int position;
    ElemType e;
    ElemType tmp;
    cout << "input the position of the element:";
    cin >> position;
    if(Full()){
        return false;
    }else if(position < 1 || position > Length()+1){
        cout << "overflower.";
        return false;
    }else{
        ++count;
        cout << "input the value of the element: ";
        cin >> e;
        for(int pos = Length(); pos >= position; --pos){
            //GetElem(pos);
            SetElem(pos+1, 1);
        }
        return true;
    }
}

template <class ElemType>
bool SqList<ElemType>::Delete(int position){
    // position范围错
    cout << "input the position of the element: ";
    cin >> position;
    if (position < 1 || position > count){     
        return false;    
    } else{       // 用e返回被删除元素的值
       ElemType e=elems[position-1];
       // 被删除元素之后的元素依次左移
       for (int curPosition = position ; curPosition < count; curPosition++)
       {     elems[curPosition-1]=elems[curPosition];  } 
       count--;      // 删除后元素个数将自减1
       return true; 
    }
}

template <class ElemType>
bool SqList<ElemType>::DeleteOne(int position){
    if (position < 1 || position > count){     
        return false;    
    }else{
        ElemType e=elems[position-1];
       // 被删除元素之后的元素依次左移
       for (int curPosition = position ; curPosition < count; curPosition++)
       {     elems[curPosition-1]=elems[curPosition];  } 
       count--;      // 删除后元素个数将自减1
       return true; 
    }
}

template <class ElemType>
void SqList<ElemType>::Traverse(){
    for (int curPosition = 0; curPosition <= count-1; 
                                            curPosition++)
    {    
        cout<<elems[curPosition ]<<'	';
    } 
    cout<<endl;

}

template <class ElemType>
void SqList<ElemType>:: Clear(){
    count = 0;
}

template <class ElemType>
SqList<ElemType>::~SqList(){
    delete[]elems;
}

template <class ElemType>
bool SqList<ElemType>::Gather(){        //定义集合操作函数 
    cout << "input two gathers
" << "gather #1:
";
    SqList<char> gath1;
    gath1.CreateList(DEFAULT_SIZE);
    cout << "gather #2:
";
    SqList<char> gath2;
    gath2.CreateList(DEFAULT_SIZE);
    int tago;
    int tagi;
    int tags = 0;
    int tagr = 0;
    cout << "their intersection:
";
    for (tago = 0; tago < DEFAULT_SIZE; ++tago){
        for (tagi = 0; tagi < DEFAULT_SIZE; ++tagi){
            if(gath1.GetElemValue(tago) == gath2.GetElemValue(tagi)){
                cout << gath1.GetElemValue(tago);
            }
        }
        
    }
    cout << "
their sum:
";
    for(tago = 0; tago < DEFAULT_SIZE; ++tago){
        for(tagi = 0; tagi < DEFAULT_SIZE; ++tagi){
            if(((tago != tagi) && (gath1.GetElemValue(tago) == gath1.GetElemValue(tagi))) ){
                tags = 1;
                //break;
            }
            if((tago != tagi) && (gath2.GetElemValue(tago) == gath2.GetElemValue(tagi)) || gath2.GetElemValue(tago) == gath1.GetElemValue(tagi) ){
                tagr = 1;
            }
        }
        
        if(tags == 0){
            cout << gath1.GetElemValue(tago);
            
        }
        if(tagr == 0){
            cout << gath2.GetElemValue(tago);
            
        }
        tags = 0;
        tagr = 0;
    }
    cout << "
#1 substract #2:
";
    for(tago = 0; tago < gath1.Length(); ++tago){
        for(tagi = 0; tagi < DEFAULT_SIZE; ++tagi){
            if(gath1.GetElemValue(tago) == gath2.GetElemValue(tagi)){
                gath1.DeleteOne(tago+1);
                --tago;
            }
        }
    }
    for(tago = 0; tago < gath1.Length(); ++tago){
        cout << gath1.GetElemValue(tago);
    }
    return true;
}

template <class ElemType>
bool SqList<ElemType>:: CreateListOrder(int num){
    if (num>maxSize) return false; //参数非法
    cout<<"建立长度为"<<num<<"的有序顺序表
";
    cout<<"请依次按大小输入"<<num<<"个元素值:
";
   
    for(int i=0;i<num;i++)
        cin>>elems[i];
    ElemType temp;
    for(int wawa = 0; wawa < num-1; ++wawa)
    {
        for(int gaga = wawa+1; gaga < num; ++gaga)
        {
            if(elems[wawa] > elems[gaga])
            {
                temp = elems[wawa];
                elems[wawa] = elems[gaga];
                elems[gaga] = temp;
            }
        }
    } 
    count=num;
    return true;
} 

template <class ElemType>
bool SqList<ElemType>::InsertOrder(){
    // 线性表已满
     if (Full()){    return false;    }
    else
{    // 插入位置之后的元素右移
    ElemType e;
    cout << "insert an element with order:";
    cin >> e;
    int position = 1;
    for(;position<count; position++){
        if(e > elems[position-1]) break;
    }
    
    
    
      for (int curPosition = count-1; curPosition >= position-1;  curPosition--){  elems[curPosition+1]= elems[curPosition];}
      elems[position-1]=e;       //将e赋值到position位置处
       count++;               // 插入后元素个数将自增1
        return true;             // 插入成功
  }
}

template <class ElemType>
bool SqList<ElemType>::WordEdit(int LineNum, int WordNum){
    SqList<char> sc[LineNum];
    
    for(int word_line = 0; word_line < LineNum; ++word_line){
        sc[word_line].CreateList(DEFAULT_SIZE);
    }
    for(int output_line = 0; output_line < LineNum; ++output_line){
        cout << "
";
        sc[output_line].Traverse();
    }
    
    /**出现问题 
    cout << "
文章总字数:";
    int Cmp_Length = 0;
    for(int line_tag = 0; line_tag < LineNum; ++line_tag){
        Cmp_Length += sc[line_tag].Length();
        cout << Cmp_Length << endl;
    } 
    cout << Cmp_Length << endl;
    **/
    int Alpha_Num = 0, Space_Num = 0, Number_Num = 0, Total_Num = 0;
    for(int test_tag = 0; test_tag < LineNum; ++test_tag){
        for(int word_tag = 0; word_tag < WordNum; ++word_tag){
            if(isalpha(sc[test_tag].elems[word_tag]))    Alpha_Num++;
            if(isblank(sc[test_tag].elems[word_tag]))     Space_Num++;
            if(isdigit(sc[test_tag].elems[word_tag]))    Number_Num++;
            Total_Num++;
        }
    }
    cout << "英文字符个数:" << Alpha_Num << endl
         << "空格数:" << Space_Num << endl
         << "数字数:" << Number_Num << endl
         << "总数:" << Total_Num << endl;
    return false;
} 



int main(void){
    SqList<int> sl;
    SqList<char> sr;
    
    int num = DEFAULT_SIZE;
    int c = 0;
    int position;
    int tag = 0;
    while (c != 16){
        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 << "9. 在线性表中删除元素";
        cout << endl << "10. 遍历线性表";
        cout << endl << "11. 清空线性表";
        cout << endl << "12. 销毁线性表";
        cout << endl << "13. 集合运算"; 
        cout << endl << "14. 创建有序顺序表";
        cout << endl << "15. 文章编辑";
        cout << endl << "16. 退出";
        cout << endl << "选择功能(1~16):";
        cin >> c;
        switch (c){
            case 1:sl.CreateList(num);    break;
            case 2:sl.Length();    break;
            case 3:sl.Empty(); break;
            case 4:sl.Full(); break;
            case 5:sl.SetElem(position, tag); break;            
            case 6:sl.GetElem(); break;         
            case 7:sl.Locate(); break;
            case 8:sl.Insert(); break;                //待测试
            case 9:sl.Delete(position); break;
            case 10:sl.Traverse(); break;
            case 11:sl.Clear(); break;
            case 12:sl.~SqList(); break;
            case 13:sr.Gather(); break;
            case 14:sl.CreateListOrder(num); break;
            case 15:sr.WordEdit(DEFAULT_SIZE, DEFAULT_SIZE);
        }
    }
    return 0;
}

main.cpp

#ifndef __SQ_LIST_H__
#define __SQ_LIST_H__
#define DEFAULT_SIZE 3

// 顺序表类模板
template <class ElemType>
class SqList 
{
    protected:
    // 顺序表实现的数据成员:
        int count;            // 元素个数
        int maxSize;        // 顺序表最大元素个数
        ElemType *elems;    // 元素存储空间
    public:
    // 顺序表的方法声明:
        SqList(int size = DEFAULT_SIZE);    // 初始化线性表
         virtual~SqList();   // 销毁线性表
         bool CreateList(int num); //构建顺序表
         void Clear();        // 将线性表清空
         int Length() ;    // 求线性表长度        
         bool Empty() ;    // 判断线性表是否为空
         bool Full() ;    // 判断线性表是否已满
        bool SetElem(int position, int tag);// 设置指定位置的元素值
        bool GetElem() ;    // 求指定位置的元素
        bool Locate (); //元素定位
          bool Insert();// 插入元素
          bool Delete(int position);// 删除元素    
        void Traverse() ;            // 遍历线性表
        ElemType GetElemValue(int position);
        bool Gather();
        bool DeleteOne(int position);
        bool CreateListOrder(int num);
        bool InsertOrder();
        bool WordEdit(int LineNum, int WordNum);    //文章编辑 
};
// 顺序表类模板的实现部分由学生自己完成





#endif

sq_list.h

原文地址:https://www.cnblogs.com/jzl123/p/6586564.html