-
Notifications
You must be signed in to change notification settings - Fork 0
/
ueTest.cpp
128 lines (114 loc) · 2.71 KB
/
ueTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Created by Maximilian Nowak on 28.06.22.
//
#include "ueTest.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
list *l_init() {
list *lst = (list *) malloc(sizeof(list));
lst->head = NULL;
lst->count = 0;
return lst;
}
void l_insert(list *lst, char *word) {
element *new_data = (element *) malloc(sizeof(element));
strcpy(new_data->word, word);
new_data->next = lst->head;
lst->head = new_data;
lst->count++;
}
void l_delete(list *lst, char *word) {
element *prev = NULL;
element *cur = lst->head;
element *next = lst->head->next;
while (cur != NULL) {
if (strcmp(cur->word, word) == 0) {
if (cur == lst->head) {
lst->head = next;
lst->count = 0;
} else {
prev->next = next;
lst->count --;
}
free(cur);
return;
}
prev = cur;
cur = cur->next;
if(cur == NULL) return;
next = cur->next;
}
}
void l_reverse(list *lst) {
element *prev = NULL;
element *cur = lst->head;
element *next = lst->head->next;
while (cur != NULL) {
cur->next = prev;
prev =cur;
cur = next;
if(cur != NULL) next = cur->next;
}
lst->head = prev;
}
int l_isSet(list *lst) {
if(lst == NULL) return -1;
if(lst != NULL || lst->head !=NULL){
element *tmp = lst->head;
while (tmp != NULL) {
if(l_find(lst,tmp->word) > 1){
return 0;
}
tmp = tmp->next;
}
return 1;
} else{
return -1;
}
}
void swap(element *a, element *b)
{
char temp [100];
strcpy(temp, a->word);
strcpy( a->word , b->word);
strcpy( b->word , temp);
}
void l_sort(list *lst, int asc) {
element *outside = lst->head;
while (outside != NULL) {
element *tmp = lst->head;
while (tmp->next != NULL) {
if(asc == 1){
if(strcmp(tmp->word,tmp->next->word) > 0){
swap(tmp->next, tmp);
}
} else{
if(strcmp(tmp->word,tmp->next->word) < 0){
swap(tmp->next, tmp);
}
}
tmp = tmp->next;
}
outside = outside->next;
}
}
void l_print(list *lst) {
element *tmp = lst->head;
while (tmp != NULL) {
printf("%s -> ", tmp->word);
tmp = tmp->next;
}
printf("\n");
}
int l_find(list *lst, char *word) {
int count =0;
element *tmp = lst->head;
while (tmp != NULL) {
if (strcmp(tmp->word, word) == 0) {
count++;
}
tmp = tmp->next;
}
return count;
}