Created
November 29, 2019 13:34
-
-
Save Janphr/6ebd4e80ed7f29040a16c2fb6e320a8e to your computer and use it in GitHub Desktop.
generic linked list c++
This file contains hidden or 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
// | |
// Created by s353498 on 11/29/2019. | |
// | |
#ifndef E6_LINKED_LIST_H | |
#define E6_LINKED_LIST_H | |
#include "node.h" | |
template<class D> | |
class linked_list { | |
private: | |
node<D>* head; | |
int list_size{}; | |
public: | |
linked_list() { | |
this->head = nullptr; | |
this->list_size = 0; | |
} | |
node<D> *getHead() { | |
return this->head; | |
} | |
int size() { | |
return this->list_size; | |
} | |
void push(node<D> *node) { | |
node->next = nullptr; | |
node->prev = nullptr; | |
if(this->head == nullptr){ | |
this->head = node; | |
} else { | |
this->head->prev = node; | |
node->next = this->head; | |
this->head = node; | |
} | |
this->list_size++; | |
} | |
node<D> *pop() { | |
node<D>* temp = this->head; | |
if(this->size() > 1) { | |
this->head = this->head->next; | |
this->head->prev = nullptr; | |
} | |
this->list_size--; | |
return temp; | |
} | |
node<D> *get(int idx) { | |
if(idx == 0){ | |
return this->head; | |
} else if(idx >= this->list_size) { | |
return nullptr; | |
} else { | |
node<D>* temp = this->head; | |
int i = 0; | |
while (i != idx){ | |
temp = temp->next; | |
i++; | |
} | |
return temp; | |
} | |
} | |
~linked_list() { | |
while (this->head){ | |
node<D>* temp = this->head->next; | |
delete this->head; | |
this->head = temp; | |
} | |
this->list_size = 0; | |
} | |
}; | |
#endif //E6_LINKED_LIST_H |
This file contains hidden or 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
#include <iostream> | |
#include "linked_list.h" | |
#include "student.h" | |
using namespace std; | |
int main() { | |
linked_list<student> list; | |
student s1 = {2170000, "Smith", "John"}; | |
list.push(new node<student>(s1)); | |
cout << "size: " << list.size() << endl; | |
cout << "pop: " << list.pop()->getData().toString() << endl; | |
return 0; | |
} |
This file contains hidden or 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
// | |
// Created by s353498 on 11/29/2019. | |
// | |
#ifndef E6_NODE_H | |
#define E6_NODE_H | |
using namespace std; | |
template<class D> | |
class node { | |
private: | |
D data; | |
public: | |
node* next; | |
node* prev; | |
explicit node(D data){ | |
this->data = data; | |
}; | |
D getData(){ | |
return this->data; | |
}; | |
~node() = default; | |
}; | |
#endif //E6_NODE_H |
This file contains hidden or 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
// | |
// Created by s353498 on 11/29/2019. | |
// | |
#ifndef E6_STUDENT_H | |
#define E6_STUDENT_H | |
#include <string> | |
#include <string> | |
#include <sstream> | |
using namespace std; | |
struct student { | |
int matricular_number; | |
string last_name; | |
string first_name; | |
string toString(){ | |
stringstream s; | |
s << this->last_name << ", " << this->first_name << ", " << this->matricular_number; | |
return s.str(); | |
} | |
}; | |
#endif //E6_STUDENT_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment