数据结构- - 线性表- -顺序表_完善

丰富了前边的功能,更加完善。

#include <iostream>
#include <stdlib.h>
#define LIST_INIT_SIZE 100	//线性表储存空间的初始分配量
#define LISTINCREMENT 10	//线性表储存空间的分配增量
using namespace std;

const int OVERFLOW = -2;
typedef int Status;
typedef int ElemType;//将ElemType类型为int
typedef struct {
    ElemType *elem;
    int length;		//当前表长
    int listsize;	//当前分配的储存容量,单位为(ElemType)
}SqList;	


Status InitList_Sq(SqList *L)	//构建一个空链表
{
    L->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L->elem)
    exit(OVERFLOW);				//内存分配失败
    L->length=0;
    L->listsize=LIST_INIT_SIZE;
    return 1;
}
/*
Status InitList _Sq(SqList &L)
{
    L.elem=new ElemType[LIST_INIT_SIZE];
    L.lenght=0;
    L.listsize=LIST_INIT_SIZE;
    return 1;
}*/
/*
void DestroyList(SqList &L)
{
  if (L.elem)
	delete[ ] L.elem;    //释放存储空间
  L.length=0;
  L.listsize=0;
}*/
void DestroyList(SqList *L)
{
    if(L->elem) free(L->elem);
    L->length=0;
    L->listsize=0;
}
void ClearList (SqList &L)
{
    L.length=0;
}
int Getlength(SqList &L)
{
    return L.length;
}
bool IsEmpty(SqList &L)
{
    if(L.length==0)
        return true;
    else
        return false;
}
Status GetElem(SqList &L,int i,ElemType &e)
{
    if(i<1||i>L.length)
        return 0;
    e=L.elem[i-1];
    return 1;
}

Status ListInsert_Sq(SqList &L,int i,ElemType e){
    if(i<1||i>L.length+1)
        return 0;
    if(L.length==L.listsize)
    {
       ElemType *newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
       L.elem=newbase;
       L.listsize+=LISTINCREMENT;
    }
    ElemType* q=&L.elem[i-1];
    for(ElemType *p=&L.elem[L.length-1];p>=q;--p)
    	*(p+1)=*p;
    *q=e;
    ++L.length;
    return 1;
}
Status ListDelete_Sq(SqList &L,int i,ElemType &e)
{
    if(i<1||i>L.length)
        return 0;
    ElemType *q=&L.elem[i-1];
    e=*q;
    ElemType *p=&L.length-1;
    for(*q=*(q+1);q<=p;q++)
        *(q-1)=*q;
    L.length--;
    return 1;
}
int show(SqList &L){
	if(L.length==0)
	{
		cout<<"线性表内没有元素"<<endl;
		return 0;
	}
	cout<<"线性表里的元素有:"<<endl; 
	ElemType *p=&L.elem[L.length-1];
	for(ElemType *i=&L.elem[0];i<=p;i++)
		cout<<*i<<" ";
	cout<<endl; 
	return 1;
}
Status List_pre(SqList &L,int i,ElemType &e)
{
	if(i==1)
		return 0;
	else
	{
		e=L.elem[i-2];
		return 1;
	}
}
Status List_flo(SqList &L,int i,ElemType &e)
{
	if(i==L.length)
	return 0;
	else{
		e=L.elem[i];
		return 1;
	}
}
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;
    SqList L;
    InitList_Sq(&L);
    show_help();
    int i,x;
    ElemType e;
    //定义线性表变量,如SqList L;
    //调用初始化线性表函数,如  Init_List(L);
    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,x);
            cout<<"在"<<i<<"位置的元素为: "<<x<<endl; 
        }
        else if(operate_code==5)
        {
			cout<<"请输入元素:"<<endl;
			cin>>i;
			List_pre(L,i,x);
			if(!List_pre(L,i,x)) 
			cout<<"这个元素没有前驱。";
			else 
			cout<<"这个元素的前驱为:"<<x<<endl; 
        }
        else if(operate_code==6)
        {
			cout<<"请输入元素:"<<endl;
			cin>>i;
			List_flo(L,i,x);
			if(!List_flo(L,i,x)) 
			cout<<"这个元素没有后继。";
			else 
			cout<<"这个元素的后继为:"<<x<<endl; 
        }
        else if(operate_code==7)
        {
        	
        	cout<<"请输入插入的位置及元素:"<<endl; 
            cin>>i>>e; 
            ListInsert_Sq(L,i,e);
        }
        else if(operate_code==8)
        {
        	cout<<"请输入你要删除哪个位置的元素:"<<endl;
        	cin>>i;
			ListDelete_Sq(L,i,e);
        }
        else if(operate_code==9)
        {
			show(L);
        }
        else if(operate_code<0)
        {
            break;
        }
        else
        {
            cout<<"\n操作码错误!!!"<<endl;
            show_help();
        }


    }
    //调用销毁线性表函数,如  Destroy_List(L);
    DestroyList(&L);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43305922/article/details/85249495