表(算法分析与设计笔记)

1.线性表定义及特点

对于非空的线性表而言,它具有如下4个特点:
 ①表中有且仅有一个开始节点
 ②表中有且仅有一个终端节点
 ③除了开始节点和终端节点外,其他每个元素前面均有且仅有一个称为直接前趋的数据元素,它的后面均有且仅有一个称为直接后继的数据元素
 ④虽然不同线性表的数据元素可以是各种各样的,但是同一线性表中的数据元素必须具有相同的数据类型

2.顺序表

(1)线性表的顺序储存结构描述:

typedef int ElemTlemype;//定义元素类型
class SqList
{
 private:
   ElemType*  elem;//声明元素指针
   int length;//元素个数
   int listSize;//表长度
 publicSqList();//表初始化
   bool isFull()const;//查看表是否为空
   bool isEmpty()const;//查看表是否满
   ElemType getElem(int)const;//查看特定位置元素
   bool insert(int,ElemType);//插入特定位置元素
   bool intsert(ElemType);//插入末尾元素
   void creat();//创建表
   void merge(SqList&,SqList&);//合并表并排序
   bool del(int);//删除表
   void displayElem();//显示表情况
   SqList();//析构
};

(2)空间分配
数组属于复合型数据类型,在使用它之前,必须为其分配空间

//参数initSize为用户所申请的空间大小
SqList :: SqList(){
elem=new ElemType[initSize];
length=0;
listSize=initSize;

(3)插入操作
顺序表的插入操作是指在具有size个元素的线性表的第i个元素前插入一个新的元素x,使线性表的长度增加1

bool SqList::insert(int i,ElemType e){
   if(i>length+1||i<1)
   {
     cout<<"i-value is illegal!";
     return false;
   }
   if(isFull())//表满重新分配内存
   {
    ElemType* newBase=new ElemType[listSize+increment];
    for(int i=0;i<=listSize;i++)
      newBase[i]=elem[i];
    delete[] elem;
    elem=newBase;
    listSize+=increment;
    }
    ElemType* p=&(elem[i-1]);
    ElemType* q=&(elem[length-1]);
    for(;q>=p;--q)
     *(q+1)=*q;
     *p=e;
     ++length;
     return true;
     }

(4)删除操作

bool SqList::del(int i)
{
  if(i>length||i<1)
  {
    cout<<"i-value is illegal!";
    return false;
  }
  ElemType* p=&(elem[i-1]);
  ElemType* q=&(elem[length-1]);
  for(++p;p<=q;++p);
  *(p-1)=*p;
  --length;
  return true;
  }  

一个完整的示例:

//头文件 sqlist.h
#include<iostream>
#ifndef SqList_H_
#define SqList_H_//定义头文件
#define initSize 100//初始化大小
#define increment 10//增长步长
typedef int ElemType;//定义元素类型
class SqList
{
private:
    ElemType* elem;//声明元素指针
    int length;//元素个数
    int listSize;//表长度
public:
    SqList();//初始化表
    bool isFull()const;//查看表是否为空
    bool isEmpty()const;//查看表是否满
    ElemType getElem(int)const;//查看特定位置元素
    bool insert(int, ElemType);//插入特定位置元素
    bool insert(ElemType);//插入末尾元素
    void creat();//创建表
    void merge(SqList&,SqList&);//合并表并排序
    bool del(int);//删除表
    void displayElem();//显示表情况
    ~SqList();//析构
};
#endif // SqList_H_

//重载函数
#include"seqList.h"
using namespace std;
//构造函数
SqList::SqList(){
   elem=new ElemType[initSize];
   length=0;
   listSize=initSize;
}
//是否为空
bool  SqList::isEmpty() const{
   return length==0;
}
//是否满了
bool SqList::isFull() const{
    return length==listSize;
}
ElemType SqList::getElem(int i) const{
     if (i>length||i<1)
     {
         cout<<"i-value is illegal!";
         return 0;
     }
     else return elem[i-1];
}
bool SqList::insert(int i,ElemType e){
 if (i>length+1||i<1)
 {
     cout<<"i-value is illegal!";
     return false;
 }
 if(isFull())//表满重新分配内存
 {
      ElemType* newBase=new ElemType[listSize+increment];
      for(int i=0;i<=listSize;i++)
        newBase[i]=elem[i];
      delete[] elem;
      elem=newBase;
      listSize+=increment;
 }
 ElemType* p=&(elem[i-1]);
 ElemType* q=&(elem[length-1]);
 for(;q>=p;--q)
    *(q+1)=*q;
 *p=e;
 ++length;
 return true;
}
bool SqList::insert(ElemType e){
if (isFull())//表满重新分配内存
{
    ElemType* newBase=new ElemType[listSize+increment];
    for(int i=0;i<=listSize;i++)
        newBase[i]=elem[i];
       delete[ ] elem;
       elem=newBase;
       listSize+=increment;
}
ElemType* p=&(elem[length]);
*p=e;
++length;
return true;

}
void SqList::creat(){
cout<<"为表添加元素(输入0退出!)"<<endl;
while(true)
{
    int elem;
    cin>>elem;
    if(elem!=0)
    {
        insert(elem);
    }
    else{
        cout<<"表创建完成,大小为"<<listSize<<"."<<endl;
        cout<<"表现有"<<length<<"个元素:"<<endl;
        for(int i=1;i<=length;i++)
            cout<<getElem(i)<<",";
        cout<<endl;
        break;
    }
}
}
void SqList::merge(SqList &La,SqList &Lb){
int i=1,j=1,k=0;
while(i<=La.length&&j<Lb.length)
{
    if(La.getElem(i)<=Lb.getElem(j))
    {
        insert(++k,La.getElem(i));
        ++i;
    }
    else
    {
        insert(++k,Lb.getElem(j));
        ++j;
    }
}
while(i<=La.length)
{

    insert(++k,La.getElem(i));
    ++i;
}
while(j<=Lb.length)
{

    insert(++k,Lb.getElem(j));
    ++j;
}
cout<<"合并后元素为:"<<endl;
  for(int m=1;m<=length;m++)
    cout<<getElem(m)<<endl;
}
bool SqList::del(int i){
if(i>length||i<1)
   {
   cout<<"i-value is illegal!";
   return false;
   }
   ElemType* p=&(elem[i-1]);
   ElemType* q=&(elem[length-1]);
   for(++p;p<=q;++p)
      *(p-1)=*p;
   --length;
   return true;
}
void SqList::displayElem(){
 cout<<"表的大小为"<<listSize<<"."<<endl;
 cout<<"表现有"<<length<<"个元素:"<<endl;
for(int i=1;i<=length;i++)
      cout<<getElem(i)<<",";
cout<<endl;
}
SqList::~SqList()//~表示析构函数
{

delete[ ] elem;
}

//主程序
#include"seqList.h"
#include <ctype.h>
using namespace std;
int main()
{

    cout<<"创建表A"<<endl;
    SqList sqa;
    sqa.creat( );
    cout<<"创建表B"<<endl;
    SqList sqb;
    sqb.creat();
    cout<<"合并表A,B的元素,并按从小到大的顺序放入表C"<<endl;
    SqList sqc;
    sqc.merge(sqa,sqb);
    cout<<"操作表C:"<<endl;
    cout<<"选择操作(Q退出):"<<endl;
    cout<<"1.查看表情况(A):"<<endl;
    cout<<"2.查看元素特定元素(C):"<<endl;
    cout<<"3.插入表尾元素(I):"<<endl;
    cout<<"4.插入特定位置元素(L):"<<endl;
    cout<<"5.删除元素(D):"<<endl;
    char str;
    cin>>str;
    while(toupper(str)!='Q')//toupper 是将小写字母转化为大写字母
    {

        switch(toupper(str))
        {
        case 'A':
             sqc.displayElem();
             break;
        case 'C':
             cout<<"请输入要查看元素位置:";
             int i;
             cin>>i;
             cout<<"表中第"<<i<<"个元素为"<<sqc.getElem(i)<<endl;
             break;
        case 'I':
            cout<<"请输入要插入的元素:";
            int elem;
            cin>>elem;
            sqc.insert(elem);
            sqc.displayElem();
            break;
        case 'L':
            cout<<"请输入插入元素位置:";
            int j;
            cin>>j;
            cout<<"请输入要插入的元素:";
            int jelem;
            cin>>jelem;
            sqc.insert(j,jelem);
          sqc.displayElem();
            break;
        case 'D':
            cout<<"请输入要删除元素位置:";
            int k;
            cin>>k;
            sqc.del(k);
       sqc.displayElem();
            break;
        default:
            cout<<"请输入正确选项!"<<endl;

        }
        cout<<"输入选项继续操作:";
        cin>>str;
    }
    cout<<"操作结束!"<<endl;
    cin.get();
    cin.get();
}

运行结果

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值