C++ 实现链表

转载自: https://blog.csdn.net/starstar1992/article/details/59808706

#include <cstdlib>
#include<iostream>

using namespace std;

typedef struct node{
    int data;
    struct node* next;
}NODE;

class Linklist{
public:
    Linklist(){head = nullptr;}
    ~Linklist();
    bool clearSqList();
    bool isEmpty() {return head == nullptr;};
    int Length();
    bool GetElem(int i, int* e);
    int LocateElem(int e);
    bool PriorElem(int cur_e, int* pre_e);
    bool NextElem(int cur_e, int* next_e);
    bool Insert(int i, int e);
    bool Delete(int i, int* e);
    NODE* Reverse();

private:
    NODE* head;
};



//清空函数
bool Linklist::clearSqList() {
    NODE *p = head;
    while(head){
        p = head;
        head = head->next;
        delete(p);
    }
    return true;
}

//析构函数
Linklist::~Linklist() {
    NODE *p = head;
    while(head){
        p = head;
        head = head->next;
        delete p;
    }
}

//获取链表长度
int Linklist::Length() {
    NODE* p = head;    //不能直接用head循环
    int len = 0;
    while( p!= nullptr){
        len++;
        p = p->next;
    }
    return len;
}

//获取指定位置元素
bool Linklist::GetElem(int i, int *e) {   //*e是返回的元素
    int j = 0;
    NODE* p = head;
    while(j < i && p){
        p = p->next;
        j++;
    }
    if (p == nullptr) return false;
    *e = p->data;
    return true;
}

//查找元素位置
int Linklist::LocateElem(int e) {
    int loc = 0;
    NODE* p = head;
    while(p->data != e && p){
        p = p->next;
        loc++;
    }
    if (p->data == e) return loc;
    else return -1;
}

//获取前驱节点
bool Linklist::PriorElem(int cur_e, int *pre_e) {
    NODE* p = head;
    if (p->data == cur_e) return false;
    while(p->next->data != cur_e && p->next){
        p = p->next;
    }
    if(p->next->data == cur_e) {
        *pre_e = p->data;
        return true;
    }
    else return false;
}

//获取后继节点
bool Linklist::NextElem(int cur_e, int *next_e) {
    NODE* p = head;
    if(head == nullptr || head->next == nullptr) return false;
    while(p->next != nullptr){
        if(p->data == cur_e)
        {
            *next_e = p->next->data;
            return true;
        }
        else
            p = p->next;
    }
    return false;
}

bool Linklist::Insert(int i, int e) {
    NODE* p = head;
    NODE* s = head;
    int loc = 0;
    if(i == 0){
        s = (NODE*)malloc(sizeof(NODE));
        s->data = e;
        s->next = p;
        head = s;
        return true;
    }
    while(p && loc < i - 1){
        p = p->next;
        loc++;
    }
    if(p == nullptr)
        return false;
    s = (NODE*)malloc(sizeof(NODE));
    s->data = e;
    s->next = p->next;
    p->next = s;
    return true;
}

//删除指定位置元素
bool Linklist::Delete(int i, int *e) {
    NODE* p = head;
    int loc = 0;
    if(i == 0){
        *e = head->data;
        head = head->next;
        delete p;
        p = nullptr;
        return true;
    }
    while( p && loc < i-1){
        loc++;
        p = p->next;
    }
    if(p == nullptr)
        return false;
    NODE* s;
    s = p->next;
    p->next = p->next->next;
    *e = s->data;
    delete s;
    s = NULL;
    return true;

    return false;
}

//反转链表
NODE *Linklist::Reverse() {
    if(head == nullptr || head->next == nullptr) return head;
    NODE *p = head, *q = head->next, *r;
    head->next = nullptr;
    while(q){
        r = q->next;
        q->next = p;
        p = q;
        q = r;
    }
    head = p;
    return head;
}



int main()
{
    int a = 0;
    int *p = &a;
    Linklist li;
    li.Insert(0, 5);
    li.Insert(1, 4);
    li.Insert(2, 12);
    li.Insert(3, 5);
    li.Insert(3, 6);
    li.Insert(1, 7);
    cout <<"链表长度"<< li.Length()<<endl;
    cout << "各个元素的值是: ";
    for (int i = 0;i < li.Length();i++)//遍历该链表
    {
        if (li.GetElem(i, p))
            cout << *p<<"   ";
    }
    cout << endl;
    cout << "反转后各个元素的值是: ";
    NODE* re_li=li.Reverse();
    while (re_li)
    {
        cout << re_li->data << "   ";
        re_li = re_li->next;
    }
    cout << endl;
}
posted @ 2018-11-08 15:33  lllittletree  阅读(2052)  评论(0编辑  收藏  举报