-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDBLinkedList.c
67 lines (52 loc) · 1.24 KB
/
DDBLinkedList.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
#include <stdlib.h>
#include "DDBLinkedList.h"
void ListInit(List * plist)
{
plist->head = (Node *)malloc(sizeof(Node));
plist->tail = (Node *)malloc(sizeof(Node));
plist->head->prev = plist->tail->next = NULL;
plist->head->next = plist->tail;
plist->tail->prev = plist->head;
plist->numOfData = 0;
}
void LInsert(List * plist, Data data)
{
Node * newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = plist->tail->prev;
newNode->prev->next = newNode;
plist->tail->prev = newNode;
newNode->next = plist->tail;
(plist->numOfData)++;
}
int LFirst(List * plist, Data * pdata)
{
if(!(plist->numOfData))
return FALSE;
plist->cur = plist->head->next;
*pdata = plist->cur->data;
return TRUE;
}
int LNext(List * plist, Data * pdata)
{
if(plist->cur->next == plist->tail)
return FALSE;
plist->cur = plist->cur->next;
*pdata = plist->cur->data;
return TRUE;
}
Data LRemove(List * plist)
{
Data rdata = plist->cur->data;
Node * before = plist->cur->prev;
plist->cur->next->prev = plist->cur->prev;
plist->cur->prev->next = plist->cur->next;
free(plist->cur);
plist->cur = before;
(plist->numOfData)--;
return rdata;
}
int LCount(List * plist)
{
return plist->numOfData;
}