Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- template<typename T>
- class list_node {
- private:
- T data;
- list_node* p_next;
- public:
- list_node(const T& data) : data(data), p_next(nullptr) { }
- ~list_node() { }
- void connectNew(list_node* p_to_new) {
- p_next = p_to_new;
- }
- list_node<T>* getNext() {
- return p_next;
- }
- void setData(const T& new_data) {
- data = new_data;
- }
- T getData() const {
- return data;
- }
- };
- template<typename T>
- class list {
- private:
- list_node<T>* head;
- public:
- list() : head(nullptr) { }
- ~list() {
- list_node<T>* next;
- while (head != nullptr) {
- list_node<T>* next = head->getNext();
- delete head;
- head = next;
- }
- }
- void push_tail(const T& data) {
- list_node<T>* current = head;
- while (current->getNext() != nullptr)
- current = current->getNext();
- list_node<T>* new_node = new list_node<T>(data);
- current->connectNew(new_node);
- }
- T pop_tail() {
- list_node<T>* prev = head;
- while (prev->getNext()->getNext() != nullptr)
- prev = prev->getNext();
- T data = prev->getNext()->getData();
- delete prev->getNext();
- prev->connectNew(nullptr);
- return data;
- }
- };
- int main()
- {
- list<int> my_list;
- std::cout << my_list.pop_tail() << std::endl;
- my_list.push_tail(1);
- my_list.push_tail(1);
- my_list.push_tail(1);
- my_list.push_tail(1);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement