-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
146 lines (123 loc) · 2.92 KB
/
list.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
//#include "iterator.h"
typedef char Item;
List* list_create()
{
List *list = (List*) malloc(sizeof(List));
list->head = NULL;
return list;
}
void help()
{
printf("The main function of this app is working with the unidirectional list. \n");
printf("Use this keys to work with the program: \n");
printf("- 'add' - to insert the value to the list\n");
printf("Input the elements in this order:\n");
printf("1. The value to set up\n");
printf("2. The value after which you have to insert\n");
printf("Note: if the value after which you want to insert doesn't exist, program should stop working\n");
printf("For inserting the element to the list origin input in this way: 'value' '.'\n");
printf("- 'delete' - to remove the value from the list\n");
printf("- 'lenght' - to gain the list size\n");
printf("- 'task' - to start removing the elements that precede and follow the specified value \n");
printf("- 'print' - to print the list\n");
printf("- 'clear' - to make the list empty \n");
printf("- 'destroy' - to destroy the list \n");
printf("- 'exit' - to quit the application\n");
}
int list_lenght(List* list)
{
Node* temp = list->head;
int size = 0;
while (temp != NULL) {
size++;
temp = temp->next;
}
return size;
}
void list_clear(List *list)
{
Node *del = list->head;
while (del != NULL) {
Node *tmp = del->next;
free(del);
del = tmp;
}
list->head = NULL;
}
Node* iterator_first(List* list)
{
return(list->head);
}
void list_delete_node(List* list, Node* delete)
{
if (delete == list->head) {
Node*temp= list->head;
list->head=list->head->next;
free(temp);
} else {
Node* tmp= list->head;
while (tmp->next != delete) {
tmp=tmp->next;
}
tmp->next= delete->next;
free(delete);
}
}
void list_destroy(List **list)
{
Node *del = (*list)->head;
while (del != NULL) {
Node *tmp = del->next;
free(del);
del = tmp;
}
free (*list);
*list = NULL;
}
void list_insert(List* list, Node *after_node, Item value)
{
Node *inserted = (Node*) malloc(sizeof(Node));
inserted->data = value;
if (after_node == NULL) {
inserted->next = list->head;
list->head = inserted;
} else {
inserted->next = after_node->next;
after_node->next = inserted;
}
}
Node* list_node_find(List* list, Item value)
{
Node* tmp = list->head;
while(tmp!= NULL) {
if (tmp->data == value) {
return tmp;
}
tmp=tmp->next;
}
if (value == '.') {
return NULL;
} else {
list_destroy(&list);
fprintf(stderr, "Error: value is out of range\n");
exit(1);
}
}
void list_print(List *list)
{
Node *temp = list->head;
while (temp != NULL) {
printf("%c ", temp->data);
temp = temp->next;
}
printf("\n");
}
void list_task(List* list, Item value)
{
list_clear(list);
list_insert(list,'.', value);
}