Common.h
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULL 0typedef int Status;typedef int ElemType;
LinkList.h
#include "Common.h"typedef struct LNode
{//结点的数据域ElemType data; //结点的数据域struct LNode *next; //结点的指针域
}LNode,*LinkList; //LinkList为指向结构体LNode的指针类型Status ListInit(LinkList& L);
Status ListDestroy(LinkList& L);
Status ListClear(LinkList& L);
Status ListInsert(LinkList& L, int i, ElemType e);
Status ListDelete(LinkList& L, int i);int GetLength(LinkList L);
int IsEmpty(LinkList L);Status GetElem(LinkList L, int i, ElemType& e);
LNode *LocateElem(LinkList L, ElemType e);
SqList.h
#include "Common.h"#define MAXSIZE 100 //最大长度
typedef struct {ElemType* elem; //指向数据元素的基地址int length; //线性表的当前长度
}SqList;Status ListInit(SqList& L);
Status ListDestroy(SqList& L);
Status ListClear(SqList& L);
Status ListInsert(SqList& L, int i, ElemType e);
Status ListDelete(SqList& L, int i);int GetLength(SqList L);
int IsEmpty(SqList L);Status GetElem(SqList L, int i, ElemType& e);
int LocateElem(SqList L, ElemType e);
LinkList.cpp
#include "LinkList.h"Status ListInit(LinkList& L)
{//构造一个空的单链表LL = new LNode; //生成新的结点作为头结点,用指针L指向头结点L->next = NULL; //头结点的指针域置空return OK;
};Status ListDestroy(LinkList& L)
{LinkList p;while (L){p = L;L = L->next;delete p;}return OK;
};
Status ListClear(LinkList& L)
{LinkList p, q;p = L->next;while (p){q = p->next;delete p;p = q;}L->next = NULL;return OK;
};
Status ListInsert(LinkList& L, int i, ElemType e)
{LinkList p = L; int j = 0;while (p && (j < i - 1)){p = p->next;++j;}if (!p || j > i - 1) return ERROR;LinkList s = new LNode;s->data = e;s->next = p->next;p->next = s;return OK;
};
Status ListDelete(LinkList& L, int i)
{LinkList p = L; int j = 0;while((p->next)&&(j<i-1)){p = p->next;++j;}if (!(p->next) || (j > i - 1)) return ERROR;LinkList q = p->next;p->next = q->next;delete q;return OK;
};int GetLength(LinkList L)
{LinkList p;p = L->next;int i = 0;while(p){i++;p = p->next;}return i;
};
int IsEmpty(LinkList L)
{if (L->next)return 0;elsereturn 1;
};Status GetElem(LinkList L, int i, ElemType& e)
{//在带头结点的单链表L中根据序号i获取元素的值,用e返回L中第i个数据元素的值LinkList p = L->next;int j = 1; //初始化,p指向首元结点,计数器j初值赋为1while (p && j < i) //顺链域向后扫描,直到p为空或p指向第i个元素{p = p->next;++j;}if (!p || j > i)return ERROR; //i值不合法i>n或i<=0e = p->data; //取第i个结点的数据域return OK;};
LNode* LocateElem(LinkList L, ElemType e)
{//在带头结点的单链表L中查找值为e的元素LinkList p = L->next; //初始化,p指向首元结点while (p && p->data != e)p = p->next;return p; //查找成功返回值为e的结点地址p,查找失败p为NULL
};
main.cpp
#include <iostream>
int main()
{}
SqList.cpp
#include <iostream>
#include "SqList.h"Status ListInit(SqList& L)
{L.elem = new ElemType[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间if (!L.elem) return ERROR; //存储分配失败退出L.length = 0; //空表长度为0return OK;
};Status ListDestroy(SqList& L)
{if (L.elem) delete[]L.elem; //释放存储空间L.length = 0;return OK;
};Status ListClear(SqList& L)
{if (!L.elem) exit(OVERFLOW);L.length = 0;
};Status ListInsert(SqList& L, int i, ElemType e)
{//在顺序表L中第i个位置插入新的元素e,i值的合法范围是1<=i<=L.length+1if ((i < 1) || (i > L.length + 1)) return ERROR; //i值不合法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; //表长加1return OK;
};Status ListDelete(SqList& L, int i)
{//在顺序表L中第i个位置删除第i个元素,i值的合法范围是1<=i<=L.lengthif ((i < 1) || (i > L.length)) return ERROR;//i值不合法for (int j = i;j <= L.length - 1;j++) L.elem[j - 1] = L.elem[j]; //被删除元素之后的元素前移--L.length;return OK;
};int GetLength(SqList L)
{return (L.length);
};int IsEmpty(SqList L)
{//if (L.length == 0) return 1;//else return 0;return L.length = 0;
};Status GetElem(SqList L, int i, ElemType& e)
{if (i<1 || i>L.length) return ERROR; //判断i值是否合理,若不合理返回ERRORe = L.elem[i - 1]; //elem[i-1]单元存储第i个数据元素return OK;
};
int LocateElem(SqList L, ElemType e)
{for (int i = 0;i < L.length;i++)if (L.elem[i] == e) return i + 1;return 0;
};