单链表总结

来源:互联网 发布:免费横道图绘制软件 编辑:程序博客网 时间:2024/06/04 19:39
#include <iostream>#include <vector>#include <string>using namespace std;void print(int a[], int n){    for (int i = 0; i < n; i++)        cout << a[i] << " ";    cout << endl;}//直接插入排序void insert_sort(int a[], int n){    int i, j, temp;    for (i = 1; i < n; i++)    {        //暂存下标为i的数        temp = a[i];        for (j = i - 1; j >= 0 && a[j]>temp; j--) {             a[j + 1] = a[j];        }        a[j + 1] = temp;    }}void shell_sort(int a[],int n){    int h, i, j, temp;//h是增量    for (h = n / 2; h > 0; h = h / 2)    {        for (i = h; i < n; i++)//下面是一个插入排序,0~h-1时在各自组的第一位,认为是最好的排序        {            temp = a[i];            for (j = i - h; j >= 0 && a[j] > temp; j -= h)                a[j + h] = a[j];            a[j + h] = temp;        }    }}void bubble_sort(int a[], int n){    int i, j, temp, exchange = 0;    for (i = 0; i < n; i++) {        exchange = 0;        for (j = n-1;j>i; j--)        {            if (a[j] < a[j - 1])            {                temp = a[j];                a[j] = a[j - 1];                a[j - 1] = temp;                exchange = 1;            }        }        if (exchange != 1)            return;    }}void quick_sort(int a[], int low, int high){    int i, j, pivot;    if (low < high)    {        pivot = a[low];        i = low;        j = high;        while (i < j)        {            //从后往前搜索(j--),找到第一个小于pivot的a[j],交换a[i]与a[j];            while (i < j&&a[j] >= pivot)                j--;            if (i < j)                a[i++] = a[j];//将比pivot小的元素移到低端            //从前往后找(i++),找到第一个大于pivot的a[i],交换a[i]与a[j]            while (i < j && a[i] <= pivot)                i++;            if (i < j)                a[j--] = a[i];//将比pivot大的元素移到高端        }        a[i] = pivot;        quick_sort(a, low, i - 1);        quick_sort(a, i + 1, high);    }}void select_sort(int a[], int n){    int i, j, x, l;    for (i = 0; i < n - 1; i++)    {        x = a[i];        l = i;        for (j = i; j < n; j++)        {            if (a[j] < x)            {                x = a[j];                l = j;            }        }        a[l] = a[i];//将最小元素与a[i]交换        a[i] = x;    }  }//以下开始是堆排序int heap_size = 0;int Left(int idx) { return ((idx<<1)+1); }int Right(int idx) { return ((idx<<1)+2); }//a[idx]与其左右子树进行递归对比,用最大值替换a[idx]void maxHeapify(int a[], int idx){    int largest = 0;    int left = Left(idx), right = Right(idx);    if ((left <= heap_size) && (a[left] > a[idx]))        largest = left;    else largest = idx;    if ((right <= heap_size) && (a[right] > a[largest]))        largest = right;    //此时largest为堆顶、左子节点、右子节点中的最大者    if (largest != idx)    {        swap(a[largest], a[idx]);        maxHeapify(a, largest);    }}//初始化堆,将数组中的每一个元素放到适当的位置//完成之后,堆顶的元素为数组的最大值void buildMaxHeap(int a[], int n){    int i=0;    heap_size = n ; //堆大小为数组长度    for (i = (n >> 1); i >= 0; i--)        maxHeapify(a, i);}void heap_sort(int a[], int n){    int i=0;    //初始化堆    //cout << "stop" << endl;    buildMaxHeap( a, (n-1));    //cout << "stop" << endl;    for (i = (n - 1); i > 0; i--)    {        //堆顶元素a[0](数组的最大值)被置换到数组的尾部a[i]        swap(a[0], a[i]);        heap_size--;//从堆中移除该元素        maxHeapify(a, 0);//重建堆    }}//下面是归并排序void merge(int a[], int temp[], int lpos, int rpos, int rend){    int i, lend, numEles, tmpPos;    lend = rpos - 1;    tmpPos = lpos;    numEles = rend - lpos + 1;    while (lpos <= lend && rpos <= rend)    {        if (a[lpos] < a[rpos])            temp[tmpPos++] = a[lpos++];        else            temp[tmpPos++] = a[rpos++];    }    while (lpos <= lend)        temp[tmpPos++] = a[lpos++];    while (rpos <= rend)        temp[tmpPos++] = a[rpos++];    //把临时数组拷贝到原始数组    for (i = 0; i < numEles; i++, rend--)        a[rend] = temp[rend];}void msort(int a[], int tmp[], int low, int high){    if (low >= high) return;    int mid = (low + high) / 2;    msort(a, tmp, low, mid);    msort(a, tmp, mid + 1, high);    merge(a, tmp, low, mid + 1, high);}void merge_sort(int a[], int n){    int *tmp = NULL;    tmp = new int[n];    if (tmp != NULL) {        msort(a, tmp, 0, n - 1);        delete[]tmp;    }}//下面是基数排序int find_max(int a[], int n){    int max = a[0];    for (int i = 1; i < n; i++)        if (max < a[i])            max = a[i];    return max;}int digit_num(int num){    int digit = 0;    do {        num /= 10;        digit++;    } while (num != 0);        return digit;}int kth_digit(int num, int k)//第k位的数字{    num /= pow(10, k);    return num % 10;}void radix_sort(int a[], int n){    int *temp[10];//指针数组,每一个指针表示一个箱子    int count[10] = { 0,0,0,0,0,0,0,0,0,0 };    int max = find_max(a, n);    int maxDigit = digit_num(max);    int i, j, k;    for (i = 0; i < 10; i++)    {        temp[i] = new int[n];        memset(temp[i], 0, sizeof(int) * n);    }    for (i = 0; i < maxDigit; i++)    {        memset(count, 0, sizeof(int) * 10);        for (j = 0; j < n; j++)        {            int xx = kth_digit(a[j], i);            temp[xx][count[xx]] = a[j];            count[xx]++;        }        int index = 0;        //把数据从暂存数组中返回到原始数组        for (j = 0; j < 10; j++)            for (k = 0; k < count[j]; k++)                a[index++] = temp[j][k];    }}//================================================================================/*以下是关于单链表的操作*/struct node {    int data;    node* next;    node() { data = 0; next = nullptr; }    node(int x) { data = x; next = nullptr; }};//下面是创建一个单链表node *creat(int a[],int n){    int i = 0;    node *head, *p, *q;    head = new node();    p = q = nullptr;    int x = 0;    cout << "please input the data ended with 0: ";    while (1)    {        //cin >> x;        x = a[i++];        if (x == 0) //data为0时创建结束            break;        p = new node(x);        if (head->next == nullptr)            head->next = p;        else q->next = p;        q = p;//q始终指向最后一个元素    }    return head;}//下面是单链表的测长int length(node* head){    int len = 0;    node* p = nullptr;    p = head->next;    while (p != nullptr)    {        len++;        p = p->next;    }    return len;}//打印单链表void print(node * head){    node * p = nullptr;    int index = 0;    p = head->next;    if (p == nullptr)    {        cout << "empty link!" << endl;        return;    }    while (p != nullptr)    {        cout << "The " << ++index << "th node is :" << p->data << endl;        p = p->next;    }}//查找单链表pos位置的节点,返回该节点的指针//pos从0开始,0返回headnode * search_node(node * head, int pos){    node* p = head->next;    if (pos < 0)    {        cout << "incorrect position to search node!\n";        return nullptr;    }    if (pos == 0)        return head;    if (p == nullptr)    {        cout << "Link is empty!\n";        return nullptr;    }    while (--pos)//head->next是第一个    {        if ((p = p->next) == nullptr)        {            cout << "incorrect position to search node!\n";            break;        }    }    return p;}//向单链表的某个位置插入节点,第0个节点是插在链表首部node *insert_node(node* head, int pos, int data){    node * item = new node(data);    node * p = head->next;    if (pos == 0)    {        item->next = p;        head->next = item;        return head;    }    p = search_node(head, pos);//获得位置pos的节点指针    if (p != nullptr)    {        item->next = p->next;        p->next = item;    }    return head;}//单链表的删除,删除pos位置的节点,返回链表头指针//pos从1开始,1表示删除head后的第一个节点node* delete_node(node* head, int pos){    node* item = nullptr;    node* p = head->next;    if (p == nullptr)    {        cout << "Link is empty!\n";        return nullptr;    }    p = search_node(head, pos - 1);    if (p != nullptr && p->next != nullptr)    {        item = p->next;        p->next = item->next;        delete item;    }    return head;}//实现单链表的逆置node *reverse1(node* head){    node* p = head->next;    if (p == nullptr || p->next == nullptr)        return head;    node *q=p->next,*nxt=nullptr;    //重要    p->next = nullptr;    while (q)    {        nxt = q->next;        q->next = p;        p = q;        q = nxt;    }    head->next = p;    return head;     }//一次遍历,寻找链表的中间节点node * search_mid(node* head){    node* slow = nullptr, *fast = nullptr;    slow = fast=head->next;    while (fast->next!=nullptr && fast->next->next!=nullptr) {        slow = slow->next;        fast = fast->next->next;    }    return slow;}//单链表的正向排序node* merge(node* h1, node* h2){    if (h1 == nullptr) return h2;    if (h2 == nullptr) return h1;    node* h = new node(0), *p = h;    while (h1&&h2)    {        if (h1->data < h2->data) {            h->next = h1;            h1 = h1->next;        }        else {            h->next = h2;            h2 = h2->next;        }        h = h->next;    }    while (h1 != nullptr) {        h->next = h1;        h1 = h1->next;        h = h->next;    }    while (h2 != nullptr)    {        h->next = h2;        h2 = h2->next;        h = h->next;    }    return p->next;}node* merge_sort(node* head){    if (head == nullptr || head->next == nullptr) return head;    node * mid ,*slow,*fast;    slow = fast = head;    while (fast->next&&fast->next->next)    {        slow = slow->next;        fast = fast->next->next;    }    mid = slow;    node* h2 = mid->next;    mid->next = nullptr;    node* h1, *h;    h1=merge_sort(head);    h = merge_sort(h2);    head=merge(h1, h);    return head;}int main() {    int a[] = { 22,32,19,53,47,29 ,0};    cout << "建表:" << endl;    node* head=creat(a,7);    cout << "链表长度:" << length(head) << endl;    cout << "打印链表:" << endl;    print(head);    cout << "找到第6个元素:";    node* tmp = search_node(head, 6);    cout << tmp->data << endl;    cout << "在第3个位置后插入data=8的节点:" << endl;    head = insert_node(head, 3, 8);    cout << "打印链表:" << endl;    print(head);    cout << "删除第1个位置的节点:" << endl;    head = delete_node(head, 1);    cout << "打印链表:" << endl;    print(head);    cout << "反转链表:" << endl;    head = reverse1(head);    print(head);    cout << "找到中间元素:" << endl;    node* mid = search_mid(head);    cout << mid->data << endl;    cout << "链表排序:" << endl;    head = merge_sort(head);    print(head);    system("pause");    return 0;}
0 0
原创粉丝点击