单链表作业

来源:互联网 发布:c语言生成随机数 编辑:程序博客网 时间:2024/06/06 02:16

不想再打多一次

放在这里保存算了


#include<iostream>  typedef int ElemType;struct LNode{    ElemType data;    LNode* next;};void InitList(LNode* &HL){    HL = NULL;}void ClearList(LNode*& HL){    LNode *cp;    for (cp = HL; cp != NULL; cp = cp->next)        delete cp;    HL = NULL;}int LenthList(LNode* HL){    int i = 0;    while (HL != NULL)    {        i++;        HL = HL->next;    }    return i;}bool EmptyList(LNode* HL){    return HL == NULL;}ElemType GetList(LNode* HL, int pos){    LNode* cp;    if (pos < 1)    {        std::cerr << "pos is out range!" << std::endl;        exit(1);    }    int i = 0;    for (cp = HL; cp != NULL; cp = cp->next)    {        i++;        if (i == pos) break;    }    if (cp != NULL)        return cp->data;    else    {        std::cerr << "pos is out range!" << std::endl;        exit(1);    }}void TraverseList(LNode* HL){    LNode* cp;    for (cp = HL; cp != NULL; cp = cp->next)        std::cout << cp->data << " ";    std::cout << std::endl;}bool FindList(LNode* HL, ElemType& item){    LNode* cp;    for (cp = HL; cp != NULL; cp = cp->next)        if (item == cp->data)        {            item = cp->data;            return true;        }    return false;}bool UpdateList(LNode* HL, const ElemType& item){    LNode* cp;    for (cp = HL; cp != NULL; cp = cp->next)        if (item == cp->data)        {            cp->data = item;            return true;        }    return false;}bool InsertList(LNode* &HL, ElemType item, int pos){    if (pos < -1)    {        std::cout << "pos值无效!" << std::endl;        return false;    }    LNode* newptr;    newptr = new LNode;    newptr->data = item;    LNode* cp = HL;    LNode* ap = NULL;    if (pos == 0)    {        for (; cp != NULL; ap = cp, cp = cp->next)            if (item < cp->data) break;    }    else if (pos == -1)        for (; cp != NULL; ap = cp, cp = cp->next)        {            ;        }    else    {        int i = 0;        for (; cp != NULL; cp = cp->next)        {            i++;            if (i == pos) break;        }        if (cp == NULL&&i + 1 < pos)        {            std::cout << "pos值超出单链表长度加一!" << std::endl;            return false;        }    }    if (ap == NULL)    {        newptr->next = HL;        HL = newptr;    }    else    {        newptr->next = cp;        ap->next = newptr;    }    return true;}bool DeleteList(LNode* &HL, ElemType& item, int pos){    if (HL == NULL)    {        std::cerr << "单链表为空,删除操作无效!" << std::endl;        return false;    }    if (pos < -1)    {        std::cout << "pos值无效!" << std::endl;        return false;    }    LNode* cp = HL;    LNode* ap = NULL;    if (pos == 0)    {        for (; cp != NULL; ap=cp,cp = cp->next)        {            if (item == cp->data) break;        }        if (cp == NULL)        {            std::cout << "单链表中没有相应的结点可删除!" << std::endl;            return false;        }    }    else if (pos == -1)        for (; cp->next!= NULL;ap=cp, cp = cp->next)        {            ;        }    else    {        int i = 0;        for (; cp != NULL; ap=cp,cp = cp->next)        {            i++;            if (i == pos) break;        }        if (cp == NULL)        {            std::cout << "pos值无效!" << std::endl;            return false;        }    }    if (ap == NULL)        HL = HL->next;    else ap->next = cp->next;    delete cp;    return true;}void SortList(LNode* &HL){    LNode* SL;    InitList(SL);    LNode* r = HL;    while (r != NULL)    {        LNode* t = r->next;        LNode* cp = SL;        LNode* ap = NULL;        for (; cp != NULL; ap = cp, cp = cp->next)            if (r->data < cp->data) break;        if (ap == NULL)        {            r->next = SL;            SL = r;        }        else        {            r->next = cp;            ap->next = r;        }        r = t;    }    HL = SL;}void main(){    int a[12], i, x;    int item = 5;    LNode *L;    InitList(L);    for (i = 0; i < 5; i++)    {        std::cin >> a[i];        InsertList(L, a[i], i + 1);    }    TraverseList(L);    std::cout << "表长为:" << LenthList(L) << std::endl;    std::cout << "在表头添加一个元素56:";    InsertList(L, 56, 1);    TraverseList(L);    std::cout << "在表尾添加一个元素77:";    InsertList(L, 77, -1);    TraverseList(L);    std::cout << "删除表头元素:";    DeleteList(L, x, 1);    TraverseList(L);    std::cout << "删除表尾元素:";    DeleteList(L, x, -1);    TraverseList(L);    std::cout << "删除表中所有值为5的元素:";    for (i = 0; i < 2; i++)        DeleteList(L, item, 0);    TraverseList(L);    system("pause");}