雙向鍊錶在類中的實現
#include#include#includeusing namespace std;
typedef int datatype;
struct node
node* _pnext;
node* _ppre;
int _data;
};class list
list(const list& l)
else //若是最後乙個節點,則尾指標指向空
}_size = l._size;
} list(size_t n, const datatype& data = datatype())
else
}_size = n;
} list& operator=(const list& l) //與拷貝建構函式類似
else
}_size = l._size;
} return *this;
} //capacity///
size_t size()const
size_t empty()const
acess///
node& front()
const node& front()const
node& back()
return *ptemp;
} const node& back()const
return *ptemp;
} modify/
void assign(size_t n, const datatype& data)
else
}_size = n;
} void pushback(const datatype& data)
else
ptemp->_pnext = pnewnode;
pnewnode->_ppre = ptemp;
pnewnode->_pnext = null;
_size = _size+1;
} }void popback()
if(1 == _size) //只有乙個節點
else //多個節點
_ptail = ptemp;
node* ptemp1 = _ptail;
_ptail = _ptail->_ppre;
_ptail->_pnext = null;
delete ptemp1;
_size = _size-1;
} }void pushfront(const datatype& data)
else//鍊錶有乙個或多個節點
}void popfront()
else if(1 == _size)//鍊錶有乙個節點
else //鍊錶有乙個或多個節點
}node* find(const datatype& data)
else
ptemp =ptemp->_pnext;
}return null;
} } void insert(node* pos, const datatype& data)
else if(pos->_pnext == null) //該節點為尾結點
else //其他節點
}void erase(node* pos)
else if(pos->_pnext == null) //該節點為尾結點
else //其他節點
}~list()
_ptail = ptemp;
while((_ptail != _phead)&&(_ptail!=null))
delete _phead;
_phead =_ptail = null;
_size = 0;
} }private:
node* _phead;
node* _ptail;
size_t _size;
};void funtest()
int main()
雙向鍊錶(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 實現雙向鍊錶
struct dlistnode 節點的建立 class dlist dlist const dlist l head null tail null else dlist operator const dlist l 傳統寫法 tail next null return this dlist ope...