线性表操作



#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>

typedef int ElemType;//ElemType定义定义为int类型
typedef struct
{
 ElemType *elem;
 int listsize;
 int length;
}SqList;

//初始化线性表
bool InitList_sq(SqList &L);
//销毁线性表
void DestroyList_sq(SqList &L);
//清空线性表
void ClearList_sq(SqList &L);
//判断线性表是否为空
bool ListEmpty_sq(SqList L);
//判断线性表是否已满
bool ListFull_sq(SqList L);
//求线性表长度
int ListLength_sq(SqList L);
//查找元素
int LocateItem_sq(SqList L,ElemType e);
//获取元素
bool GetItem_sq(SqList L,int i,ElemType &e);
//插入元素
bool InsertList_sq(SqList &L,int i,ElemType e);
//删除元素
bool ListDelete_sq(SqList &L,int i,ElemType &e);
//遍历元素
void ListTraverse_sq(SqList L);
//扩展线性表
bool Increment(SqList &L);


//初始化线性表
bool InitList_sq(SqList &L)
{
 L.elem=new ElemType[100];
 if(!L.elem){cerr<<"分配内存错误!"<<endl;return false;}
 L.listsize=100;
 L.length=0;
 return true;
}


//销毁线性表
void DestroyList_sq(SqList &L)
{
 delete [] L.elem;
 L.listsize=0;
 L.length=0;
}

//清空线性表
void ClearList_sq(SqList &L)
{
 L.length=0;
}

//判断线性表是否为空
bool ListEmpty_sq(SqList L)
{
 return(L.length==0);
}

//判断线性表是否已满
bool ListFull_sq(SqList L)
{
 return(L.length==L.listsize);
}

//求线性表长度
int ListLength_sq(SqList L)
{
 return L.length;
}

//查找元素
int LocateItem_sq(SqList L,ElemType e)
{
 int i;
 for(i=0;i<L.length;i++)
  if(L.elem[i]==e)
   return i+1;
  return 0;
}

//获取元素
bool GetItem_sq(SqList L,int i,ElemType &e)
{
 if(i<1||i>L.length)
 {
  cout<<"i值非法!"<<endl;
  return false;
 }
 e=L.elem[i-1];
 return true;
}

//插入元素
bool InsertList_sq(SqList &L,int i,ElemType e)
{
 int j;
 if(i<0||i>L.length+1)
 {
  cout<<"i值非法!"<<endl;
  return false;
 }
 if(ListFull_sq(L))
  if(Increment(L))
  {
   cout<<"扩展失败!"<<endl;
   return false;
  }

 for(j=L.length-1;j>i-1;j--)
  L.elem[j+1]=L.elem[j];
 L.elem[i-1]=e;
 ++L.length;
 return true;
 
}

//删除元素
bool ListDelete_sq(SqList &L,int i,ElemType &e)
{
 int j;
 if(i<0||i>L.length)
 {
  cout<<"i值非法!"<<endl;
  return false;
 }
 if(ListEmpty_sq(L))
 {
  cout<<"表为空!"<<endl;
  return false;
 }
 e=L.elem[i-1];
 for(j=i;j<L.length-1;j++)
  L.elem[j-1]=L.elem[j];
 --L.length;
 return true;
}

//遍历元素
void ListTraverse_sq(SqList L)
{
 int i;
 for(i=0;i<L.length;i++)
  cout<<L.elem[i]<<" ";
 cout<<endl;
}

//扩展线性表
bool Increment(SqList &L)
{
 //增加线性表L的容量为listsize+inc_size
 int i;
 ElemType *a;
 a=new ElemType[L.listsize+20];//为a指针动态分配内存
 if(!a){cout<<"分配内存错误!"<<endl;return false;}
 for(i=0;i<L.length;i++)
  a[i]=L.elem[i];
 delete []L.elem;
 L.elem=a;
 L.listsize+=20;
 return true;
}

 

//主程序
int main()
{
 SqList L;
 ElemType e;
 int i;
 cout<<"1.初始化列表L"<<endl;
 InitList_sq(L);
 cout<<"2.从键盘输入5个整型元素并插入到线性表L"<<endl;
 for(i=1;i<=5;i++)
 {
  cin>>e;
  InsertList_sq(L,i,e);
 }
 cout<<"3.输出线性表L"<<endl;
 ListTraverse_sq(L);
 cout<<"4.顺序表长度:"<<ListLength_sq(L)<<endl;
 cout<<"5.顺序表L"<<(ListEmpty_sq(L)?"空":"非空")<<endl;
 cout<<"6.请输入顺序表中待查元素位序:";
 cin>>i;
 if(GetItem_sq(L,i,e))
  cout<<"顺序表的"<<i<<"个元素是:"<<e<<endl;
 else cout<<"输入数据非法!"<<endl;


 cout<<"7.请输入线性表的待查元素:";
 cin>>e;
 if((i=LocateItem_sq(L,e)))
  cout<<"元素"<<e<<"在顺序表中的位序是"<<i<<endl;
 else cout<<"元素"<<e<<"不在顺序表中"<<endl;

 cout<<"8.输入要插入顺序表的位置和元素:"<<endl;
 cin>>i>>e;
 cout<<(InsertList_sq(L,i,e)?"插入成功!":"插入失败!")<<endl;
 cout<<"插入后输入线性表L:";
 ListTraverse_sq(L);

 cout<<"9.输入要删除的元素位置:";
 cin>>i;
 cout<<(ListDelete_sq(L,i,e)?"删除成功!":"删除失败!")<<endl;
 cout<<"删除后输出线性表L:";
 ListTraverse_sq(L);

 cout<<"10.销毁线性表L."<<endl;
 DestroyList_sq(L);
}
运行图:

你可能感兴趣的:(线性表操作)