執行結果:#include using namespace std;
// 定義單向鍊錶節點
struct listnode
; // end of listnode
// 將新節點插入煉表頭
void insertlist(listnode** head, int insertdata)
listnode* pnode = new listnode;
pnode->data = insertdata;
pnode->next = *head;
*head = pnode;
}// 刪除鍊錶中的節點
void removelist(listnode** head, int removedata)
listnode* pre = null; // 指向當前節點的前乙個節點
listnode* cur = *head;
for(; cur != null && cur->data != removedata; pre = cur, cur = cur->next)
// 找到相應節點
if(cur != null)
else // 非頭節點
delete cur; }}
// 顯示鍊錶
void showlist(const listnode* head)
cout << endl;
}int main(int argc, char* argv)
; int size = sizeof(arr) / sizeof(int);
listnode* head = null;
for(int i = 0; i < size; ++i)
showlist(head);
// 當前節點序列(1 2 3 4)
// 刪除中間節點
removelist(&head, 2);
showlist(head);
// 刪除頭節點
removelist(&head, 1);
showlist(head);
// 刪除尾節點
removelist(&head, 4);
showlist(head);
// 刪除最後乙個節點
removelist(&head, 3);
showlist(head);
}
12 34
134343
雙向鍊錶插入 刪除操作
雙向鍊錶 迴圈單鏈表的出現,雖然能夠實現從任一結點出發沿著鏈能找到其前驅結點,但時間耗費是o n 如果希望從表中快速確定某乙個結點的前驅,另乙個解決方法就是在單鏈表的每個結點裡再增加乙個指向其前驅的指標域prior。這樣形成的鍊錶中就有兩條方向不同的鏈,我們可稱之為雙 向 鍊錶 doublelink...
鍊錶插入刪除
include include typedef struct node node,linklist void createlist linklist head 建立鍊錶 s node malloc sizeof node s next null s data data p next s p s in...
一 鍊錶 插入刪除基本操作
鍊錶 一種用於儲存資料集合的資料結構 鍊錶的屬性 1.相鄰元素之間通過指標連線 2.最後乙個元素的後繼元素為null 3.鍊錶的空間可以按需分配 直到系統記憶體耗盡 4.沒有記憶體空間的浪費 但是鍊錶當中的指標需要一些額外的記憶體花銷 鍊錶和陣列的區別 1 陣列儲存在一塊連續的記憶體空間中,而鍊錶的...