1
1
//
2
2
// Created by 张成 on 16/4/26.
3
3
//
4
-
5
4
#include " CListNode.h"
6
5
#include < cstddef>
7
6
#include < iostream>
7
+ #include < memory>
8
+ #include " php.h"
9
+
8
10
using namespace std ;
9
11
12
+ CListNode::CListNode ()
13
+ {
14
+ this ->size = 0 ;
15
+ this ->head = nullptr ;
16
+ this ->tail = nullptr ;
17
+ }
18
+
19
+ CListNode::~CListNode ()
20
+ {
21
+
22
+ if (this ->head != nullptr ){
23
+ list_node *current = this ->head ;
24
+ list_node *pNext = head->next ;
25
+
26
+ while (current != nullptr )
27
+ {
28
+ efree (current);
29
+ current = nullptr ;
30
+ if (pNext){
31
+ current = pNext;
32
+ pNext = current->next ;
33
+ }
34
+ }
35
+
36
+ }
37
+
38
+ }
39
+
10
40
CListNode* CListNode::create ()
11
41
{
12
42
CListNode* obj = new CListNode;
13
- obj->head = nullptr ;
14
- obj->tail = nullptr ;
15
43
return obj;
16
44
}
17
45
@@ -21,49 +49,105 @@ int CListNode::get_length() const
21
49
return this ->size ;
22
50
}
23
51
24
- int CListNode::add_value (int val) {
25
-
26
- list_node* node = (list_node*)malloc (sizeof (list_node));
27
- if (node == nullptr ){
52
+ int CListNode::add_value (zval* val) {
53
+
54
+ list_node* node = (list_node*)emalloc (sizeof (list_node));
55
+ if (node == nullptr ){
28
56
return -1 ;
29
57
}
30
- node->value = val;
58
+ zval *new_val;
59
+ MAKE_STD_ZVAL (new_val);
60
+ *new_val = *val;
61
+ zval_copy_ctor (new_val);// 如果是非标量 添加引用计数
62
+ // convert_to_string(new_val);//转成string
63
+
64
+ node->value = new_val;
31
65
node->next = nullptr ;
32
66
33
- if (this ->head == nullptr ){
67
+ if (this ->head == nullptr ){
34
68
head = node;
35
- node ->prev = nullptr ;
69
+ head ->prev = nullptr ;
36
70
tail = node;
37
- }else {
71
+ }
72
+ else {
38
73
tail->next = node;
39
74
node->prev = tail;
40
75
tail = node;
41
76
}
42
-
77
+
43
78
this ->size = this ->size + 1 ;
44
79
45
80
return 0 ;
46
81
}
47
82
48
83
// fetch出指定 索引节点的值
49
- int CListNode::fetch_index (int index, int &data ) {
84
+ zval* CListNode::fetch_index (int index) {
50
85
51
- if (index <= 0 || index > this ->size ){
52
- return 0 ;
86
+ if (index <= 0 || index > this ->size ){
87
+ return nullptr ;
53
88
}
54
89
55
90
list_node* pHead = head;
56
- if (pHead != nullptr ){
57
- while (index > 1 ){
91
+ if (pHead != nullptr ){
92
+ while (index > 1 ){
58
93
pHead = pHead->next ;
59
94
--index ;
60
95
}
61
96
62
- data = pHead->value ;
63
- }else {
64
- return 0 ;
97
+ return pHead->value ;
98
+ }
99
+ else {
100
+ return nullptr ;
65
101
}
66
-
67
- return 1 ;
68
102
69
103
}
104
+
105
+ int CListNode::del_value (int index)
106
+ {
107
+ if (index <= 0 || index > this ->size )
108
+ {
109
+ return -1 ;
110
+ }
111
+
112
+ list_node* pCurrent = head;
113
+ list_node* tmp = nullptr ;
114
+
115
+ if (pCurrent != nullptr ){
116
+ int i = 1 ;
117
+ if (i == index ){
118
+
119
+ head = head->next ;
120
+ head->prev = nullptr ;
121
+ efree (pCurrent);
122
+ --this ->size ;
123
+ return 0 ;
124
+ }
125
+ else {
126
+ while (pCurrent != nullptr ){
127
+ ++i;
128
+ tmp = pCurrent;
129
+ pCurrent = pCurrent->next ;
130
+ if (i == index && pCurrent != nullptr ){
131
+ tmp->next = pCurrent->next ;
132
+ if (pCurrent->next != nullptr ){
133
+ pCurrent->next ->prev = tmp;
134
+ }
135
+ else {
136
+ tail = tmp;
137
+ }
138
+ zval_dtor (pCurrent->value );
139
+ efree (pCurrent);
140
+ --this ->size ;
141
+ return 0 ;
142
+ }
143
+
144
+ }
145
+
146
+ return -1 ;
147
+ }
148
+
149
+ }
150
+ else {
151
+ return -1 ;
152
+ }
153
+ }
0 commit comments