线性表

#include <iostream>
#include <cstdlib>
using namespace std;
//1.定义存储表示  ppt 22页
typedef int ElemType;               //定义ElemType类型为int
# define  LIST_INIT_SIZE   100  // 线性表存储空间的初始分配量
# define   LISTINCREMENT    10  // 线性表存储空间的分配增量

typedef struct                //若后面不再用,可省略结构名
{
    ElemType *elem;         //存储空间基址
    int    length;      //当前表长(特指元素个数)
    int   listsize;     //当前分配的存储容量(以sizeof(ElemType)为单位)
} SqList;
typedef int Status;
Status   InitList_Sq(SqList &L)
{
    //构造一个空的线性表L。
    L.elem = new ElemType[LIST_INIT_SIZE];
    L.length = 0;                    // 空表长度为0
    L.listsize = LIST_INIT_SIZE;  // 初始存储容量
    return   true;
}
//清空线性表
void ClearList(SqList &L)
{
    L.length=0;                //将线性表的长度置为0
}
//判断线性表是否为空
bool IsEmpty(SqList L)
{
    if (L.length==0)
        return true;
    else
        return false;
}
//求线性表长度
int GetLength(SqList L)
{
    return L.length;
}
//获取线性表指定位置元素
Status GetElem(SqList L,int i,ElemType &e)
{
    if (i<1||i>L.length)
        return -1;
    //判断i值是否合理,若不合理,返回ERROR
    e=L.elem[i-1];   //第i-1的单元存储第i个数据
    return true;
}
int PriorElem(SqList L,int i)
{
	 if (i<1||i>L.length)
        return -1;
	return L.elem[i-2];
}
int NextElem(SqList L,int i)
{
	 if (i<1||i>L.length)
        return -1;
	return L.elem[i];
}
Status ListInsert_Sq(SqList &L,int i,ElemType e)
{
    if(i<1 || i>L.length+1)
        return -1;	         //i值不合法
    if (L.length==LIST_INIT_SIZE)//当前存储空间已满,增加分配
    {
        ElemType* newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
        if (!newbase)
            exit(-2); //存储分配失败
        L.elem=newbase;  //新基址
        L.listsize+=LISTINCREMENT; //增加存储容量
    }
    ElemType* q=&(L.elem[i-1]); //q为插入位置
    for (ElemType* p=&L.elem[L.length-1]; p>=q; --p)
        *(p+1)=*p; 	//插入位置之后的元素右移
    *q=e; 		//插入e
    L.length++; 	//表长增1
    return true;
} //ListInsert_Sq
Status ListDelete_Sq(SqList &L,int i,ElemType &e)
{
    if (i<1||i>L.length)
        return -1;	//i值不合法
    ElemType* p=&(L.elem[i-1]); 	//p为被删除元素的位置
    e=*p;			//被删除元素的值赋给e
    ElemType* q=L.elem+L.length-1;	//表尾元素的位置
    for(++p; p<=q; ++p)
        *(p-1)=*p;		//被删除元素之后的元素左移
    --L.length;		
    return true;
}
void Display_List(SqList L)
{
    for(int i=1; i<=L.length;i++)
        cout<<L.elem[i-1]<<" ";
    cout<<endl;
}
//定义销毁线性表函数
void DestroyList(SqList &L)
{
    if (L.elem)
        delete[ ] L.elem;    //释放存储空间
    L.length=0;
    L.listsize=0;
}

void show_help()
{
    cout<<"1----清空线性表"<<endl;
    cout<<"2----判断线性表是否为空"<<endl;
    cout<<"3----求线性表长度"<<endl;
    cout<<"4----获取线性表指定位置元素"<<endl;
    cout<<"5----求前驱"<<endl;
    cout<<"6----求后继"<<endl;
    cout<<"7----在线性表指定位置插入元素"<<endl;
    cout<<"8----删除线性表指定位置元素"<<endl;
    cout<<"9----显示线性表"<<endl;
    cout<<"     退出,输出一个负数!"<<endl;
}
int main()
{
    int operate_code;
    show_help();
    SqList L;
    InitList_Sq(L);
    ElemType e;
    int i;
    while(1){
        cout<<"请输入操作代码:";
        cin>>operate_code;
        if(operate_code==1){
            ClearList(L);
        }
        else if(operate_code==2){
            if(IsEmpty(L))
                cout<<"The list is empty."<<endl;
            else
                cout<<"The list is not empty."<<endl;
        }
        else if(operate_code==3){
            cout<<"The length of list is:"<<GetLength(L)<<endl;
        }
        else if(operate_code==4){
            cout<<"请输入要获取元素的位置"<<endl;
            cin>>i;
            GetElem(L,i,e);
            cout<<e<<endl;
        }
        else if(operate_code==5){
        	  int i,e;
              cout<<"请输入元素前驱"<<endl;
			  cin>>i;
			  if(PriorElem(L,i)==-1){
			  	 cout<<"格式错误"<<endl; 
			  }else{
			  	  cout<<PriorElem(L,i)<<endl;
			  }
        }
        else if(operate_code==6){
				 int i,e;
              cout<<"请输入元素后继"<<endl;
			  cin>>i;
			  if(NextElem(L,i)==-1){
			  	 cout<<"格式错误"<<endl;
			  } else{
			   	cout<<NextElem(L,i)<<endl;
			  }
        }
        else if(operate_code==7){
            cout<<"请输入插入元素及其位置"<<endl;
            cin>>e>>i;
            ListInsert_Sq(L,i,e);
        }
        else if(operate_code==8){
            cout<<"请输入要删除元素的位置"<<endl;
            cin>>i;
            ListDelete_Sq(L,i,e);
        }
        else if(operate_code==9){
            cout<<"The element of list are:"<<endl;
            Display_List(L);
        }
        else if(operate_code<0){
            break;
        }
        else{
            cout<<"\n操作码错误!!!"<<endl;
            show_help();
        }
    }
    DestroyList(L);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值