雙向鍊錶的C 實現

2021-06-21 05:51:10 字數 2332 閱讀 1396

首先定義節點的結構

struct node 

;

老樣子,建立雙鏈表的類

class doublelink

protected:

private:

int length;

node* root;

};

插入元素

int doublelink::doublelink_insert(doublelink *ptr, int position, int member)

else

else

nodeinsert->next = current;

nodeinsert->prev = current->prev;

current->prev->next = nodeinsert;

//nodeinsert->next = nodeinsert;

current->prev = nodeinsert;

ptr->length++;

return 0;

} }}

刪除乙個元素,注意0位置上的

int doublelink::doublelink_erase(doublelink* ptr, int position)

else

deletenode->prev->next = deletenode->next;

deletenode->next->prev = deletenode->prev;

delete deletenode;

ptr->length--;

} }}

完整實現**和測試結果

測試正確~~

雙向鍊錶(C實現)

list.h ifndef list h define list h typedef struct node node typedef struct list list initlist int insertnode list l,void data,int size int deletenode ...

雙向鍊錶(c 實現)

雙向鍊錶與單鏈表有許多的相似之處,但是也有不同之處。雙向鍊錶與單鏈表主要的不同在於 雙向鍊錶可以從兩個方向進行遍歷,但是單鏈表只能從頭節點遍歷到尾節點,不能從尾節點遍歷到頭節點,對於鍊錶中一些插入和刪除等操作,雙向鍊錶較單鏈表更為簡單些。所以,雙向鍊錶有其存在的特殊意義。下面是通過c 來實現雙向鍊錶...

雙向鍊錶 C 實現

雙向鍊錶在類中的實現 include include includeusing namespace std typedef int datatype struct node node pnext node ppre int data class list list const list l else...