您的位置:首页 > 编程语言 > C语言/C++

线性表-链式(c/c++实现)

2019-10-21 12:06 1856 查看

在写数据的结构就是自己排兵布阵,根据客户的需求可增删功能,顺序与链式编程思想是一样的,两个比较只需要改变实现功能这一块。

线性表(链式)实现的三个步骤:

  • 定义所需的功能(LinkList.h)
  • 实现功能(LinkList.cpp)
  • 调用功能(LinearTable.cpp)

 按上面的三个步骤我把代码写成三个文件的形式,同时代码都有详细的注释。

LinkList.h文件

/*
*名字:白客C
*LinkList.h
*时间:2019年9月25日
*环境:Visual Studio 2019
*/
#include <iostream>
#include<stdlib.h>
using namespace std;

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1   //不可行
#define OVERFLOW -2    //溢出

typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data;  // 数据域
struct LNode *next;  // 指针域
}LNode, * LinkList; //Lnode是结点类型,LinkList是结点指针类型

//初始化顺序表
Status InitList(LinkList& L);

//创建顺序表
//void CreateSqList(LinkList& L, int n);

//销毁顺序表
Status DestroyList(LinkList& L);

//清空顺序表
Status ClearList(LinkList& L);

//判断顺序表是否为空
Status ListEmpty(LinkList L);

//查看顺序表的长度
Status ListLength(LinkList L);

//用e返回L中第i个数据元素的值,查找值
Status GetElem(LinkList L, int i, ElemType& e);

//在线性表中查找e,存在则返回位序,否则返回ERROR
Status LocateElem(LinkList L, ElemType e);

//在顺序表中查找cur_e的前驱,并存入pre_e中
Status PriorElem(LinkList L, ElemType cur_e, ElemType& pre_e);

//在顺序表中查找cur_e的后继,并存入next_e中
Status NextElem(LinkList L, ElemType cur_e, ElemType& next_e);

//在顺序表中插入数据元素e,使之成为第 i 个数据元素
Status ListInsert(LinkList& L, int i, ElemType e);

//在顺序表中删除第i 个数据元素,并存入e中
Status ListDelete(LinkList& L, int i, ElemType& e);

//遍历顺序表
Status ListTraverse(LinkList L);

LinkList.cpp文件

/*
*名字:白客C
*LinkList.cpp
*时间:2019年9月25日
*环境:Visual Studio 2019
*/
#include "LinkList.h"

Status InitList(LinkList& L)
{
L = new LNode;
if (!L)
{
exit(OVERFLOW);
}
L->next = NULL;
return OK;;
}

Status DestroyList(LinkList& L)
{
ClearList(L);
delete L;
return OK;
}

Status ClearList(LinkList& L)
{
if (!L)
{
return ERROR;
}
LinkList p = NULL;
while (L->next)
{
p = L->next;
L->next = p->next;
delete p;
}
L->next = NULL;
return OK;
}

Status ListEmpty(LinkList L)
{
return !(L->next);
}

Status ListLength(LinkList L)
{
if (!L)
{
return OK;
}
int i = 0;
LinkList p;
p = L->next;
while (p)
{
p = p->next;
i++;
}
return i;
}

Status GetElem(LinkList L, int i, ElemType& e)
{
int j = 1;
LinkList p;
p = L->next;
while (p && j < i)
{
p = p->next;
j++;
}
if (!p || j > i)
{
return ERROR;
}
e = p->data;
return OK;
}

Status LocateElem(LinkList L, ElemType e)
{
if (!L || L->next == NULL)
{
return INFEASIBLE;
}
int i = 1;
LinkList p;
p = L->next;
while (p)
{
if (e == p->data)
{
return i;
}
else
{
p = p->next;
i++;
}
}
return INFEASIBLE;
}

Status PriorElem(LinkList L, ElemType cur_e, ElemType& pre_e)
{
LinkList q, p;
p = L->next; // p指向第一个结点
while (p->next) // p所指结点有后继
{
q = p->next; // q为p的后继
if (q->data == cur_e)
{
pre_e = p->data;
return 1;
}
p = q; // p向后移
}
return ERROR;
}

Status NextElem(LinkList L, ElemType cur_e, ElemType& next_e)
{
if (!L || L->next == NULL)
{
return ERROR;
}
LinkList p;
p = L->next;
while (p)
{
if (p->data == cur_e)
{
if (p->next)
{
next_e = p->next->data;
return OK;
}
}
p = p->next;
}
return ERROR;
}

Status ListInsert(LinkList& L, int i, ElemType e)
{
int j = 0;
LinkList s,p = L;
while (p && j < i - 1)
{
p = p->next;
j++;
}
if (!p || j > i - 1)
{
return ERROR;
}
s = new LNode;
s->data = e;
s->next = p->next;
p->next = s;
return OK;
}

Status ListDelete(LinkList& L, int i, ElemType& e)
{
int j = 0;
LinkList d, p = L;
while (p->next && j < i - 1)
{
p = p->next;
j++;
}
if (!(p->next) || j > i - 1)
{
return ERROR;
}
d = p->next;
p->next = d->next;
e = d->data;
delete d;
return OK;
}

Status ListTraverse(LinkList L)
{
if (!L || L->next == NULL) {
return ERROR;
}

LinkList p;
p = L->next;
while (p)
{
cout << p->data;
p = p->next;
}
return OK;
}

LinearTable.cpp文件

/*
*名字:白客C
*LinearTable.cpp
*时间:2019年9月25日
*环境:Visual Studio 2019
*/
#include "LinkList.h"
#include <iostream>
#include<stdlib.h>
using namespace std;

int main()
{
LinkList L=NULL;
int i;
cout << "0.退出" << endl;
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 << "10.通过下标插入元素" << endl;
cout << "11.通过下标删除元素" << endl;
cout << "12.查询当前线性表的元素" << endl;

do
{
cout << endl << "请选择一个操作:";
cin >> i;
switch (i)
{
case 1:
InitList(L);
cout << "初始化线性表成功";
break;

case 2:
if (DestroyList(L))
{
cout << "销毁顺序表成功" << endl;
}
break;

case 3:
if (ClearList(L))
{
cout << "顺序表以清空" << endl;
}
break;

case 4:
if (ListEmpty(L))
{
cout << "顺序表为空" << endl;
}
else
{
cout << "顺序表不为空" << endl;
}
break;

case 5:
cout << "线性表长度为" << ListLength(L) << endl;
break;

case 6:
int Get_i;
ElemType Get_e;
cout << "请输入下标:";
cin >> Get_i;
if (GetElem(L, Get_i, Get_e))
{
cout << "您要找的元素是:" << Get_e << endl;
}
else
{
cout << "ERROR " << ERROR << endl;
}
break;

case 7:
int Locate_i;
ElemType Locate_e;
cout << "请输入查找要查询的元素的下标:";
cin >> Locate_e;
Locate_i = LocateElem(L, Locate_e);
if (!Locate_i)
{
cout << "ERROR " << ERROR << endl;
}
else
{
cout << "您要找的下标是:" << Locate_i << endl;
}
break;

case 8:

ElemType pei_cur_e, pri_e;
cout << "请输入元素:";
cin >> pei_cur_e;
if (PriorElem(L, pei_cur_e, pri_e))
{
cout << "您要找的前驱是:" << pri_e << endl;
}
else
{
cout << "ERROR " << ERROR << endl;
}
break;

case 9:
ElemType next_cur_e, next_e;
cout << "请输入元素:";
cin >> next_cur_e;
if (NextElem(L, next_cur_e, next_e))
{
cout << "您要找的后继驱是:" << next_e << endl;
}
else
{
cout << "ERROR " << ERROR << endl;
}
break;

case 10:
int insert_i;
ElemType insert_e;
cout << "请你输入要插入的下标:";
cin >> insert_i;
cout << "请你输入要插入的元素:";
cin >> insert_e;
if (ListInsert(L, insert_i, insert_e))
{
cout << "插入下标为" << insert_i << "的元素值为" << insert_e << "成功" << endl;
}
else
{
cout << "ERROR " << ERROR << endl;
}
break;

case 11:
int delete_i;
ElemType delete_e;
cout << "请你输入要删除元素的下标:";
cin >> delete_i;
if (ListDelete(L, delete_i, delete_e))
{
cout << "删除下标为" << delete_i << "的元素值为" << delete_e << "成功" << endl;
}
else
{
cout << "ERROR " << ERROR << endl;
}
break;

case 12:
cout << "线性表元素:" << endl;
ListTraverse(L);
cout << endl;
break;

default:
break;
}
} while (i != 0);
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: