Created
December 17, 2016 11:30
-
-
Save otaks/72e1688830d73eb1c0646911209a9442 to your computer and use it in GitHub Desktop.
list sample2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <stdlib.h> | |
#define FALSE 0 | |
#define TRUE 1 | |
#define FREE(X) free(X);X=NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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