Created
May 22, 2019 16:33
-
-
Save sewera/311691eabb22d58a8ac32acffcd6eece to your computer and use it in GitHub Desktop.
Basic list implementation on classes in cpp
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
#include <cstddef> | |
#include <iostream> | |
class Wezel { | |
public: | |
Wezel(int data, Wezel* prev, Wezel* next) { | |
this->data = data; | |
this->prev = prev; | |
this->next = next; | |
} | |
static Wezel* first; | |
static Wezel* last; | |
static int size; | |
int data; | |
Wezel* prev; | |
Wezel* next; | |
}; | |
Wezel* Wezel::first = NULL; | |
Wezel* Wezel::last = NULL; | |
int Wezel::size = 0; | |
void add_node(int data) { | |
Wezel* nowy; | |
if (Wezel::size == 0) { | |
nowy = new Wezel(data, NULL, NULL); | |
Wezel::first = nowy; | |
Wezel::last = nowy; | |
} else { | |
nowy = new Wezel(data, Wezel::last, NULL); | |
Wezel::last->next = nowy; | |
Wezel::last = nowy; | |
} | |
Wezel::size++; | |
} | |
void print_nodes() { | |
Wezel* current = Wezel::first; | |
std::cout << "[ "; | |
while (current->next != NULL) { | |
std::cout << current->data << ", "; | |
current = current->next; | |
} | |
if (current != NULL) { | |
std::cout << current->data; | |
} | |
std::cout << " ]" << std::endl; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
add_node(0); | |
add_node(7); | |
add_node(3); | |
add_node(4756754); | |
add_node(88); | |
add_node(1); | |
add_node(9); | |
add_node(2); | |
print_nodes(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment