美文网首页
顺序表链表实现

顺序表链表实现

作者: 榆杨丶 | 来源:发表于2020-06-11 15:44 被阅读0次

    项目相关代码在码云托管

    #include"list.h"
    #include<iostream>
    using namespace std;
    list::list()
    {
        m_pList=new Node;
        m_pList->data=0;
        m_pList->next=NULL;
        m_pLength=0;
    }
    
    bool list::IsEmpty()
    {
        if(m_pLength==0)
            return true;
        return false;
    }
    int list::listLength()
    {
        return m_pLength;
    }
    void list::ClearList()
    {
        Node *currentNode=m_pList->next;
        while(currentNode!=NULL)
        {
            Node *temp=currentNode->next;
            delete currentNode;
            currentNode=temp;
        }
        m_pList->next=NULL;
    }
    list::~list()
    {
        ClearList();
        delete m_pList;
        m_pList=NULL;
    }
    bool list::ListInsertHead(Node *pNode)
    {
        Node *temp=new Node;
        if(temp==NULL) return false;
        temp->data=pNode->data;
        temp->next=m_pList->next;
        m_pList->next=temp;
        m_pLength++;
        return true;
    }
    bool list::ListInsertTail(Node *pNode)
    {
        Node* temp=new Node;
        if(temp==NULL)
            return false;
        temp->data=pNode->data;
        temp->next=NULL;
        Node* currentNode=m_pList;
        while (currentNode->next!=NULL)
        {
            currentNode=currentNode->next;
        }
        currentNode->next=temp;
        m_pLength++;
        return true;
    }
    bool list::ListInsert(int i,Node* pNode)
    {
        if(i<0||i>m_pLength)
            return false;
        Node* temp=new Node;
        if(temp==NULL)
            return false;
        temp->data=pNode->data;
        Node* currentNode=m_pList;
        for (int k = 0; k <i; k++)
        {
            currentNode=currentNode->next;
        }
        temp->next=currentNode->next;
        currentNode->next=temp;
        m_pLength++;
        return true;
    
    }
    bool list::ListDelte(int i,Node* pNode)
    {
        if(i<0||i>=m_pLength)
            return false;
        Node* currentNode=m_pList;
        for (int k = 0; k <i; k++)
        {
            currentNode=currentNode->next;
        }
        Node* temp=currentNode->next;
        currentNode->next=currentNode->next->next;
        pNode->data=temp->data;
        delete temp;
        temp=NULL;
        m_pLength--;
        return true;
    }
    bool list::GetElem(int i,Node* pNode)
    {
        if(i<0||i>=m_pLength)
            return false;
        Node* currentNode=m_pList;
        for (int k = 0; k <=i; k++)
        {
            currentNode=currentNode->next;
        }
        pNode->data=currentNode->data;
        return true;
    }
    int list::LocateElem(Node* pNode)
    {
        Node* currentNode=m_pList->next;
        int count=0;
        while (currentNode!=NULL)
        {
            if(currentNode->data==pNode->data)
                return count;
            count++;
            currentNode=currentNode->next;
    
        }
        return -1;
    }
    bool list::PriorElem(Node* currentNode,Node* preNode)
    {
        Node* temp=m_pList;
        while (temp->next!=NULL)
        {
            if(temp->next->data==currentNode->data)
            {
                if(temp==m_pList)
                    return false;
                preNode->data=temp->data;
                return true;
            }
            temp=temp->next;
        }
        return false;
    }
    
    bool list::NextElem(Node* currentNode,Node* nextNode)
    {
        Node* temp=m_pList->next;
        while (temp!=NULL)
        {
            if(temp->data==currentNode->data)
            {
                if(temp->next==NULL) return false;
                nextNode->data=temp->next->data;
                return true;
            }
            temp=temp->next;
        }
        return false;
    }
    void list::ListTraverse()
    {
        Node* currentNode=m_pList;
        while (currentNode->next!=NULL)
        {
            currentNode=currentNode->next;
            currentNode->printNode();
        }
    }
    

    相关文章

      网友评论

          本文标题:顺序表链表实现

          本文链接:https://www.haomeiwen.com/subject/pmvbtktx.html