-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
85 lines (77 loc) · 2.01 KB
/
list.c
File metadata and controls
85 lines (77 loc) · 2.01 KB
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
#include "stdlib.h"
#include "stdio.h"
#include "list.h"
#include "item.h"
List *makeList() {
List *list = malloc(sizeof(List));
if (!list) {
return NULL;
}
list->head = NULL;
list->size = 0;
return list;
}
void add(ShopItem *item, List *list) {
ShopItem *current = NULL;
item->next = NULL;
item->id = 0;
if (list->head == NULL) {
list->head = item;
} else {
item->id++;
current = list->head;
while (current->next != NULL) {
item->id++;
current = (ShopItem *) current->next;
}
current->next = (struct ShopItem *) item;
}
}
ShopItem *get(int id, List *list) {
ShopItem *current = list->head;
while (current != NULL) {
if (current->id == id) {
return current;
}
current = (ShopItem *) current->next;
}
return NULL;
}
int delete(int id, List *list) {
ShopItem *current = list->head;
ShopItem *previous = current;
while (current != NULL) {
if (current->id == id) {
previous->next = current->next;
if (current == list->head)
list->head = (ShopItem *) current->next;
free(current);
return 1;
}
previous = current;
current = (ShopItem *) current->next;
}
return 0;
}
void display(List *list) {
ShopItem *current = list->head;
if (current == NULL) {
printf("List is empty\n");
return;
}
while (current != NULL) {
printf("%d. Title: %s\nType: %s\nDescription: %s\nCount: %d\nPrice: %lf\nStars: %lf\n\n", current->id,
current->name, current->type, current->description, current->count, current->price, current->stars);
current = (ShopItem *) current->next;
}
}
void destroy(List *list) {
ShopItem *current = list->head;
ShopItem *next = current;
while (current != NULL) {
next = (ShopItem *) current->next;
free(current);
current = next;
}
free(list);
}