-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLlist.cpp
88 lines (76 loc) · 2.21 KB
/
DLlist.cpp
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
#include "allmyfile.h"
/*initialize a dll-type node*/
void init_dl_list_node(dl_list_node * new_node, int cached_key_1, int cached_value_1, int cached_pkt_1, dl_list_head * list_head_p_temp)
{
new_node->next = NULL;
new_node->previous = NULL;
new_node->cached_key = cached_key_1;
new_node->cached_value = cached_value_1;
new_node->cached_pkt = cached_pkt_1;
new_node->list_head_p = list_head_p_temp;
return;
}
/*insert a node to the head of the list*/
void insert_node_head(dl_list_head * list_head, dl_list_node * new_node)
{
if(list_head->tail == NULL)
{
//if a list is empty, insert a new node
list_head->head = list_head->tail = new_node;
}
else
{
new_node->next = list_head->head;
new_node->next->previous = new_node;
list_head->head = new_node;
}
list_head->list_length ++;
return;
}
/*delete the node in the tail*/
int delete_node_tail(dl_list_head * list_head)
{
dl_list_node * temp_node;
temp_node = list_head->tail;
list_head->tail = temp_node->previous;
list_head->tail->next = NULL;
int i = temp_node->cached_key;
delete temp_node;
list_head->list_length --;
return i;
}
/*delete a node in the list*/
void delete_node_pointed(dl_list_head * list_head_p, dl_list_node * list_ptr)
{
if(list_ptr->next == NULL && list_ptr->previous == NULL)//if a list only have one element
{
delete list_ptr;
list_head_p->head = NULL;
list_head_p->tail = NULL;
}
else if(list_ptr->next == NULL)//delete the tail of the list
{
list_ptr->previous->next = NULL;
list_head_p->tail = list_ptr->previous;
delete list_ptr;
}
else if(list_ptr->previous == NULL)//delete the head of the list
{
list_ptr->next->previous = NULL;
list_head_p->head = list_ptr->next;
delete list_ptr;
}
else
{
list_ptr->previous->next = list_ptr->next;
list_ptr->next->previous = list_ptr->previous;
delete list_ptr;
}
list_head_p->list_length --;
return;
}
/*insert a new node before an old node*/
void insert_data_before(dl_list_node * old_node, dl_list_node * new_node)
{
return;
}