單鏈表作業

不想再打多一次

放在這裏保存算了


#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");
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章