-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.c
120 lines (112 loc) · 2.18 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
#include <stdio.h>
#include <stdlib.h>
#include "Item.h"
#include "List.h"
void Add(struct List *L, struct Item *I){
if(L->head == NULL){
L->head = I;
L->tail = I;
I->next = NULL;
I->prev = NULL;
}
else{
L->tail->next = I;
I->prev = L->tail;
L->tail = I;
I->next = NULL;
}
}
int Count(struct List *L) {
int count = 0;
struct Item *I = L->head;
while (I != NULL) {
I = I->next;
count++;
}
return count;
}
void PrintList(struct List *L) {
if (Count(L) == 0){
printf("List is null");
}
else {
int count = 1;
struct Item *I = L->head;
while (I != NULL) {
printf("%d\t%p\t%p\t%p\n", count, I, I->prev, I->next);
I = I->next;
count++;
}
}
}
struct Item* GetItem(struct List *L, int id) {
int count = 0;
struct Item *I = L->head;
while (I != NULL) {
if (count == id)
break;
count++;
}
return I;
}
int GetIndex(struct List *L, struct Item *item) {
int id = 1;
struct Item *I = L->head;
while (I != NULL) {
if (item == I)
break;
id++;
}
return id;
}
void Insert(struct List *L, struct Item *I, int id) {
struct Item *ItemInID = GetItem(L, id);
if(ItemInID == NULL){
Add(L, I);
}
else if(id == 0) {
L->head->prev = I;
I->next = L->head;
L->head = I;
I->prev = NULL;
}
else {
I->next = ItemInID;
I->prev = ItemInID->prev;
ItemInID->prev->next = I;
ItemInID->prev = I;
}
}
struct Item* Remove(struct List *L, int id) {
struct Item *I = GetItem(L, id);
if (I == NULL) {
return NULL;
}
else if (L->head == L->tail) {
L->head = NULL;
L->tail = NULL;
}
else if(I == L->head) {
L->head = L->head->next;
L->head->prev = NULL;
}
else if (I == L->tail) {
L->tail = L->tail->prev;
L->tail->next = NULL;
}
else {
I->prev->next = I->next;
I->next->prev = I->prev;
}
return I;
}
void Delete(struct List *L, int id) {
struct Item *I = Remove(L, id);
if(I)
free(I);
}
void Clear(struct List *L) {
while (L->head != NULL) {
Delete(L, 1);
}
}