Created
August 11, 2020 07:11
-
-
Save baoqger/d573443c5472b195c86dbe9f44869ccb to your computer and use it in GitHub Desktop.
double link list in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Doubly linked lists | |
Doubly linked list is a type of data structure that is made up of so called objects | |
that are created using self referential structures. Each of these nodes contain three | |
attributes, namely the value and the reference to the next list object and the reference | |
to the previous list object. | |
In this exercise you will create a doubly linked list, which exposes 4 methods: | |
push_back(): add values to the end of the list | |
push_front(): add values to the beginning to the list | |
pop_back(): delete values from the end of the list | |
pop_front(): delete values from the front of the list | |
Also, you will implement a helper function, empty(), which returns whether or not | |
the list has any nodes. | |
*/ | |
#include <iostream> | |
#include <assert.h> | |
#include <string> | |
template<class T> | |
class Node { | |
public: | |
Node(T val, Node *prev_node, Node *next_n): | |
value(val), previous_node(prev_node),next_node(next_n){} | |
T value; | |
Node *previous_node; | |
Node *next_node; | |
}; | |
template<class T> | |
class List{ | |
public: | |
List(): head(nullptr), tail(nullptr){} | |
~List(); | |
void PushFront(T); | |
void PushBack(T); | |
T PopFront(); | |
T PopBack(); | |
bool Empty() const { return (head == nullptr);} | |
int Size() const; | |
private: | |
Node<T> *head; | |
Node<T> *tail; | |
}; | |
template<class T> | |
List<T>::~List(){ | |
while ( head != nullptr ){ | |
Node<T> *tmp = head; | |
head = head->next_node; | |
delete tmp; | |
} | |
} | |
template<class T> | |
void List<T>::PushBack(T val){ | |
tail = new Node<T>(val, tail, nullptr); | |
if (head == nullptr){ | |
head = tail; | |
}else{ | |
tail->previous_node->next_node = tail; | |
} | |
} | |
template<class T> | |
void List<T>::PushFront(T val){ | |
head = new Node<T>(val, nullptr, head); | |
if (tail == nullptr){ | |
tail = head; | |
}else{ | |
head->next_node->previous_node = head; | |
} | |
} | |
template<class T> | |
int List<T>::Size() const { | |
int count = 0; | |
Node<T> *tmp = head; // copy head to tmp | |
std::string rst = ""; | |
while(tmp !=nullptr){ | |
rst = rst + std::to_string(tmp->value) +", "; | |
tmp = tmp->next_node; | |
count++; | |
} | |
std::cout << rst << std::endl; | |
return count; | |
} | |
template<class T> | |
T List<T>::PopFront() { | |
if (List::Empty()) | |
throw("Cannot List::PopBack() when List::Empty()"); | |
Node<T> *tmp = List<T>::head; | |
T value = tmp->value; | |
head = head ->next_node; | |
if(head != nullptr){ | |
head->previous_node = nullptr; | |
}else{ | |
tail = nullptr; | |
} | |
delete tmp; | |
return value; | |
} | |
template<class T> | |
T List<T>::PopBack(){ | |
if (List::Empty()) | |
throw("Cannot List::PopBack() when List::Empty()"); | |
Node<T> *tmp = tail; | |
T value = tail->value; | |
tail = tail->previous_node; | |
if (tail != nullptr){ | |
tail->next_node = nullptr; | |
}else{ | |
head = nullptr; | |
} | |
delete tmp; | |
return value; | |
} | |
int main() { | |
// Sanity test | |
List<int> list1; | |
list1.PushBack(9); | |
std::cout << "Size() = " << list1.Size() << std::endl; | |
assert(list1.Size() == 1); | |
// Deeper test | |
List<int> list2; | |
list2.PushFront(9); | |
list2.PushBack(10); | |
assert(list2.Size() == 2); | |
list2.PushBack(15); | |
list2.PushFront(2); | |
std::cout << "Size() = " << list2.Size() << std::endl; | |
assert(list2.PopFront() == 2); | |
std::cout << "list2.PopFront() = " << list2.PopFront() << std::endl; | |
std::cout << "Size() = " << list2.Size() << std::endl; | |
assert(list2.PopBack() == 15); | |
assert(list2.Size() == 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment