zoukankan      html  css  js  c++  java
  • 顺序表类的定义

    ///////////////////////////////////
    //日期:2014-09-30
    //作者:Davis
    //功能:顺序表类的实现 // #include "SqList.h" ////////////////////////////////// template<class T> class SqList { private: T* elem; //表首址 int length; //表长 int listsize; //表容量 public: //构造函数,创建容量为m的空表 SqList(int m) { elem = new T[m]; //申请表空间 length = 0; //空表,表长为0 listsize = m; //表容量为m } //析构函数,删除表空间 ~SqList() { delete [] elem; //释放表空间 length = 0; listsize = 0; } //创建具有n个元素的线性表 void CreateList(int n) { if(n>listsize) throw"参数非法"; cout<<"请依次输入"<<n<<"个元素值:"<<endl; for(int i =1;i<=n;i++) cin>>elem[i-i]; length = n; } //在表中第i个位置插入元素 void Insert(int i,T e) { //在第i个位置插入元素,如果不能插入,显示异常信息 if(length>=listsize)throw"上溢"; if(i<1||i>length+1)throw"插入位置异常"; for(int j = length;j>=i;j--) elem[j] = elem[j-1]; elem[i-1] = e; length++; } //删除表中的第i个元素 T Delete(int i) { T x; if(length == 0)throw"下溢"; if(i<1||i>length+1)throw"删除位置异常"; x=elem[i-1]; for(int j=i;j<length;j++) elem[j-1] = elem[j]; length--; return x; } //获取第i个元素的值 T GetElem(int i) { T e; if(i<1||i>length)throw"位置不合法"; e = elem[i-1]; return e; } //元素定位 int Locate(T e) { for(int i = 0;i<length;i++) if(elemi[i] == e) return i+1; //找到,返回该元素在表中的位序 return 0; //未找到,返回0 } //清空表 void Clear() { length =0; } //测表是否空 int Empty() { if(length == 0)//判断表,若空,返回1. return 1; else //不空,返回0 return 0; } //测表是否满 int Full() { if(length == listsize) //表满,返回1 return 1; else //不满,返回0 return 0; } //返回表长 int Length() { return length; } //输出表元素 void ListDisp() { for(int i = 0;i<length;i++) { cout<<i+1<<" "; cout<<elem[i]<<endl; } } }
     
    /////////////////////////////////////////////////
    //          顺序链表的测试
    /////////////////////////////////////////////////
    // SqList_main.cpp : 定义控制台应用程序的入口点。
    //
    #include "stdafx.h"
    
    #include "SqList.h"
    #include "process.h"
    #include<iostream>
    
    using namespace std;
    
    
    char pause;
    typedef int T;  //T表示int类型
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int i;
        T e;
        SqList<int>L(20); //建立容量为20、元素类型为整形的空顺序表
        system("cls");    //执行系统命令,清屏
        int choice;
        do
        {
          cout<<"1-创建顺序表
    ";
          cout<<"2-在链表第i个位置插入元素
    ";
          cout<<"3-删除顺序表中第i个位置的元素
    ";
          cout<<"4-返回第i个元素的值
    ";
          cout<<"5-元素定位
    ";
          cout<<"6-清空表
    ";
          cout<<"7-测表空
    ";
          cout<<"8-测表满
    ";
          cout<<"9-测表长
    ";
          cout<<"10-显示表长
    ";
          cout<<"11-退出
    ";
          cout<<"Enter choice:";
          cin>>choice;
          switch(choice)
          {
          case 1://创建表
                 cout<<"请输入要创建的顺序表中元素的个数:";
                 cin>>i;
                 cout<<endl;
                 try
                 {
                   L.CreateList(i);
                 }
                 catch(char* err)
                 {
                  cout<<err<<endl;
                 }
                 break;
          case 2://元素插入
               cout<<"请输入插入的位置";
               cin>>i;
               cout<<endl;
               cout<<"请输入插入元素的值:";
               cin>>e;
               cout<<endl;
               try
               {
                   L.Insert(i,e);
               }
               catch(char* err)
               {
                 cout<<err<<endl;
               }
               break;
          case 3://元素删除
              cout<<"请输入删除位置";
              cin>>i;
              cout<<endl;
              try
              {
                  e = L.Delete(i);
                  cout<<"被删除元素为:"<<e<<endl;
              }
              catch(char* err)
              {
                 cout<<err<<endl;
              }
              cin.get(pause);
              system("pause");
              break;
          case 4://返回第i个元素值
              cout<<"请输入要查询的元素位置:";
              cin>>i;
              try
              {
                  e = L.GetElem(i);
                  cout<<""<<i<<"个元素值为:"<<e<<endl;
              }
              catch(char* err)
              {
                cout<<err<<endl;
              }
              cin.get(pause);
              system("pause");
              break;
          case 5://按值进行元素查询
              cout<<"请输入要查询的元素值:";
              cin>>e;
              i = L.Locate(e);
              cout<<"查询元素"<<e<<"在表中的位置为:"<<i<<endl;
              cin.get(pause);
              system("pause");
              break;
          case  6://清空表
              L.Clear();
              cout<<"表以清空"<<endl;
              cin.get(pause);
              system("pause");
              break;
          case 7://测表空
              i = L.Empty();
              if(i)
              {
                cout<<"表空"<<endl;
              }
              else
              {
                cout<<"表不空"<<endl;
              }
              cin.get(pause);
              system("pause");
              break;
          case 8://测表满
              i = L.Full();
              if(i)
              {
                cout<<"表满"<<endl;
              }
              else
              {
                cout<<"表未满"<<endl;
              }
              cin.get(pause);
              system("pause");
              break;
          case 9://测表长
              i = L.Length();
              cout<<"表长为:"<<endl;
              cin.get(pause);
              system("pause");
              break;
          case 10://遍历输出表
              L.ListDisp();
              cout<<endl;
              cin.get(pause);
              system("pause");
              break;
          case 11://退出
              break;
          default://非法选择
              cout<<"Invalid choice";
              break;
          }       
    
        }while(choice != 11);
        return 0;
    }// end of main function
  • 相关阅读:
    【More Effective C++】Item 4
    【More Effective C++】Item 3
    【More Effective C++】Item 2
    【More Effective C++】Item 1
    ”win7笔记本共享无线网络,手机连接成功却无法上网“的解决之道【亲身经历】
    【RFID防碰撞协议/算法】动态二进制搜索算法
    【RFID防碰撞协议/算法】二进制搜索防碰撞算法
    总结ASP标准控件
    总结ASP控件属性
    ..........
  • 原文地址:https://www.cnblogs.com/Davis812/p/4002145.html
Copyright © 2011-2022 走看看