View difference between Paste ID: Wnv2KXi2 and 2ha8jPRa
SHOW: | | - or go back to the newest paste.
1
#include <iostream>
2
#include <stdlib.h>
3
#include <conio.h>
4
#include <time.h>
5
6
using namespace std;
7
8
template <class Type>
9
struct Node{
10
    Node *next;
11
    Type data;
12
};
13
14
template <class Type>
15
class List{
16
    private:
17
        int size;
18
        Node<Type> *first;
19
        Node<Type> *last;
20
21
        Type getRecursive(int index, int counter, Node<Type> *currentNode){
22
            if(index == counter){
23
                return (*currentNode).data;
24
            }else{
25
                if((*currentNode).next == NULL){
26
                    return NULL;
27
                }else{
28
                    return getRecursive(index, counter + 1, (*currentNode).next);
29
                }
30
            }
31
        }
32
33
        Node<Type> *getNodeByIndex(int index, Node<Type> *startNode){
34
            if(index == 0){
35
                return startNode;
36
            }
37
38
            struct Node<Type> *help = startNode;
39
40
            for(int i = 1; i <= index; i++){
41
                help = (*help).next;
42
            }
43
44
            return help;
45
        }
46
47
    public:
48
        List(){
49
            first = NULL;
50
            last = NULL;
51
            size = 0;
52
        }
53
54
        int getSize(){
55
            return this->size;
56
        }
57
58
        void add(Type param){
59
            if(size == 0){
60
                //add first element to list
61
                struct Node<Type> *element = (struct Node<Type>*)malloc(sizeof(struct Node<Type>));
62
                (*element).next = NULL;
63
                (*element).data = param;
64
                this->first = element;
65
                this->last = element;
66
                this->size++;
67
            }else{
68
                //add element at the end
69
                struct Node<Type> *element = (struct Node<Type>*)malloc(sizeof(struct Node<Type>));
70
                (*element).next = NULL;
71
                (*element).data = param;
72
                //umpointen
73
                this->last->next = element;
74
                this->last = element;
75
                this->size++;
76
            }
77
        }
78
79
        void addAt(Type param, int index){
80
            if(index < 0){
81
                return;
82
            }else if(index >= this->size){
83
                add(param);
84
            }else{
85
                if(index == 0){
86
                    //add at first position
87
                    struct Node<Type> *element = (struct Node<Type>*)malloc(sizeof(struct Node<Type>));
88
                    (*element).next = this->first;
89
                    (*element).data = param;
90
                    this->first = element;
91
                }else{
92
                    //add further back
93
                    //get node before the index on which a new one should added
94
                    Node<Type> *node = getNodeByIndex(index - 1, this->first);
95
                    struct Node<Type> *element = (struct Node<Type>*)malloc(sizeof(struct Node<Type>));
96
                    (*element).data = param;
97
                    (*element).next = (*node).next;
98
                    (*node).next = element;
99
                    this->size++;
100
                }
101
            }
102
        }
103
104
        Type get(int index){
105
            if(index >= this->size || index < 0){
106
                return NULL;
107
            }
108
109
            return getRecursive(index, 0, this->first);
110
        }
111
112
        void remove(int index){
113
            if(index >= this->size){
114
                return;
115
            }else if(index == 0){
116
                struct Node<Type> *help = this->first;
117
                this->first = (*help).next;
118
                free(help);
119
                this->size--;
120
            }else{
121
                //get node before the index on which the node should be removed
122
                struct Node<Type> *before = getNodeByIndex(index - 1, this->first);
123
                //check if it's last one
124
                if(index == (this->size - 1)){
125
                    struct Node<Type> *helpToLast = this->last;
126
                    this->last = before;
127
                    (*before).next = NULL;
128
                    this->size--;
129
                    free(helpToLast);
130
                    return;
131
                }
132
133
                //otherwise remove node inbewteen
134
                struct Node<Type> *help = (*before).next;
135
                (*before).next = (*help).next;
136
                free(help);
137
                this->size--;
138
            }
139
        }
140
141
        int indexOf(Type param){
142
            for(int i = 0; i < this->size; i++){
143
                struct Node<Type> *current = getNodeByIndex(i, this->first);
144
145
                if((*current).data == param){
146
                    //printf("Here\n");
147
                    return i;
148
                }
149
            }
150
151
            return -1;
152
        }
153
154
        int *indexesOf(Type param){
155
            int *result = (int*)malloc(sizeof(int) * 2);
156
            int counter = 1;
157
158
            for(int i = 0; i < this->size; i++){
159
                struct Node<Type> *current = getNodeByIndex(i, this->first);
160
                if((*current).data == param){
161
                    result[counter] = i;
162
                    counter++;
163
                    realloc(result, (sizeof(int) * counter) + 1);
164
                }
165
            }
166
167
            result[0] = counter - 1;
168
169
            return result;
170
        }
171
172
        void replace(int index, Type param){
173
            if(index < -1 || index >= this->size){
174
                return;
175
            }
176
177
            struct Node<Type> *node = getNodeByIndex(index, this->first);
178
            (*node).data = param;
179
        }
180
};
181
182
int main()
183
{
184
    List<int> list;
185
    list.add(0);
186
    list.add(2);
187
    list.add(3);
188
    list.add(3);
189
    list.add(3);
190
    list.add(3);
191
    list.add(2);
192
    list.add(5);
193
    list.add(7);
194
    list.add(3);
195
    list.add(4);
196
197
    for(int i = 0; i < list.getSize(); i++){
198
        printf("[%d] -> %d\n", i, list.get(i));
199
    }
200
201
    printf("\n\nAdd Node inbetween\n");
202
    list.addAt(1, 1);
203
    printf("\n\n");
204
205
    for(int i = 0; i < list.getSize(); i++){
206
        printf("[%d] -> %d\n", i, list.get(i));
207
    }
208
209
    printf("\n\nRemove on index 1\n");
210
    list.remove(1);
211
    printf("\n\n");
212
213
    for(int i = 0; i < list.getSize(); i++){
214
        printf("[%d] -> %d\n", i, list.get(i));
215
    }
216
217
    printf("\n\nIndex of 3 => [%d]\n\n", list.indexOf(3));
218
219
    //indexesof => an 1. Stelle Anzahl wie oft gefunden
220
    int *indexes = list.indexesOf(3);
221
    printf("sizeof => %d\n", indexes[0]);
222
    for(int i = 0; i < indexes[0]; i++){
223
        printf("3 was found on => [%d]\n", indexes[i + 1]);
224
    }
225
226
    printf("\n\nReplace the before last element with number '9'\n");
227
    list.replace(list.getSize() - 2, 9);
228
229
    for(int i = 0; i < list.getSize(); i++){
230
        printf("[%d] -> %d\n", i, list.get(i));
231
    }
232
233
    return 0;
234
}