Create a gist now

Instantly share code, notes, and snippets.

@otaks /list.c
Created Dec 17, 2016

What would you like to do?
list sample2
#include <stdlib.h>
#include <string.h>
#include "list.h"
//リスト作成
list list_create( ) {
list t = calloc( sizeof( struct _list ), 1 );
return t;
}
//リスト削除
void list_delete( list t ) {
if( !t )return;
node n = t->first;
while( n != NULL ) {
node a = n;
n = n->next;
FREE( a );
}
FREE( t );
}
//ノード追加
int list_add( list t, void* v ) {
if( !t || !v ) return FALSE;
//ノード作成、
node newNode = ( node )calloc( sizeof( struct _node ), 1 );
if( !newNode ) {
return FALSE;
}
newNode->data = v;
//末尾ノードにつなぐ
if( t->last) {
t->last->next = newNode;
t->last = newNode;
}
else {
t->first = t->last = newNode;
}
( t->num )++;
return TRUE;
}
#pragma once
#include "sysCommon.h"
//ノード
typedef struct _node* node;
struct _node {
void* data; //データ
node next; //次ノード
};
//リスト
typedef struct _list* list;
struct _list {
int num; //ノード数
node first; //先頭ノード
node last; //末尾ノード
};
/**
*リスト作成。
*
*@retern NULL以外:作成したリスト、NULL:失敗
*/
list list_create( );
/**
*リスト削除。
*リストを追加終わった時に呼ぶこと
*
*@param t リスト
*/
void list_delete( list t );
/**
*ノード追加。
*
*@param t リスト
*@param v 追加ノード
*@return TRUE:成功、FALSE:失敗
*/
int list_add( list t, void* v );
#pragma once
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
#define FREE(X) free(X);X=NULL;
#include<stdio.h>
#include "list.h"
#include "memoryPool.h"
typedef struct _aaa aaa;
struct _aaa {
int a;
int b;
};
int main() {
poolHandle ph = memoryPool_Create();
list intList = list_create();
list stList = list_create();
int *i1 = memoryPool_Alloc( ph, sizeof( int ) );
*i1 = 2;
list_add( intList, i1 );
printf( "%d\n", intList->num);
printf( "%d\n", *( int * )( intList->first->data ) );
aaa* pa = memoryPool_Alloc( ph, sizeof( aaa ) );
pa->a = 3;
pa->b = 4;
list_add( stList, pa );
aaa* pa2 = memoryPool_Alloc( ph, sizeof( aaa ) );
pa2->a = 11;
pa2->b = 231;
list_add( stList, pa2 );
printf( "%d\n", stList->num );
node tmp = stList->first;
while( tmp ) {
aaa* p = ( aaa* )tmp->data;
printf( "%d\n", p->a );
printf( "%d\n", p->b );
tmp = tmp->next;
}
list_delete( intList );
list_delete( stList );
memoryPool_Release( ph );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment