📄 linelistm.txt
字号:
//线性表的类定义linelist.h
#ifndef LLIST
#define LLIST
#include<stdlib.h>
#define OVERFLOW -1
#define LIST_INIT_SIZE 30
#define LISTINCREMENT 10
typedef char ElemType;
//线性表的动态分配顺序存储结构
class List{
private:
ElemType *elem;//存储空间基址
int length; //当前长度
int listsize;
//当前分配的存储容量以一数据元素存储长度为单位
public:
//无参构造函数
List(){}
//初始化顺序表
List(List *);
//删除顺序表
void DestroyList(List &L) {free(&L);}
//将顺序表置为空表
void ClearList() {length=0;}
//判断顺序表是否为空表
bool ListEmpty()
{if(length==0) return true;
else return false;}
//判断顺序表是否为满
bool ListFull()
{return length==listsize;}
//返回顺序表的长度
int ListLength();
// 获取顺序表中第i 个元素
ElemType GetElem(int,ElemType *);
//顺序表的插入算法
bool ListInsert(int,ElemType);
};
#endif
//线性表的操作linelist.cpp
#include "linelist.h"
List::List(List *L)
{L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem) exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
}
int List::ListLength()
{return length;}
ElemType List::GetElem(int i,ElemType *e)
{*e=elem[i];return *e;}
bool List::ListInsert(int i,ElemType e)
{ ElemType *p,*q;
if (i<0||i>length) return false;
q=&elem[i];
for(p=&elem[length];p>=q;--p)
*(p+1)=*p;
*q=e;
++length;
return true;
}
//linelistm.cpp
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include "linelist.h"
#include "linelist.cpp"
void main()
{ clrscr();
cout<<"linelistm.cpp运行结果:\n";
List La;
List Lb(&La);
ElemType ch[10];
int k;
cout<<"首先调用插入函数.\n";
for(k=0;k<8;k++)
{ch[k]='a'+k;
La.ListInsert(k,ch[k]);}
cout<<"表La长:"<<La.ListLength()<<endl;
getch();//cin.get();按回车键
for(k=0;k<7;k++)
cout<<La.GetElem(k,&ch[k])<<',';
cout<<La.GetElem(k,&ch[k])<<endl;getch();
}
linelistm.cpp运行结果:
首先调用插入函数.
表La长:8
a,b,c,d,e,f,g,h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -