Skip to content

Commit 20423c9

Browse files
committed
update cpp class
1 parent 3c6feab commit 20423c9

File tree

3 files changed

+176
-85
lines changed

3 files changed

+176
-85
lines changed

CListNode.cc

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
11
//
22
// Created by 张成 on 16/4/26.
33
//
4-
54
#include "CListNode.h"
65
#include <cstddef>
76
#include <iostream>
7+
#include <memory>
8+
#include "php.h"
9+
810
using namespace std;
911

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+
1046
CListNode* CListNode::create()
1147
{
1248
CListNode* obj = new CListNode;
13-
obj->head = nullptr;
14-
obj->tail = nullptr;
1549
return obj;
1650
}
1751

@@ -21,49 +55,55 @@ int CListNode::get_length() const
2155
return this->size;
2256
}
2357

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){
2862
return -1;
2963
}
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;
3171
node->next = nullptr;
3272

33-
if(this->head == nullptr){
73+
if (this->head == nullptr){
3474
head = node;
35-
node->prev = nullptr;
75+
head->prev = nullptr;
3676
tail = node;
37-
}else{
77+
}
78+
else{
3879
tail->next = node;
3980
node->prev = tail;
4081
tail = node;
4182
}
42-
83+
4384
this->size = this->size + 1;
44-
85+
cout << "CListNode Add" << endl;
4586
return 0;
4687
}
4788

4889
// fetch出指定 索引节点的值
49-
int CListNode::fetch_index(int index, int &data) {
90+
zval* CListNode::fetch_index(int index) {
5091

51-
if(index <= 0 || index > this->size){
52-
return 0;
92+
if (index <= 0 || index > this->size){
93+
return nullptr;
5394
}
5495

5596
list_node* pHead = head;
56-
if(pHead != nullptr){
57-
while(index > 1){
97+
if (pHead != nullptr){
98+
while (index > 1){
5899
pHead = pHead->next;
59100
--index;
60101
}
61102

62-
data = pHead->value;
63-
}else{
64-
return 0;
103+
return pHead->value;
104+
}
105+
else{
106+
return nullptr;
65107
}
66-
67-
return 1;
68108

69109
}

CListNode.h

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
1-
//
2-
// Created by 张成 on 16/4/26.
3-
//
4-
5-
#ifndef LISTNODE_CLISTNODE_H
6-
#define LISTNODE_CLISTNODE_H
7-
8-
1+
#ifndef _CLISTNODE_H_
2+
#define _CLISTNODE_H_
3+
#include "php.h"
94
typedef struct list_node{
10-
int value;
5+
zval* value;
6+
//int value;
117
list_node *prev;
128
list_node *next;
139
}list_node;
1410

1511
class CListNode {
1612
public:
17-
13+
CListNode();
14+
~CListNode();
1815
static CListNode* create();
1916

20-
int add_value(int val);
17+
int add_value(zval *val);
2118

22-
int fetch_index(int index,int &data);
19+
zval* fetch_index(int index);
2320

2421
int get_length() const;
2522
private:
2623

27-
CListNode(){};
28-
~CListNode(){};
2924
int size;
3025
list_node *head;
3126
list_node *tail;
3227
};
33-
34-
35-
#endif //LISTNODE_CLISTNODE_H
28+
#endif

0 commit comments

Comments
 (0)