Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * List.cpp
- *
- */
- #include "List.h"
- List::Node::~Node(){
- // TO DO
- }
- List::~List() {
- }
- void List::push_front(int object){
- Node* new_node = new Node(object);
- head->previous = new_node;
- new_node->next = head;
- head = new_node;
- ++n;
- }
- void List::pop_front(){
- Node* next = head->next;
- delete head;
- next->previous = nullptr;
- head = next;
- --n;
- }
- void List::push_back(int object) {
- Node* new_node = new Node(object);
- if (n == 0) {
- head = new_node;
- tail = new_node;
- }
- else {
- tail->next = new_node;
- new_node->previous = tail;
- tail = new_node;
- }
- ++n;
- }
- void List::pop_back(){
- Node* previous = tail->previous;
- delete tail;
- previous->next = nullptr;
- tail = previous;
- --n;
- }
- void List::insert(int index, int object){
- if (index == 0){
- push_front(object);
- }
- else if (index == (n - 1)){
- push_back(object);
- }
- else{
- try{
- Node* new_node = new Node(object);
- Node* previous = get_node_ptr(index - 1);
- Node* next = get_node_ptr(index + 1);
- next->previous = new_node;
- new_node->next = next;
- previous->next = new_node;
- new_node->previous = previous;
- ++n;
- }
- catch (const char* &s) {
- std::cerr << s;
- system("PAUSE");
- exit(1);
- }
- }
- }
- void List::erase(int index){
- if (index == 0){
- pop_front();
- }
- else if (index == (n - 1)){
- pop_back();
- }
- else
- try{
- Node* current = get_node_ptr(index);
- Node* previous = get_node_ptr(index - 1);
- Node* next = get_node_ptr(index + 1);
- next->previous = current->previous;
- previous->next = current->next;
- delete current;
- --n;
- }
- catch (const char* &s) {
- std::cerr << s;
- system("PAUSE");
- exit(1);
- }
- }
- void List::swap(int index1, int index2){
- //try{
- // Node* first = get_node_ptr(index1);
- // Node* second = get_node_ptr(index2);
- // Node* tmp = new Node(first);
- // first->data = second->data;
- // first->next = second->next;
- // first->previous = second->previous;
- // second->data = tmp->data;
- // second->next = tmp->next;
- // second->previous = tmp->previous;
- // delete tmp;
- //}
- //catch (const char* &s) {
- // std::cerr << s;
- // system("PAUSE");
- // exit(1);
- //}
- }
- int List::size(){
- return n;
- }
- int List::get_index_of(const int object){
- return 0;
- }
- List::Node& List::get_node_ref(int index){
- Node* node = head;
- if (index >= n || index < 0)
- throw "Przekroczono zakres!\n";
- for (int i = 0; i < index; ++i) {
- if (node == nullptr)
- throw "Blad! Dziura w liscie!\n";
- node = node->next;
- }
- return *node;
- }
- List::Node* List::get_node_ptr(int index) {
- Node* node = head;
- if (index >= n || index < 0)
- throw "Przekroczono zakres!\n";
- for (int i = 0; i < index; ++i) {
- if (node == nullptr)
- throw "Blad! Dziura w liscie!\n";
- node = node->next;
- }
- return node;
- }
- void List::save_to_file(std::string file_name){
- }
- void List::load_from_file(std::string file_name){
- }
- //bool List::operator==(const List &L){
- //
- //}
- //List& List::operator=(const List &L){
- //
- //}
- int& List::operator[](int index) {
- try{
- return get_node_ref(index).data;
- }
- catch (const char* &s) {
- std::cerr << s;
- system("PAUSE");
- exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement