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
+ cout << " CListNode Destory" << endl;
22
+
23
+ if (this ->head != nullptr ){
24
+ list_node *current = this ->head ;
25
+ list_node *pNext = head->next ;
26
+
27
+ while (current != nullptr )
28
+ {
29
+
30
+ cout << " CListNode Destory -- node->" << Z_STRVAL_P (current->value ) << endl;
31
+ // zval_dtor(current->value);
32
+ efree (current);
33
+ current = nullptr ;
34
+ if (pNext){
35
+ current = pNext;
36
+ pNext = current->next ;
37
+ }
38
+
39
+ cout << " CListNode Destory -- while" << endl;
40
+ }
41
+
42
+ }
43
+
44
+ }
45
+
10
46
CListNode* CListNode::create ()
11
47
{
12
48
CListNode* obj = new CListNode;
13
- obj->head = nullptr ;
14
- obj->tail = nullptr ;
15
49
return obj;
16
50
}
17
51
@@ -21,49 +55,55 @@ int CListNode::get_length() const
21
55
return this ->size ;
22
56
}
23
57
24
- int CListNode::add_value (int val) {
25
-
26
- list_node* node = (list_node*)malloc (sizeof (list_node));
27
- if (node == nullptr ){
58
+ int CListNode::add_value (zval* val) {
59
+
60
+ list_node* node = (list_node*)emalloc (sizeof (list_node));
61
+ if (node == nullptr ){
28
62
return -1 ;
29
63
}
30
- node->value = val;
64
+ zval *new_val;
65
+ MAKE_STD_ZVAL (new_val);
66
+ *new_val = *val;
67
+ zval_copy_ctor (new_val);
68
+ convert_to_string (new_val);// 转成string
69
+
70
+ node->value = new_val;
31
71
node->next = nullptr ;
32
72
33
- if (this ->head == nullptr ){
73
+ if (this ->head == nullptr ){
34
74
head = node;
35
- node ->prev = nullptr ;
75
+ head ->prev = nullptr ;
36
76
tail = node;
37
- }else {
77
+ }
78
+ else {
38
79
tail->next = node;
39
80
node->prev = tail;
40
81
tail = node;
41
82
}
42
-
83
+
43
84
this ->size = this ->size + 1 ;
44
-
85
+ cout << " CListNode Add " << endl;
45
86
return 0 ;
46
87
}
47
88
48
89
// fetch出指定 索引节点的值
49
- int CListNode::fetch_index (int index, int &data ) {
90
+ zval* CListNode::fetch_index (int index) {
50
91
51
- if (index <= 0 || index > this ->size ){
52
- return 0 ;
92
+ if (index <= 0 || index > this ->size ){
93
+ return nullptr ;
53
94
}
54
95
55
96
list_node* pHead = head;
56
- if (pHead != nullptr ){
57
- while (index > 1 ){
97
+ if (pHead != nullptr ){
98
+ while (index > 1 ){
58
99
pHead = pHead->next ;
59
100
--index;
60
101
}
61
102
62
- data = pHead->value ;
63
- }else {
64
- return 0 ;
103
+ return pHead->value ;
104
+ }
105
+ else {
106
+ return nullptr ;
65
107
}
66
-
67
- return 1 ;
68
108
69
109
}
0 commit comments