线性表-链表实现

来源:互联网 发布:mysql handlersocket 编辑:程序博客网 时间:2024/06/05 17:32

c++实现的线性表的链表实现,基本操作基本上和数组实现的一样,但是其实为了突出链表的特点,可以增加几个特色操作,比如在第一个位置插入元素,对于链表来说,复杂度是O(1)的。有需要的,可以自己实现。这一系列的都是对新手而言的。自己也是新手。。

/*  Author : Moyiii *  Mail:  lc09@vip.qq.com *  线性表的链表实现,仅作学习之用,当然如果 *  你想拿去用,随你好啦。*/#include<iostream>using namespace std;class Node{public:    int data;    Node* next;};class  LinkList{public:    LinkList();    //析构函数,释放elems占用的内存空间    ~LinkList();    //清空表    void clear();    //判断表是否为空    bool isEmpty();    //获得表的当前元素个数    int getLength();    //在第pos个元素位置之前插入一个新元素    bool insertElem(int pos, int elem);    //删除第pos个元素    bool deleteElem(int pos);    //获得第pos个元素    int& getElem(int pos);    //打印表中元素    void print();private:    Node *head;    int m_length;};//被你发现了,这是个带头节点的链表,为了方便LinkList :: LinkList(){    head = new Node;    if(!head)    {        cout << "Space allocate failed!" << endl;        return;    }    head->data = 0;    head->next = NULL;    m_length =  0;}LinkList :: ~LinkList(){    clear();    delete head;}bool LinkList :: isEmpty(){    if(m_length == 0)    {        return true;    }    else    {        return false;    }}void LinkList :: clear(){    Node *cur;    while(head->next != NULL)    {        cur = head->next;        head->next = cur->next;        delete cur;    }    m_length = 0;}int LinkList :: getLength(){    return m_length;}void LinkList :: print(){    Node *cur = head;    while(cur->next != NULL)    {        cur = cur->next;        cout << cur->data << " ";    }    cout << endl;}bool LinkList :: insertElem(int pos, int elem){    if(pos < 1 || pos > m_length + 1)    {        cout << "Over Bound!" << endl;        return false;    }    Node *cur = head;    //找到插入位置的前一个节点    for(int i = 1; i <= pos - 1; ++i)    {        cur = cur->next;    }    Node* node = new Node;    if(!node)    {        cout << "Space allocate failed!" << endl;        return false;    }    node->data = elem;    node->next = cur->next;    cur->next = node;    m_length++;    return true;}bool LinkList :: deleteElem(int pos){    if(pos < 1 || pos > m_length)    {        cout << "Over Bound!" << endl;        return false;    }    Node *cur = head;    //找到删除位置的前一个节点    for(int i = 1; i <= pos - 1; ++i)    {        cur = cur->next;    }    Node *node = cur->next;    cur->next = node->next;    delete node;    m_length--;    return true;}int& LinkList :: getElem(int pos){    if(pos < 1 || pos > m_length)    {        cout << "Over Bound" << endl;        return head->data;    }    Node *cur = head;    for(int i = 1; i <= pos; ++i)    {        cur = cur->next;    }    return cur->data;}int main(){    LinkList myList;    for(int i = 1; i <= 10; ++i)    {        myList.insertElem(1,i);    }    myList.print();    cout << "Length= " << myList.getLength() <<endl;    myList.deleteElem(5);    myList.print();    cout << "Length= " << myList.getLength() <<endl;    cout << myList.isEmpty() << endl;    int &elem = myList.getElem(3);    elem = 99;    myList.print();    myList.clear();    myList.print();    return 0;}


0 0