-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.c
157 lines (144 loc) · 4.61 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
147
148
149
150
151
152
153
154
155
156
157
/*------------------------------------------------------------------------------
-- SOURCE FILE: List.c - Contains some basic functionality for a
-- linked-list.
--
-- PROGRAM: RFID Reader - Enterprise Edition
--
-- FUNCTIONS:
-- DWORD AddToBack(CHAR_LIST**, CHAR*, DWORD);
-- DWORD GetFromList(CHAR_LIST*, UINT);
-- CHAR* RemoveFromFront(CHAR_LIST**, DWORD);
--
--
-- DATE: Nov 06, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Dean Morin
--
-- PROGRAMMER: Dean Morin
--
-- NOTES:
-- These linked list functions are designed to process incoming characters on a
-- buffer. The functions take or return entire strings of characters, and handle
-- the conversion to a linked list internally. More specifically, the functions
-- will treat the lists as queues.
------------------------------------------------------------------------------*/
#include "List.h"
/*------------------------------------------------------------------------------
-- FUNCTION: AddToBack
--
-- DATE: Nov 06, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Dean Morin
--
-- PROGRAMMER: Dean Morin
--
-- INTERFACE: DWORD AddToBack(CHAR_LIST** pHead, CHAR* psBuf, DWORD dwLength)
-- pHead - the first node in the list
-- psBuf - the characters to be added
-- dwLength - the length of psBuf
--
-- RETURNS: The length of the linked-list.
--
-- NOTES:
-- Adds the contents of psBuf to the end of the list.
------------------------------------------------------------------------------*/
DWORD AddToBack(CHAR_LIST** pHead, CHAR* psBuf, DWORD dwLength) {
CHAR_LIST* newNode = NULL;
CHAR_LIST* p = NULL;
DWORD dwCount = 0;
DWORD i = 0;
p = *pHead;
if (p == NULL) {
newNode = (CHAR_LIST*) malloc(sizeof(CHAR_LIST));
newNode->c = psBuf[i++];
newNode->next = NULL;
p = newNode;
*pHead = newNode;
} else {
while (p->next != NULL) {
p = p->next;
dwCount++;
}
}
dwCount++;
for (i; i < dwLength; i++) {
newNode = (CHAR_LIST*) malloc(sizeof(CHAR_LIST));
newNode->c = psBuf[i];
newNode->next = NULL;
p->next = newNode;
p = p->next;
dwCount++;
}
return dwCount;
}
/*------------------------------------------------------------------------------
-- FUNCTION: GetFromList
--
-- DATE: Nov 06, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Dean Morin
--
-- PROGRAMMER: Dean Morin
--
-- INTERFACE: DWORD GetFromList(CHAR_LIST* p, UINT ordinal)
-- p - the first node in the list
-- uiOrdinal - the node to get the value from
--
-- RETURNS: The value in the node specified by uiOrdinal.
--
-- NOTES:
-- Returns the value in the node specified by uiOrdinal (if
-- uiOrdinal = 2 for example, then it returns the value in the
-- 2nd node in the list).
------------------------------------------------------------------------------*/
DWORD GetFromList(CHAR_LIST* p, UINT uiOrdinal) {
UINT i = 0;
for (i = 1; i < uiOrdinal; i++) {
p = p->next;
}
return p->c;
}
/*------------------------------------------------------------------------------
-- FUNCTION: AddToBack
--
-- DATE: Nov 06, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Dean Morin
--
-- PROGRAMMER: Dean Morin
--
-- INTERFACE: CHAR* RemoveFromFront(CHAR_LIST** pHead, DWORD dwLength)
-- pHead - the first node in the list
-- dwLength - the number of nodes to remove
--
-- RETURNS: The contents of the first n nodes in the list,
-- where n = dwLength
--
-- NOTES:
-- Retrieves and removes a specified amount of nodes in the form
-- of a character array.
------------------------------------------------------------------------------*/
CHAR* RemoveFromFront(CHAR_LIST** pHead, DWORD dwLength) {
CHAR_LIST* p = NULL;
CHAR_LIST* tracer = NULL;
UINT i = 0;
CHAR* removed = NULL;
p = *pHead;
removed = (CHAR*) malloc(sizeof(CHAR) * dwLength);
for (i = 0; i < dwLength; i++) {
removed[i] = p->c;
tracer = p;
p = p->next;
free(tracer);
}
*pHead = p;
return removed;
}