博客
关于我
C++实现单链表基本操作
阅读量:116 次
发布时间:2019-02-26

本文共 4603 字,大约阅读时间需要 15 分钟。

C++?????????

?????????????????????????????????????????????????????????????????????????????????????????????

????????

#define OK     1#define ERROR  0typedef int Status;typedef char ElemType;

???????

typedef struct LNode {    ElemType data;    struct LNode *next;} LNode, *LinkList;LinkList L;  // ??????

?????

Status InitList(LinkList &L) {    L = new LNode;  // ?????    L->next = NULL;  // ??????????????    return OK;}

????

Status DestroyList(LinkList &L) {    while (L) {  // ???????        LinkList p = L;  // ??????        L = L->next;  // ????????        delete p;  // ?????????    }    return OK;}

????

Status ClearList(LinkList &L) {    if (!L) {  // ????        return OK;    }    LinkList p = L->next;  // ???????????    while (p) {  // ???????        LinkList q = p->next;  // ????????????        delete p;  // ??????        p = q;  // ????????    }    L->next = NULL;  // ??????????????    return OK;}

????????

int ListEmpty(LinkList L) {    return L->next ? 0 : 1;  // ??????????0?????1}

??????

int ListLength(LinkList L) {    int count = 0;    LinkList p = L->next;  // ??????????    while (p) {        count++;        p = p->next;    }    return count;}

???????i???

Status GetElem(LinkList L, int i, ElemType &e) {    if (i <= 0 || i > ListLength(L)) {        return ERROR;    }    LinkList p = L;    int j = 0;    while (p && j < i - 1) {        p = p->next;        j++;    }    if (!p || j >= i - 1) {        return ERROR;    }    e = p->data;    return OK;}

??????

Status LocateElem(LinkList L, ElemType &e) {    if (!L) {  // ????        return 0;    }    LinkList p = L;    int j = 0;    while (p && p->data != e) {        p = p->next;        j++;    }    if (p) {        return j;    } else {        return 0;    }}

?????

Status ListInsert(LinkList &L, int i, ElemType e) {    if (i <= 0 || i > ListLength(L) + 1) {        return ERROR;    }    LinkList p = L;    int j = 0;    while (p && j < i - 1) {        p = p->next;        j++;    }    if (!p || j >= i - 1) {        return ERROR;    }    LinkList s = new LNode;    s->data = e;    s->next = p->next;    p->next = s;    return OK;}

???????i???

Status ListDelete(LinkList &L, int i, ElemType &e) {    if (i <= 0 || i > ListLength(L)) {        return ERROR;    }    LinkList p = L;    int j = 0;    while (p && j < i - 1) {        p = p->next;        j++;    }    if (!p || j >= i - 1) {        return ERROR;    }    LinkList q = p->next;    e = q->data;    p->next = q->next;    delete q;    return OK;}

??????????

void CreateList_H(LinkList &L, int n) {    L = new LNode;  // ?????    L->next = NULL;  // ??????????????    for (int i = n; i > 0; --i) {        LinkList p = new LNode;  // ?????        cin >> p->data;  // ????        p->next = L->next;  // ????????        L->next = p;  // ???????????    }}

??????????

void CreateList_R(LinkList &L, int n) {    L = new LNode;  // ?????    L->next = NULL;  // ??????????????    LinkList r = L;  // ??????????    for (int i = 0; i < n; ++i) {        LinkList p = new LNode;  // ?????        cin >> p->data;  // ????        p->next = NULL;  // ??????????????        r->next = p;  // ?????????        r = p;  // ?????    }}

??????

int main() {    int ret = InitList(L);    if (ret) {        cout << "???????" << endl;    } else {        cout << "???????" << endl;    }    int i;    cout << "?????????";    cin >> i;    cout << "???" << i << "??????" << endl;    CreateList_H(L, i);  // ????    // CreateList_R(L, i);  // ????    cout << "????????" << ListLength(L) << endl;    ElemType e;    cout << "?????????????";    cin >> i;    GetElem(L, i, e);    cout << "???" << i << "??????" << e << endl;    cout << "?????????";    cin >> e;    LocateElem(L, e);    cout << "??" << e << "?????" << j << endl;    cout << "?????????????";    cin >> e >> i;    ret = ListInsert(L, i, e);    if (ret) {        cout << "????" << endl;    } else {        cout << "????" << endl;    }    cout << "???????????" << ListLength(L) << endl;    cout << "?????????????";    cin >> i;    ret = ListDelete(L, i, e);    if (ret) {        cout << "????" << endl;    } else {        cout << "????" << endl;    }    cout << "?????" << endl;    ClearList(L);    if (ListEmpty(L)) {        cout << "?????" << endl;    } else {        cout << "?????" << endl;    }    DestroyList(L);    cout << "??????" << endl;    return 0;}

????

  • ????????InitList????????????
  • ???????DestroyList??????????
  • ???????ClearList??????????????
  • ???????GetElem????????????????
  • ???????LocateElem???????????????
  • ???????ListInsert?????????????
  • ???????ListDelete????????????????
  • ????????CreateList_H?????????????CreateList_R?????????????
  • 转载地址:http://yvoy.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现2 个数字之间的算术几何平均值算法(附完整源码)
    查看>>
    Objective-C实现3n+1猜想(附完整源码)
    查看>>
    Objective-C实现A-Star算法(附完整源码)
    查看>>
    Objective-C实现all combinations所有组合算法(附完整源码)
    查看>>
    Objective-C实现An Armstrong number阿姆斯特朗数算法(附完整源码)
    查看>>
    Objective-C实现anagrams字谜算法(附完整源码)
    查看>>
    Objective-C实现ApproximationMonteCarlo蒙特卡洛方法计算pi值算法 (附完整源码)
    查看>>
    Objective-C实现area under curve曲线下面积算法(附完整源码)
    查看>>
    Objective-C实现armstrong numbers阿姆斯壮数算法(附完整源码)
    查看>>
    Objective-C实现atoi函数功能(附完整源码)
    查看>>
    Objective-C实现average absolute deviation平均绝对偏差算法(附完整源码)
    查看>>
    Objective-C实现average mean平均数算法(附完整源码)
    查看>>
    Objective-C实现base64加密和base64解密算法(附完整源码)
    查看>>
    Objective-C实现base85 编码算法(附完整源码)
    查看>>
    Objective-C实现basic graphs基本图算法(附完整源码)
    查看>>
    Objective-C实现BCC校验计算(附完整源码)
    查看>>
    Objective-C实现bead sort珠排序算法(附完整源码)
    查看>>
    Objective-C实现BeadSort珠排序算法(附完整源码)
    查看>>
    Objective-C实现bellman ford贝尔曼福特算法(附完整源码)
    查看>>
    Objective-C实现bellman-ford贝尔曼-福特算法(附完整源码)
    查看>>