C++靜態鏈表

來源:互聯網
上載者:User

標籤:靜態鏈表

//靜態鏈表,我覺得刪除,排序,插入有意思。#include <iostream>#define _MAX_ 0x7fffffffusing namespace std;template<typename Type>struct Node{    Type data;//儲存的值    int cour;//下標,只用一個下標來解決問題。};template<typename Type>class GList{public:    GList(int n ,Type a[])    {        node = new Node<Type>[n];        size = n;        space = n;//空間容量大小。        int i = 0;        for (; i < size; i++)        {            node[i].cour = i;//將下標賦值。            node[i].data = a[i];//將值賦值。        }    }    void Sort()//排序。    {        int *save = new int[size];//記錄下標是否改變。        int i = 0;        for (; i < size; i++)        {            save[i] = 0;//記錄的初始值都設定為0。        }        i = 0;        int j;        int tmp;        int m = size - 1;        int flags = 0;//記錄最小位置。        int k = 0;        while (m>=0)        {            for (; i < size; i++)//找到剩餘第一個沒有排序的。            {                if (save[i] == 0)                    break;            }            if (i >= size)return;//沒有找到說明排序結束了。            tmp = node[i].data;            for (j = i + 1; j < size; j++)            {                if (tmp > node[j].data && save[j] == 0)                {                    flags = j;                    tmp = node[j].data;//交換比較值。                }            }            node[flags].cour = k++;//賦值下標。            save[flags] = 1;//標記。            flags = i;//這裡必須讓flags還原,如果它沒有進入迴圈的話,那麼這個就可以起作用了。            i = 0;//從0位置開始找。            m--;        }        delete[] save;        save = NULL;    }    void GetSpace()    {        space *= 2;//開闢兩倍的空間。        Node<Type>* tmp = new Node<Type>[space];        int i = 0;        for (;i<size;i++)        {            tmp[i].data = node[i].data;            tmp[i].cour = i;        }        delete[]node;        node = NULL;        node = tmp;    }    void Insert(int x)    {        //既然要插入元素,肯定是在靜態鏈表後面插入,且第一次肯定空間是不足的,所以        //在這裡我選擇開闢兩倍的空間並且將原來的資料拷貝過來。        if (space <= size)            GetSpace();        int i = 0;        node[size].data = x;        node[size].cour = size;        size++;        Sort();//這裡直接調用這個函數,我感覺比較無恥。        //如果不調用這個排序函數,應該怎麼做呢?挺麻煩的。    }    void Delete(int x)    {        int i = 0;        for (; i < size; i++)        {            if (x == node[i].data)            {                node[i].data = node[size - 1].data;                node[i].cour = node[size - 1].cour;//用最後一個覆蓋這個位置的值。                size--;            }        }        for (i = 0; i < size; i++)        {            node[i].cour = i;        }        Sort();    }    void Printf()//列印。    {        int i = 0;        for (; i < size; i++)        {            cout << node[i].data << "    ";        }        cout << endl;        for (i=0; i < size; i++)        {            cout << node[i].cour << "    ";        }        cout << endl;    }private:    Node<Type> *node;    int size;    int space;};int main(){    int a[] = { 2, 8, 4,0, 1 };    GList<int> gl(5 ,a);    gl.Sort();    gl.Printf();    cout << "-----------------" << endl;    gl.Insert(-1);    gl.Insert(5);    gl.Printf();    cout << "-----------------" << endl;    gl.Delete(4);    gl.Printf();    return 0;}

C++靜態鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.