資料結構之鏈表篇(單鏈表的常見操作)

來源:互聯網
上載者:User

標籤:rev   ||   反轉   new   list()   font   tle   printf   list   

單鏈表的常見操作:

  1. 鏈表的定義
  2. 鏈表的初始化
  3. 鏈表的建立
  4. 測表長
  5. 鏈表插入
  6. 鏈表刪除
  7. 列印鏈表
  8. 反轉鏈表
// 鏈表的定義struct linkList {    int value;    linkList *next;};// 鏈表的初始化void initList(linkList *L){    L = nullptr;}// 鏈表的建立(頭插入法)linkList* createList(){    linkList * head = new linkList;    head->next = nullptr;    if (head == nullptr)    {        std::cout << "分配記憶體失敗!" << std::endl;        return nullptr;    }    int input;    std::cout << "輸入鏈表值:" << std::endl;    while (cin >> input && input != -1)    {        linkList *tmp = new linkList;        tmp->value = input;        tmp->next = head->next;        head->next = tmp;        tmp = nullptr;    }    return head;}// 測表長int listLen(linkList *L){        int length = 0;    while (L != nullptr)    {        length++;        L = L->next;    }    //std::cout << "表長為:" << length - 1 << std::endl;    return length-1;}// 鏈表插入void insertList(linkList *L, int pos, int value){    int index = 1;    linkList *p = L;    linkList *tmp = new linkList;    tmp->value = value;    if (pos <= 0 || pos > listLen(L) + 1)    {        printf("輸入的位置不合法,無法插入!");        return;    }    else if (pos == listLen(L) + 1)    {        while (p->next != NULL)        {            p = p->next;        }        tmp->next = nullptr;        p->next = tmp;        return;    }    else    {        while (index < pos)        {            index++;            p = p->next;        }        tmp->next = p->next;        p->next = tmp;    }}// 鏈表刪除void deleteList(linkList *L, int pos){    int index = 1;    if (pos <= 0 || pos > listLen(L))    {        printf("輸入的位置不合法,無法刪除!\n");        return;    }    else    {        while (index < pos)        {            index++;            L = L->next;        }        L->next = L->next->next;    }}// 列印鏈表void print(linkList *L){    if (L->next == nullptr)    {        printf("鏈表中無元素!");        return;    }    else    {        linkList *tmp = new linkList;        tmp = L->next;        while (tmp != NULL)        {            printf("%d->", tmp->value);            tmp = tmp->next;        }        printf("NULL\n");    }}// 反轉鏈表linkList* reverseList(linkList *L){    if (L == nullptr)        return nullptr;    linkList *rev = new linkList;    rev->next = nullptr;    while (L->next != nullptr)    {        L = L->next;        linkList *tmp = new linkList;        tmp->value = L->value;        tmp->next = rev->next;        rev->next = tmp;        tmp = nullptr;    }    return rev;}int main(int argc,int* argv[]){    linkList *L = new linkList;    initList(L);    L = createList();    print(L);    insertList(L, 4, 10);    print(L);    deleteList(L, 5);    print(L);    linkList *rev = new linkList;    rev->next = nullptr;    rev = reverseList(L);    print(rev);    while (true)    {        getchar();    }    return 0;}

 

資料結構之鏈表篇(單鏈表的常見操作)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.