Skip to content

Commit 5cea1b9

Browse files
authored
Merge pull request #1 from phpzc-net/dev-0.0.1
Dev 0.0.1
2 parents 3c6feab + e0e48bf commit 5cea1b9

File tree

3 files changed

+236
-85
lines changed

3 files changed

+236
-85
lines changed

CListNode.cc

Lines changed: 106 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
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+
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+
1040
CListNode* CListNode::create()
1141
{
1242
CListNode* obj = new CListNode;
13-
obj->head = nullptr;
14-
obj->tail = nullptr;
1543
return obj;
1644
}
1745

@@ -21,49 +49,105 @@ int CListNode::get_length() const
2149
return this->size;
2250
}
2351

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){
2856
return -1;
2957
}
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;
3165
node->next = nullptr;
3266

33-
if(this->head == nullptr){
67+
if (this->head == nullptr){
3468
head = node;
35-
node->prev = nullptr;
69+
head->prev = nullptr;
3670
tail = node;
37-
}else{
71+
}
72+
else{
3873
tail->next = node;
3974
node->prev = tail;
4075
tail = node;
4176
}
42-
77+
4378
this->size = this->size + 1;
4479

4580
return 0;
4681
}
4782

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

51-
if(index <= 0 || index > this->size){
52-
return 0;
86+
if (index <= 0 || index > this->size){
87+
return nullptr;
5388
}
5489

5590
list_node* pHead = head;
56-
if(pHead != nullptr){
57-
while(index > 1){
91+
if (pHead != nullptr){
92+
while (index > 1){
5893
pHead = pHead->next;
5994
--index;
6095
}
6196

62-
data = pHead->value;
63-
}else{
64-
return 0;
97+
return pHead->value;
98+
}
99+
else{
100+
return nullptr;
65101
}
66-
67-
return 1;
68102

69103
}
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+
}

CListNode.h

Lines changed: 11 additions & 18 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);
21-
22-
int fetch_index(int index,int &data);
17+
int add_value(zval *val);
18+
int del_value(int index);
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)