📄 1.h
字号:
#include <iostream>
using namespace std;
#define list_init_size 100
#define LISINTCREMENT 20
#define MAX 10
typedef int Status;
#define ERROR -1
#define OK 1
#define OVERFLOW -2
typedef int ElemType;
typedef int SElemType;
typedef struct //建立线性表
{
ElemType *elem;
int length;
int listsize;
}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 OK;
}
Status listinsert_Sq(Sqlist &l,int i,ElemType e)//第 i个位子前插入新的元素e
{ int *p,*q;
if(i<1||i>l.length+1) return ERROR;
if(l.length>=l.listsize)
{
l.elem=(ElemType * )realloc(l.elem,(l.listsize+LISINTCREMENT) * sizeof(ElemType));
if(! l.elem) exit(OVERFLOW);
l.listsize+=LISINTCREMENT;
}
q=&(l.elem[i-1]);
for(p=&l.elem[l.length-1];p>=q;--p)
*(p+1)=*p;
*q=e;
++l.length;
return OK;
}
Status listDelete_Sq(Sqlist &l,int i,ElemType &e)//删除线性表中第i个元素,并用e返回其值
{ int *p,*q;
if(i<1||i>l.length) return ERROR;
p=&(l.elem[i-1]);
e=*p;
q=l.elem+l.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--l.length;
cout<<"删除的元素为"<<e<<endl;
return OK;
}
Status Display_Sq(Sqlist &l)//显示线性表
{
int i;
cout<<""<<endl;
for(i=0;i<l.length;i++)
{
cout<<l.elem[i]<<' ';
}
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -