码迷,mamicode.com
首页 > 编程语言 > 详细

c++单链表操作

时间:2017-02-25 12:08:08      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:include   节点   color   blog   cout   const   create   clu   dex   

/*
 * SingleList.cpp
 *
 *  Created on: Feb 24, 2017
 */

#include <cassert>
#include <iostream>

using namespace std;

struct SingleListNode {
    int val;
    SingleListNode *next;
    SingleListNode(int x) :
            val(x), next(nullptr) {
    }
    SingleListNode(int x, SingleListNode* pn) {
        val = x;
        next = pn;
    }
};

class SingleList {
private:
    SingleListNode *m_head;
    int m_length;
public:
    SingleList() {
        m_head = nullptr;
        m_length = 0;
    }
    ~SingleList() {
        while (m_head) {
            SingleListNode *p = m_head->next;
            delete m_head;
            m_head = p;
        }
    }
    //插入
    void insert(int index, const int Ele) {
        assert(index > 0 && index < m_length + 2);
        if (index == 1) {
            m_head = new SingleListNode(Ele, m_head);
        } else {
            SingleListNode *p = m_head;
            while (index > 2) {
                p = p->next;
                index--;
            }
            p->next = new SingleListNode(Ele, p->next);
        }
        m_length++;
    }

    //删除操作:
    //-1:error 0:correct
    int erase(int index) {
        if (index < 1 || index > m_length) {
            return -1;
        }
        SingleListNode *p = m_head;
        if (index == 1) {
            m_head = m_head->next;
            delete p;
        } else {
            while (index > 2) {
                index--;
                p = p->next;
            }
            SingleListNode* tmp = p->next;
            p->next = p->next->next;
            delete tmp;
        }
        m_length--;
        return 0;
    }
    int getlength() {
        return m_length;
    }

    void print() {
        SingleListNode* p = m_head;
        while (p) {
            cout << p->val << "->";
            p = p->next;
        }
        cout << endl;
    }

    //插入排序
    void insertSort() {
        SingleListNode* fake = new SingleListNode(-1, m_head); //建一个假的节点
        if (m_head->next == nullptr) return;
        SingleListNode* cur = m_head->next;
        SingleListNode* cur_next = cur->next;
        SingleListNode* cur_pre = m_head;
        while (cur) {
            SingleListNode *p = fake;
            while (p->next != cur) {
                if (cur->val < p->next->val) {
                    SingleListNode* tmp = p->next;
                    p->next = cur;
                    cur->next = tmp;
                    cur_pre->next = cur_next;
                    cur = cur_next;
                    if (cur)
                        cur_next = cur->next;
                    break;
                } else {
                    p = p->next;
                }
            }
            if (p->next == cur) {
                cur_pre = cur;
                cur = cur_next;
                if (cur)
                    cur_next = cur->next;
            }
        }
        m_head = fake->next;
    }

    //归并排序
    void mergeSort() {
        m_head = mergeSort(m_head);
    }

    //链表翻转
    void reverse() {
        if (!m_head || !m_head->next)
            return;
        SingleListNode* cur = m_head->next;
        SingleListNode* cur_next;
        m_head->next = nullptr;
        while (cur) {
            cur_next = cur->next;
            cur->next = m_head;
            m_head = cur;
            cur = cur_next;
        }
    }
private:
    SingleListNode* mergeSort(SingleListNode* head) {
        if (head == nullptr || head->next == nullptr)
            return head; //less 2 nodes
        SingleListNode* slow = head;
        SingleListNode* fast = head;
        SingleListNode* pre = head;
        while (fast && fast->next) {
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        pre->next = nullptr;
        cout << slow->val << endl;
        return merge(mergeSort(head), mergeSort(slow));
    }

    SingleListNode* merge(SingleListNode* l1, SingleListNode* l2) {
        SingleListNode* dummy = new SingleListNode(-1);
        SingleListNode* cur = dummy;
        while (l1 && l2) {
            if (l1->val < l2->val) {
                cur->next = l1;
                l1 = l1->next;
            } else {
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }
        if (l1)
            cur->next = l1;
        if (l2)
            cur->next = l2;
        return dummy->next;
    }

};

void insertSort(int a[], int n) {
    int temp;
    for (int i = 1; i < n; i++) {
        temp = a[i];
        int j = i - 1;
        while (j > -1 && temp < a[j]) {
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = temp;
    }
}

int main() {
    SingleList l = SingleList();
    l.insert(1, 2);
    l.insert(1, 3);
    l.insert(1, 1);
    l.insert(2, 0);
    l.print();
    l.mergeSort();
//    l.insertSort();
//    l.mergeSort();
//    l.reverse();
    l.print();
    return 0;
}

 

c++单链表操作

标签:include   节点   color   blog   cout   const   create   clu   dex   

原文地址:http://www.cnblogs.com/wxquare/p/6441202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!