69,382
社区成员
发帖
与我相关
我的任务
分享
typedef int Item;
struct node {
Item item;
struct node * left;
struct node * right;
};
typedef struct node node_t;
struct tree{
node_t * root;
int size;
};
typedef struct tree tree_t;
/*通用部分:*/
/* 定义打印节点回调函数类型 */
#pragma pack(1)
typedef struct inode
{
struct inode *next;
char nodedata[0];
}node;
#pragma pack()
typedef void (*CALLBACK)(node *);
/* 使用该宏判断结构的长度,由于不定长度的结构在使用sizeof时
得到的值会混乱,故使用该宏 */
#define offset(TYPE, MEMBER) \
((size_t)&((TYPE *)0)->MEMBER)
/* 考虑节点内数据部分结构无关性 */
int MakeNode (node **pnode, void *data, int idatalen)
{
int inodelen = offset(node, nodedata);
if ((*pnode = (node *)malloc(inodelen + idatalen)) == NULL)
{
return -1;
}
memcpy ((unsigned char *)&((*pnode)->nodedata),
(unsigned char *)data, idatalen);
(*pnode)->next = NULL;
return 0;
}
void FreeList (node **pnode)
{
node *p = *pnode;
node *ptmp = NULL;
if (p == NULL)
{
return;
}
do
{
ptmp = p->next;
free(p);
p = ptmp;
}while(p != NULL);
*pnode = NULL;
}
void PrintList (CALLBACK p, node *head)
{
if (NULL == p)
{
return;
}
p(head);
}
/* 测试代码 */
/* 测试函数内容 */
#ifdef _MAIN_TEST_
#pragma pack(1)
typedef struct iElemType
{
char pstr[40];
int iparainout;
int iparatype;
char pparaname[40];
}ElemType;
#pragma pack()
void MYPrintList (node *head)
{
node *p = head;
ElemType *pnode = NULL;
fprintf (stderr, "%s\n", "*head");
while (p != NULL)
{
pnode = (ElemType *)&(p->nodedata);
/* 打印列表 */
fprintf (stdout, "===>[%s] | [%s] | [%d] | [%d] ===\n", \
pnode->pstr,
pnode->pparaname,
pnode->iparainout,
pnode->iparatype);
p = p->next;
}
}
int main (int argc, char **argv)
{
int iret = -1;
int i = 0;
node *head = NULL;
node *tail = NULL;
ElemType mydata;
CALLBACK p = NULL;
memset ((unsigned char *)&mydata, 0, sizeof(ElemType));
mydata.iparainout = 0;
mydata.iparatype = 0;
strcat (mydata.pparaname, "MYTEST0000");
strcat (mydata.pstr, "LONG LONG LONG LONG STR");
p = MYPrintList;
if (MakeNode (&tail, (void *)&mydata, sizeof(ElemType)) != 0)
{
FreeList (&head);
exit(0);
}
head = tail;
for (i=1;i<=6;i++)
{
memset ((unsigned char *)&mydata, 0, sizeof(ElemType));
mydata.iparainout = i;
mydata.iparatype = i;
sprintf (mydata.pparaname, "MYTEST %4d", i);
sprintf (mydata.pstr, "str %4d", i);
if (MakeNode (&(tail->next), (void *)&mydata, sizeof(ElemType)) != 0)
{
FreeList (&head);
exit(0);
}
tail = tail->next;
}
PrintList(p, head);
// PrintList (&MYPrintList);
FreeList (&head);
return 0;
}
#endif
List list1,list2;
list1= new ArrayList();
list2 = new ArrayList();
list1.add(1);//list1 里装的是整数类型的
list1.add(2);
list2.add("hello");//list2 里装的是 string
list2.add("world");
List intList;
List floatList