Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <conio.h>
- #include <time.h>
- using namespace std;
- template <class Type>
- struct Node{
- Node *next;
- Type data;
- };
- template <class Type>
- class List{
- private:
- int size;
- Node<Type> *first;
- Node<Type> *last;
- Type getRecursive(int index, int counter, Node<Type> *currentNode){
- if(index == counter){
- return (*currentNode).data;
- }else{
- if((*currentNode).next == NULL){
- return NULL;
- }else{
- return getRecursive(index, counter + 1, (*currentNode).next);
- }
- }
- }
- Node<Type> *getNodeByIndex(int index, Node<Type> *startNode){
- if(index == 0){
- return startNode;
- }
- struct Node<Type> *help = startNode;
- for(int i = 1; i <= index; i++){
- help = (*help).next;
- }
- return help;
- }
- public:
- List(){
- first = NULL;
- last = NULL;
- size = 0;
- }
- int getSize(){
- return this->size;
- }
- void add(Type param){
- if(size == 0){
- //add first element to list
- struct Node<Type> *element = (struct Node<Type>*)malloc(sizeof(struct Node<Type>));
- (*element).next = NULL;
- (*element).data = param;
- this->first = element;
- this->last = element;
- this->size++;
- }else{
- //add element at the end
- struct Node<Type> *element = (struct Node<Type>*)malloc(sizeof(struct Node<Type>));
- (*element).next = NULL;
- (*element).data = param;
- //umpointen
- this->last->next = element;
- this->last = element;
- this->size++;
- }
- }
- void addAt(Type param, int index){
- if(index < 0){
- return;
- }else if(index >= this->size){
- add(param);
- }else{
- if(index == 0){
- //add at first position
- struct Node<Type> *element = (struct Node<Type>*)malloc(sizeof(struct Node<Type>));
- (*element).next = this->first;
- (*element).data = param;
- this->first = element;
- }else{
- //add further back
- //get node before the index on which a new one should added
- Node<Type> *node = getNodeByIndex(index - 1, this->first);
- struct Node<Type> *element = (struct Node<Type>*)malloc(sizeof(struct Node<Type>));
- (*element).data = param;
- (*element).next = (*node).next;
- (*node).next = element;
- this->size++;
- }
- }
- }
- Type get(int index){
- if(index >= this->size || index < 0){
- return NULL;
- }
- return getRecursive(index, 0, this->first);
- }
- void remove(int index){
- if(index >= this->size){
- return;
- }else if(index == 0){
- struct Node<Type> *help = this->first;
- this->first = (*help).next;
- free(help);
- this->size--;
- }else{
- //get node before the index on which the node should be removed
- struct Node<Type> *before = getNodeByIndex(index - 1, this->first);
- //check if it's last one
- if(index == (this->size - 1)){
- struct Node<Type> *helpToLast = this->last;
- this->last = before;
- (*before).next = NULL;
- this->size--;
- free(helpToLast);
- return;
- }
- //otherwise remove node inbewteen
- struct Node<Type> *help = (*before).next;
- (*before).next = (*help).next;
- free(help);
- this->size--;
- }
- }
- int indexOf(Type param){
- for(int i = 0; i < this->size; i++){
- struct Node<Type> *current = getNodeByIndex(i, this->first);
- if((*current).data == param){
- //printf("Here\n");
- return i;
- }
- }
- return -1;
- }
- int *indexesOf(Type param){
- int *result = (int*)malloc(sizeof(int) * 2);
- int counter = 1;
- for(int i = 0; i < this->size; i++){
- struct Node<Type> *current = getNodeByIndex(i, this->first);
- if((*current).data == param){
- result[counter] = i;
- counter++;
- realloc(result, (sizeof(int) * counter) + 1);
- }
- }
- result[0] = counter - 1;
- return result;
- }
- void replace(int index, Type param){
- if(index < -1 || index >= this->size){
- return;
- }
- struct Node<Type> *node = getNodeByIndex(index, this->first);
- (*node).data = param;
- }
- };
- int main()
- {
- List<int> list;
- list.add(0);
- list.add(2);
- list.add(3);
- list.add(3);
- list.add(3);
- list.add(3);
- list.add(2);
- list.add(5);
- list.add(7);
- list.add(3);
- list.add(4);
- for(int i = 0; i < list.getSize(); i++){
- printf("[%d] -> %d\n", i, list.get(i));
- }
- printf("\n\nAdd Node inbetween\n");
- list.addAt(1, 1);
- printf("\n\n");
- for(int i = 0; i < list.getSize(); i++){
- printf("[%d] -> %d\n", i, list.get(i));
- }
- printf("\n\nRemove on index 1\n");
- list.remove(1);
- printf("\n\n");
- for(int i = 0; i < list.getSize(); i++){
- printf("[%d] -> %d\n", i, list.get(i));
- }
- printf("\n\nIndex of 3 => [%d]\n\n", list.indexOf(3));
- //indexesof => an 1. Stelle Anzahl wie oft gefunden
- int *indexes = list.indexesOf(3);
- printf("sizeof => %d\n", indexes[0]);
- for(int i = 0; i < indexes[0]; i++){
- printf("3 was found on => [%d]\n", indexes[i + 1]);
- }
- printf("\n\nReplace the before last element with number '9'\n");
- list.replace(list.getSize() - 2, 9);
- for(int i = 0; i < list.getSize(); i++){
- printf("[%d] -> %d\n", i, list.get(i));
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment