add solution of lab 5
This commit is contained in:
@@ -131,19 +131,32 @@ dslist<T>& dslist<T>::operator= (const dslist<T>& old) {
|
||||
|
||||
template <class T>
|
||||
void dslist<T>::push_front(const T& v) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Node<T>* newp = new Node<T>(v);
|
||||
// if list is empty
|
||||
if (!head_) {
|
||||
head_ = newp;
|
||||
tail_ = newp;
|
||||
} else {
|
||||
newp->next_ = head_;
|
||||
head_->prev_ = newp;
|
||||
head_ = newp;
|
||||
}
|
||||
++size_;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void dslist<T>::pop_front() {
|
||||
|
||||
|
||||
|
||||
|
||||
assert(head_ != NULL); // list must not be empty
|
||||
Node<T>* temp = head_;
|
||||
head_ = head_->next_;
|
||||
if (head_) {
|
||||
head_->prev_ = NULL;
|
||||
} else {
|
||||
// list becomes empty
|
||||
tail_ = NULL;
|
||||
}
|
||||
delete temp;
|
||||
--size_;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@@ -163,11 +176,17 @@ void dslist<T>::push_back(const T& v) {
|
||||
|
||||
template <class T>
|
||||
void dslist<T>::pop_back() {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
assert(tail_ != NULL); // list must not be empty
|
||||
Node<T>* temp = tail_;
|
||||
tail_ = tail_->prev_;
|
||||
if (tail_) {
|
||||
tail_->next_ = NULL;
|
||||
} else {
|
||||
// list becomes empty
|
||||
head_ = NULL;
|
||||
}
|
||||
delete temp;
|
||||
--size_;
|
||||
}
|
||||
|
||||
// do these lists look the same (length & contents)?
|
||||
@@ -186,7 +205,7 @@ bool operator== (dslist<T>& left, dslist<T>& right) {
|
||||
|
||||
template <class T>
|
||||
bool operator!= (dslist<T>& left, dslist<T>& right){ return !(left==right); }
|
||||
template <class T>
|
||||
template <class T>
|
||||
typename dslist<T>::iterator dslist<T>::erase(iterator itr) {
|
||||
assert (size_ > 0);
|
||||
--size_;
|
||||
@@ -256,13 +275,15 @@ void dslist<T>::copy_list(const dslist<T>& old) {
|
||||
|
||||
template <class T>
|
||||
void dslist<T>::destroy_list() {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Node<T>* current = head_;
|
||||
while (current) {
|
||||
Node<T>* next = current->next_;
|
||||
delete current;
|
||||
current = next;
|
||||
}
|
||||
head_ = NULL;
|
||||
tail_ = NULL;
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user