严蔚敏版数据结构(C语言版)算法实现代码
数据结构(C语言版)代码实现
部分采用C++的语法实现
线性表
顺序表与链表
顺序表
代码如下:
#include <bits/stdc++.h>
using namespace std;
#define MaxSize 100
#define ElemType int
#define Status int
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ERROR 0
#define OK 1
#define INFEASIBLE -1
#define TRUE 1
#define FALSE 0
/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef struct
{
ElemType * elem;
int length;
int listSize;
}SqList;
Status CmpGreater(ElemType data, ElemType e)
{
return data > e ? TRUE : FALSE;
}
Status CmpSame(ElemType data, ElemType e)
{
return data == e ? TRUE : FALSE;
}
void visit(ElemType *c) /* ListTraverse()调用的函数(类型要一致) */
{
printf("%d ",*c);
}
Status ClearList(SqList &L)
{
/* 将L重置为空表 */
L.length = 0;
return OK;
}
Status DestoryList(SqList &L)
{
/* 销毁顺序线性表L */
delete(L.elem);
L.elem = NULL;
L.length = 0;
L.listSize = 0;
return OK;
}
bool ListEmpty(SqList L)
{
if (0 == L.length)
return true;
return false;
}
Status GetElem(SqList L, int i, ElemType &e)
{
if (i < 1 || i > L.length)
exit(ERROR);
e = *(L.elem + i - 1);
return OK;
}
Status PriorElem(SqList L, ElemType e, ElemType &pre_e)
{
/* 若e是L的数据元素,且不是第一个,则用pre_e返回它的前驱, */
/* 否则操作失败,pre_e无定义 */
int i = 2;
ElemType * p = L.elem + 1;
while (i <= L.length && *p != e)
{
p++;
i++;
}
if (i > L.length)
return INFEASIBLE;
pre_e = *(--p);
return OK;
}
Status NextElem(SqList L, ElemType e, ElemType next_e)
{
/* 若e是L的数据元素,且不是最后一个,则用next_e返回它的后继, */
/* 否则操作失败,next_e无定义 */
int i = 1;
ElemType *p = L.elem;
while (i < L.length && *p != e)
{
i++;
p++;
}
if (i == L.length)
return INFEASIBLE;
next_e = *(++p);
return OK;
}
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)
{
/* 在L中第i个位置之前插入新的数据元素e,L的长度加1 */
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 (NULL == newbase) exit(OVERFLOW);
L.elem = newbase;
L.listSize += LISTINCREMENT;
}
for (int j = L.length - 1; j >= i - 1; j++)
L.elem[j + 1] = L.elem[j];
L.elem[i - 1] = e;
L.length ++;
return OK;
}
Status ListDelete_Sq(SqList &L, int i, ElemType &e)
{
if (i < 1 || i > L.length) return ERROR;
e = L.elem[i - 1];
for (int j = i - 1; j < L.length - 1; j++)
L.elem[j] = L.elem[j + 1];
L.length --;
return OK;
}
Status LocateElem(SqList L, ElemType e, Status(* compare)(ElemType, ElemType))
{
/* 在顺序表L中查找第一个值与e满足compare()的元素位序 */
int i = 1;
ElemType *p = L.elem;
/* int j = 0;
while (i <= L.length && !(* compare)(L.elem[j++], e)); */
while (i <= L.length && !(* compare)(* p++, e)) ++i;
if (i <= L.length) return i;
else return 0;
}
void Merge_Sq(SqList La, SqList Lb, SqList &Lc)
{
/* 已知顺序线性表La和Lb的元素按值非递减排列 */
/* 归并La和Lb得到新的顺序线性表Lc, Lc的元素也按值非递减排序 */
ElemType *pa = La.elem, *pb = Lb.elem;
Lc.listSize = Lc.length = La.length + Lb.length;
ElemType *pc = Lc.elem = (ElemType *)malloc(Lc.listSize * sizeof(ElemType));
if (NULL == Lc.elem) exit(OVERFLOW);
ElemType *pa_last, *pb_last;
pa_last = La.elem + La.length - 1;
pb_last = Lb.elem + Lb.length - 1;
while (pa <= pa_last && pb <= pb_last)
{
if (*pa < *pb)
*pc++ = *pa++;
else
*pc++ = *pb++;
}
while (pa <= pa_last) *pc++ = *pa++;
while (pb <= pb_last) *pc++ = *pb++;
}
void union_Sq(SqList &La, SqList Lb)
{
/* 将线性表在Lb中但不在La中的数据元素插入到La中 */
for (int i = 1; i <= Lb.length; i++)
{
if (LocateElem(La, Lb.elem[i - 1], CmpSame))
ListInsert_Sq(La, La.length++, Lb.elem[i - 1]);
}
}
Status ListTraverse(SqList L, void(*vi)(ElemType*))
{
/* 依次对L的每个数据元素调用函数vi()。一旦vi()失败,则操作失败 */
ElemType * p = L.elem;
for (int i = 1; i <= L.length; i++)
vi(p++);
cout << endl;
return OK;
}
void InsertAscend(SqList &L, ElemType e)
{
/* 在L中按非降序插入新的数据元素e,L的长度加1 */
ElemType *newbase, *p;
int k;
if (L.length >= (L.listSize))
{
newbase = (ElemType *)realloc(L.elem, (L.listSize + LISTINCREMENT) * sizeof(ElemType));
if (!newbase)
exit(OVERFLOW);
L.elem = newbase;
L.listSize += LISTINCREMENT;
}
p = L.elem;
for (k = 1; k <= L.length; k++)
if(e > *p)
p++;
else
break;
ListInsert_Sq(L, k, e);
}
int main(int argc, char const *argv[])
{
SqList L;
ElemType e;
cout << "InitList_Sq" << endl;
InitList_Sq(L);
cout << "ListEmpty" << endl;
if(ListEmpty(L) == TRUE)
cout << "L is empty" << endl;
else
cout << "L is not empty" << endl;
cout << "ListInsert" << endl;
for (int i = 1; i <= 12; i++)
ListInsert_Sq(L, i, 2 * i);
cout << "ListTraverse" << endl;
ListTraverse(L, visit);
cout << "ListDelete" << endl;
ListTraverse(L, visit);
if(OK == ListDelete_Sq(L, 5, e))
cout << "Delete " << e << "successfully" << endl;
else
cout << "NOT FOUND" << endl;
ListTraverse(L, visit);
cout << "GetElem" << endl;
GetElem(L, 3, e);
cout << "LocateElem" << endl;
int i = LocateElem(L, 7, CmpGreater);
cout << L.elem[i - 1] << endl;
cout << "PriorElem" << endl;
ElemType cur_e = 4;
if(OK == PriorElem(L, cur_e, e))
cout << e << endl;
else
cout << "NOT FOUND" << endl;
cout << "NextElem" << endl;
cur_e = 5;
if(OK == NextElem(L, cur_e, e))
cout << e << endl;
else
cout << "NOT FOUND" << endl;
cout << "ClearList" << endl;
cout << "Before cleaning" << endl;
if(TRUE == ListEmpty(L))
cout << "L is empty" << endl;
else
cout << "L is not empty" << endl;
ClearList(L);
cout << "After cleaning" << endl;
if(TRUE == ListEmpty(L))
cout << "L is empty" << endl;
else
cout << "L is not empty" << endl;
cout << "Before destruction" << endl;
if(L.elem != NULL)
cout << "Exist" << endl;
else
cout << "Does not exist" << endl;
DestoryList(L);
cout << "After destruction" << endl;
if(L.elem != NULL)
cout << "Exist" << endl;
else
cout << "Does not exist" << endl;
return 0;
}
运行结果如下:
链表
单向链表
#include <bits/stdc++.h>
using namespace std;
#define MaxSize 100
#define ElemType int
#define Status int
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ERROR 0
#define OK 1
#define INFEASIBLE -1
#define TRUE 1
#define FALSE 0
typedef struct LNode
{
ElemType data; // data为抽象元素类型
struct LNode *next; // next为指针类型
}LNode, *LinkList;
void visit(ElemType c) /* ListTraverse()调用的函数(类型要一致) */
{
printf("%d ",c);
}
Status CmpGreater(ElemType data, ElemType e)
{
return data > e ? TRUE : FALSE;
}
Status DestoryList(LinkList &L)
{
// 销毁链表
LinkList p;
if (NULL == L) // 确保链表存在
return ERROR;
p = L;
while (p != NULL)
{
p = L->next;
free(L);
L = p;
}
L = NULL;
return OK;
}
Status Cl