資料結構--鏈表

來源:互聯網
上載者:User

標籤:ret   std   並且   pac   nod   type   tin   include   struct   

#include <iostream>using namespace std;typedef struct LNode{int data;struct LNode * next;}LNode, * Listlist;//bool init(Listlist & L,int i) //尾插法//{//LNode *new_node, *temp_node;//temp_node = L;//for (int num = 0; num < i; num++)//{//new_node = new LNode;//new_node->next = NULL;//cin >> new_node->data;//temp_node->next = new_node;//temp_node = new_node;//}//}bool init(Listlist & L, int i)    //頭插法{LNode * new_item, * temp_item;//申明一個暫時的節點的指標,然後一個向下傳的界點的指標temp_item = L;//將這個臨時的節點當成是一個鄉下傳遞的一個指標int num;//數量for (num = 0; num < i; num++){new_item = new LNode;//申明一個新的節點new_item->next = NULL;//將末尾置為空白cin >> new_item->data;//輸入這個新節點的值new_item->next = L->next;//將新節點指向第一個節點的地址L->next = new_item;//將前端節點指向這個新的節點}return true;//返回正確}bool destroy(Listlist &L){Listlist temp;//臨時的指標temp = L->next;//指向第一個節點while (temp)//判斷這個節點是不是空的,並作為一個結束條件{L->next = L->next->next;//將頭結點的下一個指標,指向下下個節點free(temp);//釋放單獨提出來的節點temp = L->next;//臨時節點指向下一個節點}return true;//返回正確}bool listempty(Listlist L){return L->next ? false : true;//如果頭指標的指向下一個節點的指標為空白的話呢,那麼這個就是一個空的鏈表.}int listlength(Listlist & L){Listlist temp;temp = L->next;//將頭指標指向的一個節點,當做是第一個節點。int num = 1;//並賦值為1while (temp)//將temp作為一個判斷是否為空白的一個條件{temp = temp->next;//指向下一個,就像在數數一樣num++;//數目加一}return num - 1;//因為最後一個數到的數是null 指標所索引的數}bool getitem(Listlist L, int index, int &item){if (index < 1 && index>listlength(L))//先判斷這個條件是不是成立的,符不符合我們的輸入的條件{return false;//如果不符合,我們就返回這個錯誤的}LNode * temp;//申明的是一個臨時節點的指標temp = L->next;//指向第一個節點int num = 1;//並且將這個節點的標號為1while (temp&&num < index - 1)//一個是將這個指標是不是空的作為一個判斷條件,然後將這個是不是到了,這個後面當做是一個判斷的條件{temp = temp->next;//將這個節點的值,指向下一個節點num++;//數目加一}if (!temp || num >= index - 1){return false;}item = temp->data;return true;}bool locateitem(Listlist L, int index, int item){if (index<1 && index>listlength(L)){return false;}LNode * temp=L->next;int num = 1;while (temp&&num < index - 1){temp = temp->next;num++;}return item == temp->data ? true : false;}bool listinsert(Listlist &L, int index, int item){if (index<1 && index>listlength(L)){return false;}LNode * temp=L;int num = 0;while (temp&&num < index - 1){temp = temp->next;num++;}LNode * new_node = new LNode;new_node->next = temp->next;cin >> new_node->data;temp->next = new_node;return true;}bool deleteitem(Listlist & L, int index, int &item){if (index<1 && index>listlength(L)){return false;}LNode * temp=L;int num = 0;while (temp&&num < index - 1){temp = temp->next;num++;}item = temp->next->data;temp->next = temp->next->next;return true;}

  

資料結構--鏈表

聯繫我們

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