数据结构笔记--顺序表

来源:互联网 发布:淘宝小镇姗姗模特是谁 编辑:程序博客网 时间:2024/05/21 15:01

………………数据结构太烂了。。

找了好几本书,重头学吧。。。。


万事开头难啊

写个顺序存储的顺序表写了一个下午!!总算写好了,有点小问题,大家可以讨论讨论

源代码:   我在头文件里把所有的函数都定义出来了。。(好吧。我是懒得再写源文件了。。觉得不爽的可以把定义和声明分开。。。)
FileName:  SqList.h

----------------------------------------------------------------------------------------------------------------------------------

#ifndef __SQLIST__H__#define __SQLIST__H__#include <stdio.h>#include <malloc.h>#define LIST_INIT_SIZE 100#define LISTINCREMENT 10#define ElemType inttypedef enum {    OK,     NO,    OVERFLOW}Status;typedef struct {    ElemType *elem;    int length;    int listsize;}SqList;//Initial the listStatus InitList_Sq(SqList *L) {    L->elem = (ElemType *) malloc(LIST_INIT_SIZE * sizeof(ElemType));    if (!L->elem) {        return OVERFLOW;    }    L->length = 0;    L->listsize = LIST_INIT_SIZE;    return OK;}void DestroyList_Sq(SqList *L) {    free(L->elem);    L->length = 0;    L->listsize = 0;}Status ClearList_Sq(SqList *L) {    DestroyList_Sq(L);    L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));    L->length = 0;    if (!L->elem) {        return OVERFLOW;    } else {        return OK;    }}Status ListEmpty_Sq(SqList L) {    if (!L.elem) {        return OVERFLOW;    } else {        if (0 == L.length) {            return OK;        } else {            return NO;        }    }}int ListLength_Sq(SqList L) {    return L.length;}Status ListInsert_Sq(SqList *L, int i, ElemType e) {    if ( !L->elem && (i > L->length + 1 || i < 1) ) {        return OVERFLOW;    }    if (L->length + 1 > L->listsize) {        ElemType *newbase = (ElemType *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType));        if (!newbase) {            return OVERFLOW;        }        L->elem = newbase;        L->listsize += LISTINCREMENT;    }    //The pointer tail acts as the iterator end() of vector    //it points an invalid address just after the last available elements    ElemType *head, *tail;    for (head = &L->elem[i - 1], tail = L->elem + L->length; tail != head ; --tail) {        *tail = *(tail - 1);    }    *head = e;    ++L->length;    return OK;} Status ListDelete_Sq(SqList *L, int i, ElemType *e) {    if ( !L->elem && (i < 1 || i > L->length) ) {        return OVERFLOW;    }    //The pointer tail acts as the iterator end() of vector    //it points an invalid address just after the last available elements    ElemType *head, *tail;    head = &L->elem[i];    *e = *head;    for (tail = L->elem + L->length; head != tail; ++head) {        *(head - 1) = *head;    }    --L->length;    return OK;}Status GetElem_Sq(SqList L, int i, ElemType *e) {    if ( !L.elem && (i < 1 || i > L.length) ) {        return OVERFLOW;    }    e = &L.elem[i - 1];    return OK;}int LocateElem_Sq(SqList L, ElemType e) {    if (!L.elem) {        return OVERFLOW;    }    int i;    for (i = 0 ; i < L.length; ++i) {        if (L.elem[i] == e) {            return i + 1;        }    }    return 0;}Status PriorElem_Sq(SqList L, ElemType cur_e, ElemType *pre_e) {    int i = LocateElem_Sq(L, cur_e);    if (1 == i || 0 == i) {        return OVERFLOW;    }    pre_e = &L.elem[i - 2];    return OK;}Status NextElem_Sq(SqList L, ElemType cur_e, ElemType *next_e) {    int i = LocateElem_Sq(L, cur_e);    if(0 == i || L.length == i) {        return OVERFLOW;    }    *next_e = L.elem[i];    return OK;}void ListTraverse_Sq(SqList L, void (*visit)(ElemType)) {    int i;    for (i = 0; i < L.length; ++i) {        visit(L.elem[i]);    }}#endif


----------------------------------------------------------------------------------------------------------------------------------

插入和删除还是很麻烦的。

这里的插入和删除函数主要是靠head和tail两个指针,其中tail指针类似vector中的end()迭代器。


还有一个很囧的问题。

我这个代码用g++编译通过:g++ main.c 完全没问题

但是用gcc编译就报错:

/tmp/cchCMy3W.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

要加上-lstdc++才行。。

但是我觉得我的代码是纯c的啊。。连引用的形参我都全搞成指针了。。

囧啊 。

原创粉丝点击