-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
99 lines (85 loc) · 2.29 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
/***********************************************************************************************************************
Implementation file : list.c
Author : <Costas Chatzopoulos -1115201300202- 29/10/2017>
Purpose : list structure
***********************************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "list.h"
typedef struct node *nodePtr;
struct list {
nodePtr start;
nodePtr current;
};
struct node {
nodePtr right, left;
listItemType *data;
};
int _existsNode(nodePtr node) {
return node != NULL;
}
int _isFirst(nodePtr target) {
return target->left == NULL;
}
int _isLast(nodePtr node) {
return node->right == NULL;
}
nodePtr _createNode() {
nodePtr node = (nodePtr) malloc(sizeof(struct node));
if (node != NULL) {
node->right = NULL;
node->left = NULL;
return node;
} else
return NULL;
}
nodePtr _attachNode(listPtr list, nodePtr newNode) {
assert(list != NULL);
assert(newNode != NULL);
newNode->right = list->start;
if (list->start != NULL)
list->start->left = newNode;
list->start = newNode;
list->current = newNode;
return newNode;
}
bool listExists(listPtr *list) {
return (*list != NULL);
}
void listCreate(listPtr *list) {
assert(*list == NULL);
*list = (listPtr) malloc(sizeof(struct list));
if ((*list) != NULL) {
(*list)->start = NULL;
(*list)->current = NULL;
}
}
listItemType *listInsert(listPtr list, listItemType *data) {
assert(list != NULL);
assert(data != NULL);
nodePtr newNode = _createNode();
if (newNode != NULL) {
newNode = _attachNode(list, newNode);
if (newNode != NULL) {
list->current = newNode;
list->current->data = data;
return list->current->data;
}
}
}
listItemType *listGetFirstData(listPtr list) {
assert(list != NULL);
if (list->start != NULL)
return list->start->data;
else
return NULL;
}
listItemType *listNext(listPtr list) {
assert(list != NULL);
if (!_isLast(list->current)) {
list->current = list->current->right;
return list->current->data;
}
return NULL;
}