严蔚敏数据结构顺序表的所有基本操作
2015-12-23 23:31
579 查看
头文件:
#include<cstdlib>
#include<iostream>
using namespace std;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
//构造一个空的线性表L
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;
}
//销毁线性表L
Status DestroyList_Sq(SqList& L)
{
free(L.elem);
L.elem = NULL;
return OK;
}
//将线性表L置为空表
Status ClearList_Sq(SqList& L)
{
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
//判断线性表L是否为空
Status ListEmpty_Sq(SqList L)
{
if (0 == L.length)
return TRUE;
else
return FALSE;
}
//返回线性表L中数据元素个数
int ListLength_Sq(SqList L)
{
return L.length;
}
//用元素e返回线性表L中第i个数据元素的值
Status GetElem_Sq(SqList L, int i, ElemType &e)
{
if (i<1 || i>L.length)
exit(ERROR);
e = *(L.elem + i - 1);
return OK;
}
//数据元素之间的关系函数,例如数学关系:a=b
Status compare(ElemType a, ElemType b)
{
if (a ==b)
return TRUE;
else
return FALSE;
}
//返回线性表L中第一个与元素e满足compare()关系的数据元素的位序;若不存在,返回0
int LocateElem_Sq(SqList L, ElemType e, Status (*compare)(ElemType,ElemType))
{
int i = 1;
ElemType* p = L.elem;
while (i <= L.length&&!(*compare)(*p++, e))
++i;
if (i <= L.length)
return i;
else
return 0;
}
//若cur_e是线性表L中的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败,pre_e无定义
Status PriorElem_Sq(SqList L, ElemType cur_e, ElemType& pre_e)
{
int i = LocateElem_Sq(L, cur_e, compare);
if(i>1&&i<=L.length)
{
pre_e = *(L.elem + i - 2);
return OK;
}
return ERROR;
}
//若cur_e是线性表L中的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败,next_e无定义
Status NextElem_Sq(SqList L, ElemType cur_e, ElemType& next_e)
{
int i = LocateElem_Sq(L, cur_e, compare);
if (i>=1 && i < L.length)
{
next_e = *(L.elem + i);
return OK;
}
return ERROR;
}
//在线性表L中第i个位置之前插入新的数据元素e,表L的长度加1
Status ListInsert_Sq(SqList& L, int i, ElemType e)
{
if (i<1 || i>L.length + 1)
return ERROR;
if (L.length >= L.listsize)
{
ElemType* newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT)*sizeof(ElemType));
if (!(newbase))
exit(OVERFLOW);
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 OK;
}
//删除线性表L的第i个元素,并用元素e返回其值,表L的长度减1
Status ListDelete_Sq(SqList& L, int i, ElemType& e)
{
if (i<1 || i>L.length)
return ERROR;
ElemType* p = &(L.elem[i - 1]);
e = *p;
ElemType* q = L.elem + L.length - 1;
for (++p; p <= q; ++p)
*(p - 1) = *p;
--L.length;
return OK;
}
//访问线性表中的数据元素
Status visit(ElemType i)
{
if (cout << i << " ")
return TRUE;
else
return FALSE;
}
//一次对线性表L的每个数据元素调用函数visit();一旦visit()失败,则操作失败
Status ListTraverse_Sq(SqList L, Status (*visit)(ElemType))
{
for (int i = 1; i <= L.length; ++i)
if (!visit(*L.elem++))
return ERROR;
return OK;
}
主函数:
#include"SqList.h"
void main(void)
{
SqList L;
InitList_Sq(L);
for (int i = 1,j=1; j <=10; ++j,i+=2)
ListInsert_Sq(L, j, i);
cout << "线性表L的信息:" << endl;
cout << "*****************************************" << endl;
cout << "L.listsize=" << L.listsize << endl;
cout << "L.length=" << ListLength_Sq(L) << endl;
cout << "数据元素依次为:";
ListTraverse_Sq(L, visit);
cout << endl;
cout << "*****************************************" << endl;
cout << "表L中值为7的数据元素的位置在:第" << LocateElem_Sq(L, 7, compare) << endl;
ElemType e;
GetElem_Sq(L, 2, e);
cout << "线性表L中第2个数据元素的值为:" << e << endl;
ElemType pre;
PriorElem_Sq(L, e, pre);
cout << "它的前驱是:" << pre << endl;
ElemType next;
NextElem_Sq(L, e, next);
cout << "它的后继是:" << next << endl;
cout << "删除数据元素" << e << endl;
if (ListDelete_Sq(L, LocateElem_Sq(L, e, compare), e))
{
cout << "删除成功!" << endl;
cout << "更新后的线性表L的信息为:" << endl;
cout << "*****************************************" << endl;
cout << "L.listsize=" << L.listsize << endl;
cout << "L.length=" << ListLength_Sq(L) << endl;
cout << "数据元素依次为:";
ListTraverse_Sq(L, visit);
cout << endl;
cout << "*****************************************" << endl;
}
else
cout << "删除失败!" << endl;
cout << "线性表是否为空表:";
if (ListEmpty_Sq(L))
cout << "是" << endl;
else
{
cout << "否" << endl;
cout << "将线性表L置为空表" << endl;
ClearList_Sq(L);
}
cout << "销毁线性表L" << endl;
DestroyList_Sq(L);
}
#include<cstdlib>
#include<iostream>
using namespace std;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
//构造一个空的线性表L
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;
}
//销毁线性表L
Status DestroyList_Sq(SqList& L)
{
free(L.elem);
L.elem = NULL;
return OK;
}
//将线性表L置为空表
Status ClearList_Sq(SqList& L)
{
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
//判断线性表L是否为空
Status ListEmpty_Sq(SqList L)
{
if (0 == L.length)
return TRUE;
else
return FALSE;
}
//返回线性表L中数据元素个数
int ListLength_Sq(SqList L)
{
return L.length;
}
//用元素e返回线性表L中第i个数据元素的值
Status GetElem_Sq(SqList L, int i, ElemType &e)
{
if (i<1 || i>L.length)
exit(ERROR);
e = *(L.elem + i - 1);
return OK;
}
//数据元素之间的关系函数,例如数学关系:a=b
Status compare(ElemType a, ElemType b)
{
if (a ==b)
return TRUE;
else
return FALSE;
}
//返回线性表L中第一个与元素e满足compare()关系的数据元素的位序;若不存在,返回0
int LocateElem_Sq(SqList L, ElemType e, Status (*compare)(ElemType,ElemType))
{
int i = 1;
ElemType* p = L.elem;
while (i <= L.length&&!(*compare)(*p++, e))
++i;
if (i <= L.length)
return i;
else
return 0;
}
//若cur_e是线性表L中的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败,pre_e无定义
Status PriorElem_Sq(SqList L, ElemType cur_e, ElemType& pre_e)
{
int i = LocateElem_Sq(L, cur_e, compare);
if(i>1&&i<=L.length)
{
pre_e = *(L.elem + i - 2);
return OK;
}
return ERROR;
}
//若cur_e是线性表L中的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败,next_e无定义
Status NextElem_Sq(SqList L, ElemType cur_e, ElemType& next_e)
{
int i = LocateElem_Sq(L, cur_e, compare);
if (i>=1 && i < L.length)
{
next_e = *(L.elem + i);
return OK;
}
return ERROR;
}
//在线性表L中第i个位置之前插入新的数据元素e,表L的长度加1
Status ListInsert_Sq(SqList& L, int i, ElemType e)
{
if (i<1 || i>L.length + 1)
return ERROR;
if (L.length >= L.listsize)
{
ElemType* newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT)*sizeof(ElemType));
if (!(newbase))
exit(OVERFLOW);
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 OK;
}
//删除线性表L的第i个元素,并用元素e返回其值,表L的长度减1
Status ListDelete_Sq(SqList& L, int i, ElemType& e)
{
if (i<1 || i>L.length)
return ERROR;
ElemType* p = &(L.elem[i - 1]);
e = *p;
ElemType* q = L.elem + L.length - 1;
for (++p; p <= q; ++p)
*(p - 1) = *p;
--L.length;
return OK;
}
//访问线性表中的数据元素
Status visit(ElemType i)
{
if (cout << i << " ")
return TRUE;
else
return FALSE;
}
//一次对线性表L的每个数据元素调用函数visit();一旦visit()失败,则操作失败
Status ListTraverse_Sq(SqList L, Status (*visit)(ElemType))
{
for (int i = 1; i <= L.length; ++i)
if (!visit(*L.elem++))
return ERROR;
return OK;
}
主函数:
#include"SqList.h"
void main(void)
{
SqList L;
InitList_Sq(L);
for (int i = 1,j=1; j <=10; ++j,i+=2)
ListInsert_Sq(L, j, i);
cout << "线性表L的信息:" << endl;
cout << "*****************************************" << endl;
cout << "L.listsize=" << L.listsize << endl;
cout << "L.length=" << ListLength_Sq(L) << endl;
cout << "数据元素依次为:";
ListTraverse_Sq(L, visit);
cout << endl;
cout << "*****************************************" << endl;
cout << "表L中值为7的数据元素的位置在:第" << LocateElem_Sq(L, 7, compare) << endl;
ElemType e;
GetElem_Sq(L, 2, e);
cout << "线性表L中第2个数据元素的值为:" << e << endl;
ElemType pre;
PriorElem_Sq(L, e, pre);
cout << "它的前驱是:" << pre << endl;
ElemType next;
NextElem_Sq(L, e, next);
cout << "它的后继是:" << next << endl;
cout << "删除数据元素" << e << endl;
if (ListDelete_Sq(L, LocateElem_Sq(L, e, compare), e))
{
cout << "删除成功!" << endl;
cout << "更新后的线性表L的信息为:" << endl;
cout << "*****************************************" << endl;
cout << "L.listsize=" << L.listsize << endl;
cout << "L.length=" << ListLength_Sq(L) << endl;
cout << "数据元素依次为:";
ListTraverse_Sq(L, visit);
cout << endl;
cout << "*****************************************" << endl;
}
else
cout << "删除失败!" << endl;
cout << "线性表是否为空表:";
if (ListEmpty_Sq(L))
cout << "是" << endl;
else
{
cout << "否" << endl;
cout << "将线性表L置为空表" << endl;
ClearList_Sq(L);
}
cout << "销毁线性表L" << endl;
DestroyList_Sq(L);
}
相关文章推荐
- java数据结构--二叉树,递归遍历,非递归遍历,层次遍历
- 二叉树的先序、中序和后序遍历(总结)
- 数据结构实验之排序四:寻找大富翁
- 数据结构与算法 LeetCode编程练习--counts Prime
- 数据结构之图(邻接表存储,DFS和BFS遍历)
- 严蔚敏数据结构单链表的所有基本操作
- 2-5-归并链式存储的单链表-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版
- 数据结构之链表
- 【Educational Codeforces Round 3 E】【树链剖分】Minimum spanning tree for each edge 图构最小生成树,生成树必须包含第i条边
- 2-路插入排序
- 数据结构实验之排序六:希尔排序 【OJ--3403】
- 2-4-单链表链式存储结构-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版
- 数据结构算法1------算法和算法分析
- 数据结构设计——考生报名管理
- SHH数据结构-【实训-银行业务的模拟系统 】
- 2-3-归并单链表(顺序表)-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版
- 数据结构实践课程设计【通讯录管理系统】
- hdu 1800 (字符串哈希)
- 数据结构基础概念
- Java数据结构和算法系列1--链表