#include "LinearList.h" Status comp(ElemType c1, ElemType c2) { if (c1 == c1 * c2) return TRUE; else return FALSE; } void print(ElemType *e) { printf("%d ",*e); } void dbl(ElemType *c) { *c *= 2; } void main() { SqList L; ElemType e, e0; Status i; int j, k; i = InitList(&L); printf("===========================================================================\n"); printf("初始化L后:L.elem = %u,L.length = %d,L.listsize = %d\n", L.elem, L.length, L.listsize); for (j = 1; j <= 5; j++) i = ListInsert(&L,1,j); printf("在L的表头一次插入1-5后:*L.elem = "); ListTraverse(L, &print); system("pause"); }
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <malloc.h>#include <io.h>using namespace std;//=================================================#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1//=================================================typedef int Status;typedef int Boolean;
#include "Common.h" //================================================= #define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 typedef int ElemType; typedef struct { ElemType *elem;//存储空间的基址; int length;//当前长度; int listsize;//当前分配的存储容量; }SqList,*pSqList; //================================================= Status InitList(SqList *L); Status DestroyList(SqList *L); Status ClearList(SqList *L); Status ListEmpty(SqList L); int ListLength(SqList L); Status GetElem(SqList L, int i, ElemType *e); int LocateElem(SqList L, ElemType e, Status(*compare)(ElemType, ElemType)); Status PriorElem(SqList L, ElemType cur_e, ElemType *pre_e); Status NextElem(SqList L, ElemType cur_e, ElemType *next_e); Status ListInsert(SqList *L, int i, ElemType e); Status ListDelete(SqList *L, int i, ElemType *e); Status ListTraverse(SqList L, void(*v)(ElemType*));
#include "LinearList.h" //----------------------------------------------------------------------- //---初始化线性表; //----------------------------------------------------------------------- Status InitList(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 DestroyList(SqList *L) { free((*L).elem);//释放内存; (*L).length = 0; (*L).listsize = 0; return OK; } //----------------------------------------------------------------------- //---清空线性表; //----------------------------------------------------------------------- Status ClearList(SqList *L) { (*L).length = 0;//将当前长度置空; return OK; } //----------------------------------------------------------------------- //---检测线性表是否为空; //----------------------------------------------------------------------- Status ListEmpty(SqList L) { if (L.length == 0) return TRUE; else return ERROR; } //----------------------------------------------------------------------- //---获取线性表的长度; //----------------------------------------------------------------------- int ListLength(SqList L) { return L.length; } //----------------------------------------------------------------------- //---获取第i个位置的元素; //----------------------------------------------------------------------- Status GetElem(SqList L,int i,ElemType *e) { if (i < 1 || i > L.length) return ERROR; *e = *(L.elem + i - 1);//亦可写为L.elem[i - 1]; return OK; } //----------------------------------------------------------------------- //---查找线性表中是否包含元素e; //----------------------------------------------------------------------- int LocateElem(SqList L, ElemType e, Status(*compare)(ElemType, ElemType)) { ElemType *p = L.elem;//指针变量p指向L.elem; int i = 1; while ((i < L.length) && !compare(*p++, e)) i++; if (i < L.length) return i; else return 0; } //----------------------------------------------------------------------- //---获取当前元素cur_e的前一个元素; //----------------------------------------------------------------------- Status PriorElemType(SqList L, ElemType cur_e, ElemType *pre_e) { ElemType *p = L.elem + 1;//因为寻找的是当前元素前一个元素,所以这里必须让p开始指向第二个元素; int i = 1; while ((i < L.length) && *p++ != cur_e) i++; if (i < L.length) { *pre_e = *(L.elem + i - 1); return OK; } else return INFEASIBLE; } //----------------------------------------------------------------------- //---获取当前元素cur_e的下一个元素; //----------------------------------------------------------------------- Status NextElem(SqList L, ElemType cur_e, ElemType *next_e) { ElemType *p = L.elem;//因为寻找的当前元素的下一个元素,所以p初始化必须指向首地址; int i = 1; while ((i < L.length) && *p++ != cur_e) { i++; } if (i < L.length) { *next_e = *p; return OK; } else return INFEASIBLE; } //----------------------------------------------------------------------- //---在线性表的位置i插入一个元素; //----------------------------------------------------------------------- Status ListInsert(SqList *L, int i, ElemType e) { ElemType *pNew,*p,*q; if (i < 1 || i >(*L).length + 1)//首先检查i值是否符合要求; return ERROR; if ((*L).length >= (*L).listsize)//如果当前长度大于开辟空间则扩大空间; { pNew = (ElemType*)realloc((*L).elem,((*L).listsize + LISTINCREMENT) * sizeof(ElemType)); if (!pNew) exit(OVERFLOW); (*L).elem = pNew; (*L).listsize += LISTINCREMENT; } //从i位置开始的所有后序元素后移; p = (*L).elem + i - 1; for (q = (*L).elem + (*L).length - 1; q >= p; q--) { *(q + 1) = *q; } *p = e;//将当前元素赋给i位置; (*L).length++;//存储长度加1; return OK; } //----------------------------------------------------------------------- //---删除线性表位置i的元素; //----------------------------------------------------------------------- Status ListDelete(SqList *L, int i, ElemType *e) { ElemType *p, *q; if (i < 1 || i >(*L).length) return ERROR; //删除位置后序的元素前移; p = (*L).elem + i - 1; *e = *p; for (q = p; q < (*L).elem + (*L).length - 1; q++) { *q = *(q + 1); } (*L).length--; return OK; } //----------------------------------------------------------------------- //---遍历线性表元素; //----------------------------------------------------------------------- Status ListTraverse(SqList L, void(*v)(ElemType)) { ElemType *p = L.elem; while (p != L.elem + L.length - 1) { v(*p++); } printf("\n"); return OK; }
线性表的顺序表示
最新推荐文章于 2024-03-09 18:46:56 发布