链表的各种操作

#include <iostream>
#include <stdlib.h>

using namespace  std;

struct Node
{
    int data;
    Node* next;
};

using PNODE = Node*;

PNODE createList()
{
    int len;
    int val;

    PNODE pHead = (PNODE)malloc(sizeof(Node));
    if(pHead == nullptr)
    {
        cout <<" malloc memory fail,and exit.";
        exit(-1);
    }
    PNODE pTail = pHead;
    pTail->next = nullptr;
    cout<<"please Input ListNode length:";
    scanf("%d", &len);


    for(int i = 0;i < len; i++)
    {
        cout <<"please Input "<< i+1 <<"th node val:";
        scanf("%d",&val);

        PNODE pNew = (PNODE)malloc(sizeof(Node));
        if(pNew == nullptr)
        {
            cout <<" malloc"<< i+1 <<"th node fail,and exit.";
            exit(-1);
        }
        pNew->data = val;
        pTail->next = pNew;
        pNew->next = nullptr;
        pTail = pNew;
    }

    return pHead;
}

PNODE traverseList(PNODE pHead)
{
    PNODE p = pHead->next;
    while (p != nullptr)
    {
        cout << p->data << " ";
        p = p->next;
    }
    cout << endl;
}

bool isListNodeEmpty(PNODE pHead)
{
    return pHead->next == nullptr;
}

int LengthListNode(PNODE pHead)
{
    PNODE p = pHead->next;
    int length = 0;
    while (p != nullptr)
    {
        ++length;
        p = p->next;
    }

    return length;
}

void sortList(PNODE pHead)
{
    PNODE p,q;
    for(p = pHead->next; p != nullptr; p=p->next)
    {
        for(q = p->next; q != nullptr; q = q->next)
        {
            if(p->data > q->data)
            {
                swap(p->data, q->data);
            }
        }
    }
}

bool insertList(PNODE pHead, int pos, int val)
{
    if(pHead == nullptr)
    {
        return false;
    }

    PNODE current = pHead;
    int j = 0;
    while(current != nullptr and j < pos - 1)
    {
        current = current->next;
        j++;
    }

    if(current == nullptr or j > pos - 1)
    {
        return false;
    }

    PNODE s  = (PNODE)malloc(sizeof(Node));
    if(s == nullptr)
    {
        cout <<" malloc memory fail,and exit.";
        return false;
    }

    s->data = val;
    s->next = current->next;
    current->next = s;
    
    return true;
}

bool delElement(PNODE pHead, int val)
{
    PNODE pTmp = pHead;
    PNODE delNode;
    //delNode = findNode(pHead, positionData);
    while (pTmp->data != val and pTmp->next)
    {
        delNode = pTmp;
        pTmp = pTmp->next;
    }
    
    if(pTmp->data == val)
    {  
        if(pTmp == pHead)
        {
            pHead = pHead->next;
        }
        else
        {
            delNode->next = pTmp->next;    
        }
        free(pTmp);
        pTmp = nullptr;
        return true;
    }
    else
    {
        cout <<"can't find " <<val<<" in ListNode"<<endl;
        return false;
    }
}

int main()
{
    PNODE pHead = nullptr;
    pHead = createList();
    traverseList(pHead);
    cout << isListNodeEmpty(pHead) << endl;
    cout << LengthListNode(pHead) << endl;
    sortList(pHead);
    traverseList(pHead);
    insertList(pHead, 2, 19);
    traverseList(pHead);

    delElement(pHead, 4);
    traverseList(pHead);
    getchar();
    return 0;
}
posted @ 2021-10-25 21:20  Kiris  阅读(58)  评论(0)    收藏  举报