首页 > 代码库 > C++__单向链表(练习)

C++__单向链表(练习)

单向链表

link.h

#ifndef LINK_H_
#define LINK_H_

#define HEADER 0
#define TAIL  -1

typedef int data_type;

enum LINK_OP {
    LINK_ERR = -1, LINK_OK
};

class LINK {
private:
    data_type data;
    LINK *next;
public:
    LINK();
    LINK(data_type data);
    virtual ~LINK();

    data_type getData() const;
    LINK *getNext() const;
    void setData(data_type data);
    void setNext(LINK *next);

//    LINK *CreateList();
//    void DestroyList(LINK *pList);
    int InsertItem(LINK *pList, data_type tData, int iOffset);
    int DeleteItem(LINK *pList, data_type *pData, int iOffset);
    int UpdateItem(LINK *pList, data_type tNew, data_type tOld);
    int SearchItem(LINK *pList, data_type tData);
    void ShowList(LINK *pList);
    void operator delete(void *pList);
};

#endif /* LINK_H_ */

 

link.cpp

#include "LINK.h"
#include <iostream>
using namespace std;

LINK::LINK() {
    // TODO Auto-generated constructor stub
    this->setData(0);
    this->setNext(NULL);
}

LINK::LINK(data_type data) {
    // TODO Auto-generated constructor stub
    this->setData(data);
    this->setNext(NULL);
}

LINK::~LINK() {
    // TODO Auto-generated destructor stub
    cout<<"~LINK"<<endl;
}

data_type LINK::getData() const {
    return data;
}

LINK *LINK::getNext() const {
    return next;
}

void LINK::setData(data_type data) {
    this->data =http://www.mamicode.com/ data;
}

void LINK::setNext(LINK *next) {
    this->next = next;
}

/*
 LINK *LINK::CreateList() {
 LINK *pList = new LINK;
 if (!pList)
 return NULL;

 return pList;
 }

void LINK::DestroyList(LINK *pList) {
    if (!pList)
        return;
    LINK *Tmp = pList->getNext();
    while (Tmp) {
        pList->setNext(Tmp->getNext());
        delete Tmp;
        Tmp = pList->getNext();
    }
    delete pList;
    pList = NULL;
}
*/

int LINK::InsertItem(LINK *pList, data_type tData, int iOffset) {
    if (!pList || -1 > iOffset)
        return LINK_ERR;

    LINK *New = new LINK(tData);
    if (!New)
        return LINK_ERR;

    LINK *Tmp = NULL;
    switch (iOffset) {
    case HEADER:
        New->setNext(pList->getNext());
        pList->setNext(New);
        if (!New->getNext())
            pList->setData((data_type) New);
        break;

    case TAIL:
        Tmp = (LINK *) pList->getData();
        Tmp->setNext(New);
        pList->setData((data_type) New);
        break;

    default:
        Tmp = pList->getNext();
        while (iOffset--) {
            if (NULL == Tmp) {
                cout << "iOffset error" << endl;
                return LINK_ERR;
            }
            Tmp = Tmp->getNext();
        }
        New->setNext(Tmp->getNext());
        Tmp->setNext(New);
        if (!New->getNext())
            pList->setData((data_type) New);
        break;
    }

    return LINK_OK;
}

int LINK::DeleteItem(LINK *pList, data_type *pData, int iOffset) {
    if ((!pList) || (!pData) || -1 > iOffset)
        return LINK_ERR;

    LINK *Del = NULL;
    switch (iOffset) {
    case HEADER:
        Del = pList->getNext();
        *pData = http://www.mamicode.com/Del->getData();
        pList->setNext(Del->getNext());
        if (!Del->getNext())
            pList->setData(0);
        break;

    default:
        LINK *Tmp = pList;
        while (iOffset--) {
            if (!Tmp->getNext()) {
                cout << "iOffset error" << endl;
                return LINK_ERR;
            }
            Tmp = Tmp->getNext();
        }
        if (!Tmp->getNext()) {
            cout << "iOffset error" << endl;
            return LINK_ERR;
        }
        Del = Tmp->getNext();
        *pData = http://www.mamicode.com/Del->getData();
        Tmp->setNext(Del->getNext());
        if (!Del->getNext())
            pList->setData(0);
        break;
    }
    Del->setNext(NULL);
    delete Del;

    return LINK_OK;
}

int LINK::UpdateItem(LINK *pList, data_type tNew, data_type tOld) {
    if (!pList)
        return LINK_ERR;

    LINK *Tmp = pList->getNext();
    while (Tmp) {
        if (Tmp->getData() == tOld) {
            Tmp->setData(tNew);
            break;
        }
        Tmp = Tmp->getNext();
    }

    return LINK_OK;
}

int LINK::SearchItem(LINK *pList, data_type tData) {
    if (!pList)
        return LINK_ERR;

    int i = 0;
    LINK *Tmp = pList->getNext();
    while (Tmp) {
        if (Tmp->getData() == tData) {
            cout << "The data is No." << i << endl;
            return i;
        }
        Tmp = Tmp->getNext();
        i++;
    }

    return LINK_OK;
}

void LINK::ShowList(LINK *pList) {
    if (!pList)
        return;

    LINK *Tmp = pList->getNext();
    while (Tmp) {
        cout << Tmp->getData() << "  ";
        Tmp = Tmp->getNext();
    }
    cout << endl;

    return;
}

void LINK::operator delete(void *pList) {
    if (!pList)
        return;

    LINK *Tmp = ((LINK *) pList)->getNext();
    while (Tmp) {
        ((LINK *) pList)->setNext(Tmp->getNext());
        free(Tmp);
        Tmp = ((LINK *) pList)->getNext();
    }
    free(pList);

    return;
}

 

main.cpp

#include "LINK/LINK.h"
#include <iostream>
using namespace std;

void function() {

    LINK *pLink = new LINK;
    if (!pLink) {
        cout << "create error" << endl;
        return;
    }
    int i = 8;
    while (i--) {
        pLink->InsertItem(pLink, i, 0);
    }
    pLink->ShowList(pLink);
    pLink->InsertItem(pLink, 888, 6);
    pLink->ShowList(pLink);
    pLink->InsertItem(pLink, 88, -1);
    pLink->ShowList(pLink);
    pLink->DeleteItem(pLink, &i, 5);
    pLink->ShowList(pLink);
    pLink->DeleteItem(pLink, &i, 0);
    pLink->ShowList(pLink);
    pLink->DeleteItem(pLink, &i, 7);
    pLink->ShowList(pLink);
    pLink->UpdateItem(pLink, 999, 888);
    pLink->ShowList(pLink);
    pLink->SearchItem(pLink, 999);
    pLink->ShowList(pLink);
    //pLink->DestroyList(pLink);
    delete pLink;
}

int main() {
    function();

    return 0;
}

 

C++__单向链表(练习)