相比於單鏈表,除了指向下乙個節點的next指標,雙向鍊錶在每個節點中,還設定了乙個指向前驅節點的prev指標。
雙鏈表示意圖
優點:有了向前指向的指標,雙向鍊錶的反向查詢操作無疑優於單鏈表,求得了以空間換時間的效果。
缺點:插入或刪除節點操作比單鏈表複雜,需要維護兩個指標變數。
雙向鍊錶插入結點過程:
雙向鍊錶刪除結點過程:
完整**:
test1 結果:#include
using namespace std;
typedef int datatype;//雙向鍊錶
struct listnode//節點結構體
};class list
list(const list
& l)
:_head(null)
,_tail(null)
}list
&operator=(list l)
void clear()
_head = _tail =
null;
}~list()
void pushback(datatype x)
else
}void popback()
else
if(_head->_next ==
null)
else
}void pushfront(datatype x)
void popfront()
else
if(_head->_next ==
null)
else
}void insert(node* pos, datatype x)
else
}else
}void erase(node* pos) //刪除pos
else
if(pos->_next ==
null)
else
}node* find(datatype x)
cur = cur->_next;
}return
null;
}void print()
}void reverse()
swap(_head,_tail);
}private:
node* _head;
node* _tail;
};//void test1()
//void test2()
int main()
test2 結果:
雙向鍊錶(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...