Skip to content

Dev 0.0.1 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
update cpp class
  • Loading branch information
phpzc committed May 3, 2016
commit 20423c9c5f744653168821e8daa6d7bf385236ce
86 changes: 63 additions & 23 deletions CListNode.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
//
// Created by 张成 on 16/4/26.
//

#include "CListNode.h"
#include <cstddef>
#include <iostream>
#include <memory>
#include "php.h"

using namespace std;

CListNode::CListNode()
{
this->size = 0;
this->head = nullptr;
this->tail = nullptr;
}

CListNode::~CListNode()
{
cout << "CListNode Destory" << endl;

if (this->head != nullptr){
list_node *current = this->head;
list_node *pNext = head->next;

while (current != nullptr)
{

cout << "CListNode Destory -- node->" << Z_STRVAL_P(current->value) << endl;
//zval_dtor(current->value);
efree(current);
current = nullptr;
if (pNext){
current = pNext;
pNext = current->next;
}

cout << "CListNode Destory -- while" << endl;
}

}

}

CListNode* CListNode::create()
{
CListNode* obj = new CListNode;
obj->head = nullptr;
obj->tail = nullptr;
return obj;
}

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

int CListNode::add_value(int val) {

list_node* node = (list_node*)malloc(sizeof(list_node));
if(node == nullptr){
int CListNode::add_value(zval* val) {
list_node* node = (list_node*)emalloc(sizeof(list_node));
if (node == nullptr){
return -1;
}
node->value = val;
zval *new_val;
MAKE_STD_ZVAL(new_val);
*new_val = *val;
zval_copy_ctor(new_val);
convert_to_string(new_val);//转成string

node->value = new_val;
node->next = nullptr;

if(this->head == nullptr){
if (this->head == nullptr){
head = node;
node->prev = nullptr;
head->prev = nullptr;
tail = node;
}else{
}
else{
tail->next = node;
node->prev = tail;
tail = node;
}

this->size = this->size + 1;

cout << "CListNode Add" << endl;
return 0;
}

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

if(index <= 0 || index > this->size){
return 0;
if (index <= 0 || index > this->size){
return nullptr;
}

list_node* pHead = head;
if(pHead != nullptr){
while(index > 1){
if (pHead != nullptr){
while (index > 1){
pHead = pHead->next;
--index;
}

data = pHead->value;
}else{
return 0;
return pHead->value;
}
else{
return nullptr;
}

return 1;

}
27 changes: 10 additions & 17 deletions CListNode.h
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
//
// Created by 张成 on 16/4/26.
//

#ifndef LISTNODE_CLISTNODE_H
#define LISTNODE_CLISTNODE_H


#ifndef _CLISTNODE_H_
#define _CLISTNODE_H_
#include "php.h"
typedef struct list_node{
int value;
zval* value;
//int value;
list_node *prev;
list_node *next;
}list_node;

class CListNode {
public:

CListNode();
~CListNode();
static CListNode* create();

int add_value(int val);
int add_value(zval *val);

int fetch_index(int index,int &data);
zval* fetch_index(int index);

int get_length() const;
private:

CListNode(){};
~CListNode(){};
int size;
list_node *head;
list_node *tail;
};


#endif //LISTNODE_CLISTNODE_H
#endif
Loading