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 all commits
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
128 changes: 106 additions & 22 deletions CListNode.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
//
// 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()
{

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

while (current != nullptr)
{
efree(current);
current = nullptr;
if (pNext){
current = pNext;
pNext = current->next;
}
}

}

}

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

Expand All @@ -21,49 +49,105 @@ 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;

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;

}

int CListNode::del_value(int index)
{
if (index <= 0 || index > this->size)
{
return -1;
}

list_node* pCurrent = head;
list_node* tmp = nullptr;

if (pCurrent != nullptr){
int i = 1;
if (i == index){

head = head->next;
head->prev = nullptr;
efree(pCurrent);
--this->size;
return 0;
}
else{
while (pCurrent != nullptr){
++i;
tmp = pCurrent;
pCurrent = pCurrent->next;
if (i == index && pCurrent != nullptr){
tmp->next = pCurrent->next;
if (pCurrent->next != nullptr){
pCurrent->next->prev = tmp;
}
else{
tail = tmp;
}
zval_dtor(pCurrent->value);
efree(pCurrent);
--this->size;
return 0;
}

}

return -1;
}

}
else{
return -1;
}
}
29 changes: 11 additions & 18 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 fetch_index(int index,int &data);
int add_value(zval *val);
int del_value(int index);
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