数据结构(C语言)-顺序表操作.docVIP

  • 7
  • 0
  • 约7.62千字
  • 约 13页
  • 2017-08-26 发布于河南
  • 举报
数据结构(C语言)-顺序表操作

数据结构(C语言)-顺序表操作 #include stdio.h #include stdlib.h #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int ElemType; typedef int Status; // 线性表顺序存储结构 #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 typedef struct{ ElemType *elem; int length; int listsize; }SqList; Status InitList_Sq(SqList L){ // 构造一个空的线性表L。 L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)); if (! L.elem) exit(OVERFLOW); // 存储分配失败 L.length = 0; // 空表长度为0 L.listsize = LIST_INIT_SIZE; // 初始存储容量 return OK; } // InitList_Sq Status DestroyList_Sq(SqList L){ // 销毁线性表 L。 if (L.elem){ L.length = 0; L.listsize = 0; free(L.elem); } return OK; } // DestroyList_Sq Status ListEmpty_Sq(SqList L){ // 判断线性表是否为空。 if (L.length == 0) return TRUE; else return ERROR; } // ListEmpty_Sq Status ClearList_Sq(SqList L){ // 清空线性表 L。 if (! ListEmpty(L)){ for (int i = 0; i L.length; i++) L.elem[i] = 0; L.length = 0; } return OK; } // ClearList_Sq Status ListLength_Sq(SqList L){ // 返回线性表的长度。 return L.length; } // ListLength_Sq Status GetElem_Sq(SqList L, int i, ElemType e){ // 用 e 返回线性表的第 i 个元素。 if (i = 0 || i L.length) return ERROR; else{ e = L.elem[i - 1]; return OK; } } // GetElem_Sq Status LocateElem_Sq(SqList L, ElemType e, Status (*compare)(ElemType, ElemType)){ // 在顺序线性表 L 中查找第 1 个值和 e 满足 compare() 的元素的位序。 // 若找到,则返回其在 L 中的位序,否则返回 0 int i = 1; ElemType *p = L.elem; while (i = L.length !(*compare)(*p++, e)) ++i; if (i = L.length) return i; else return 0; } // LocateElem_Sq Status PriorElem_Sq(SqList L, ElemType cur_e, ElemType pre_e){ // 查找直接前驱 for (int i = 1; i L.length; i++) if (L.elem[i] == cur_e){ pre_e = L.elem[i-1]; return OK; } return FALSE; } // PriorElem_Sq Status NextElem_Sq(SqList L, ElemType cur_e, ElemType next_e){ // 查找直接后继 for (int i = 0; i L.length; i++) if (L.elem [i] == cur_e){ next_e = L.elem [i+1]; return OK; } return FALSE; } // Ne

文档评论(0)

1亿VIP精品文档

相关文档