C++实现顺序表基本操作

此代码按照上篇博客线线性表基础概念,顺序表实现将概念用代码实现

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

using namespace std;

//函数结果状态代码
#define TRUE   1
#define FALSE  0
#define OK     1
#define ERROR  0
#define INFEASIBLE -1
#define OVERFLOW   -2
//Status 是函数的类型,其值是函数结果状态代码

typedef int Status;    // Status 相当于 int 
typedef char ElemType; //Elemtype 相当于 char 

#define  MAXSIZE  100       //线性表存储空间的初始分配量
typedef  struct
{
  ElemType *elem;           //存储空间的基地址
  int length;               //当前长度
}SqList;                    //顺序表类型名称为Sqlist 

Status InitList(SqList &L);   //初始化操作,建立一个空的线性表L 

void DestroyList(SqList &L);  //销毁线性表L 

void ClearList(SqList &L);    //清空线性表L 

int ListEmpty(SqList L);      //判断线性表L是否为空表 

int ListLength(SqList L);     //返回线性表L中的数据元素个数
  
Status GetElem(SqList L,int i,ElemType &e);       //用e返回线性表L中第i个元素 

Status LocateElem(SqList L,ElemType e);    //返回L中第1个值与e相同的元素在L中的位置。若这样的元素不存在,则返回值为0

Status ListInsert(SqList &L,int i,ElemType e);    //在线性表L的第i个位置插入元素e 

Status ListDelete(SqList &L,int i,ElemType &e);   //删除线性表L中第i个元素并用e返回 

void ListTraversals(SqList L);  //对线性表L进行遍历,在遍历过程中对L的每个结点访问一次


int main(){
	
	SqList L;   //创建线性表L 
	 
	if(InitList(L))
		cout << "线性表L创建成功"  << endl;
	
	ListInsert(L,1,'s');  //在线性表第一个位置里放入元素
	if(ListInsert(L,1,'s'))
		cout << "插入成功"  << endl;
	else
		cout << "插入失败"  << endl;
			 
	ListInsert(L,2,'m');
	if(ListInsert(L,2,'m'))
		cout << "插入成功"  << endl;
	else
		cout << "插入失败"  << endl;	
		
	ListInsert(L,3,'h');
	if(ListInsert(L,3,'h'))
		cout << "插入成功"  << endl;
	else
		cout << "插入失败"  << endl; 
	
	cout << "遍历线性表:";
	void ListTraversals(SqList L);
	
	cout << "线性表长度:" << ListLength(L) << endl; 
	
	ElemType e; 
	ListDelete(L, 1, e);
	cout << "被删除的元素:" << e << endl;
	cout << "被删除后线性表的长度:" << ListLength(L) << endl;
	
	ElemType a='m';
	int aa=LocateElem(L,a);
	cout << "该元素"  << a << "在表中序号为:" << aa << endl;
	
	GetElem(L,1,e);
	cout << "线性表L第一个元素:" << e << endl;
	
	cout << "清空线性表" << endl; 
	ClearList(L);
	
	
	ListEmpty(L);
	if(ListEmpty(L))
		cout << "线性表为空" << endl;
	else
		cout << "线性表非空" << endl;
			 
	DestroyList(L);
	cout << "线性表已销毁" << endl; 
	
	
	return 0;
} 



Status  InitList(SqList &L)       //构造一个空的顺序表L
{
   L.elem = new ElemType[MAXSIZE]; //为顺序表分配空间
   if(!L.elem) exit(OVERFLOW) ;   //存储分配失败
   L.length=0;                    //空表长度为0
   return OK;
 }

void DestroyList(SqList &L){
	if(L.elem)delete L.elem;   //释放存储空间
}	

void ClearList(SqList &L){
	L.length=0;                //将线性表的长度设置为0
}	

int ListEmpty(SqList L){
	if (L.length == 0) return 1;
	else return 0;
}

int ListLength(SqList L){
	return (L.length);       //调用SqList结构里length 
}

Status  GetElem(SqList L ,int i ,ElemType &e)
{
     if(i < 1 || i > L.length)  return ERROR;  //判断i值是否合理
     e=L.elem[i-1];                        //elem[i-1]存储第i个数据元素
     return OK;
 }

Status  LocateElem(SqList L,ElemType e)     //在线性表L中查找值为e的数据元素,返回其序号(是第几个元素)
{
    for(int i=0 ;i<L.length ;i++)
       if(L.elem[i] == e)   return i+1;  //查找成功,返回序号i+1
    return 0;                            //查找失败,返回0
 }

Status  ListInsert(SqList  &L,int i,ElemType e)
{
      if( (i<1)||(i>L.length+1) )   return ERROR;
      if(L.length == MAXSIZE)       return ERROR;
      for(int j=L.length-1 ;j>=i-1 ;j--){ 
    		L.elem[j+1] = L.elem[j];        //插入位置及之后的元素后移
      } 
	  L.elem[i-1] = e;                  //将新元素 e 放入第 i 个位置
      ++L.length;                       //表长加1
      return OK;
 }

Status  ListDelete(SqList  &L,int  i, ElemType &e)
{
     if( (i<1) || (i>L.length) )       return ERROR;
     e = L.elem[i-1];
     for(int j=i ;j<=L.length-1 ;j++)
        L.elem[j-1]=L.elem[j];        //被删除元素之后的元素前移
     --L.length;                      //表长减1
     return OK;
 }

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

发布了5 篇原创文章 · 获赞 29 · 访问量 4095

猜你喜欢

转载自blog.csdn.net/diviner_s/article/details/103992586
今日推荐