当前位置: 技术问答>linux和unix
共享内存问题
来源: 互联网 发布时间:2015-11-30
本文导语: //AVL树结构 typedef struct _TreeNode { char strData[21]; int npos; int balanceFactor; struct _TreeNode *left; struct _TreeNode *right; }TREENODE,*PTREENODE,**PPTREENODE; 我想将这种结构的AVL树放到共享内存中使用,从文件中每读入一条记录,...
//AVL树结构
typedef struct _TreeNode
{
char strData[21];
int npos;
int balanceFactor;
struct _TreeNode *left;
struct _TreeNode *right;
}TREENODE,*PTREENODE,**PPTREENODE;
我想将这种结构的AVL树放到共享内存中使用,从文件中每读入一条记录,就插入一个节点,最后生成一颗完整的树,请问该怎么用?
现在的代码为
int CreateTree()
{
FILE *fp = NULL;
char buffer[256];
int size =0;
int npos = 0;
char strData[32];
//跟节点
pRoot = NULL;
fp = fopen(TEST_PRODUCT_LIST,"rb");
if( fp == NULL)
return 0;
memset( buffer, 0x00, 256);
size = fread(buffer,sizeof(char),PRODUCT_LENGTH,fp);
while( size == PRODUCT_LENGTH)
{
//插入新节点
memset( strData, 0x00, 32);
strncpy( strData, buffer,13);
InsertNode(strData,npos);
npos ++;
size = fread(buffer,sizeof(char),PRODUCT_LENGTH,fp);
}
fclose(fp);
return 0;
}
int InsertNode(char *strData, int npos)
{
PTREENODE pNewNode;
PTREENODE pNode = pRoot;
PTREENODE pFindNode = NULL;
int nFind = 0;
// 供 AVLInsert 重新平衡结点的标志
int reviseBalanceFactor = 0;
pNewNode = ( TREENODE * ) malloc( sizeof( TREENODE ) );
memset(pNewNode->strData,0x00, sizeof(pNewNode->strData));
strncpy( pNewNode->strData, strData,13);
pNewNode->npos = npos;
pNewNode->left = NULL;
pNewNode->right = NULL;
AVLInsertNode(&pNode,pNewNode,&reviseBalanceFactor);
pRoot = pNode;
return 0;
}
typedef struct _TreeNode
{
char strData[21];
int npos;
int balanceFactor;
struct _TreeNode *left;
struct _TreeNode *right;
}TREENODE,*PTREENODE,**PPTREENODE;
我想将这种结构的AVL树放到共享内存中使用,从文件中每读入一条记录,就插入一个节点,最后生成一颗完整的树,请问该怎么用?
现在的代码为
int CreateTree()
{
FILE *fp = NULL;
char buffer[256];
int size =0;
int npos = 0;
char strData[32];
//跟节点
pRoot = NULL;
fp = fopen(TEST_PRODUCT_LIST,"rb");
if( fp == NULL)
return 0;
memset( buffer, 0x00, 256);
size = fread(buffer,sizeof(char),PRODUCT_LENGTH,fp);
while( size == PRODUCT_LENGTH)
{
//插入新节点
memset( strData, 0x00, 32);
strncpy( strData, buffer,13);
InsertNode(strData,npos);
npos ++;
size = fread(buffer,sizeof(char),PRODUCT_LENGTH,fp);
}
fclose(fp);
return 0;
}
int InsertNode(char *strData, int npos)
{
PTREENODE pNewNode;
PTREENODE pNode = pRoot;
PTREENODE pFindNode = NULL;
int nFind = 0;
// 供 AVLInsert 重新平衡结点的标志
int reviseBalanceFactor = 0;
pNewNode = ( TREENODE * ) malloc( sizeof( TREENODE ) );
memset(pNewNode->strData,0x00, sizeof(pNewNode->strData));
strncpy( pNewNode->strData, strData,13);
pNewNode->npos = npos;
pNewNode->left = NULL;
pNewNode->right = NULL;
AVLInsertNode(&pNode,pNewNode,&reviseBalanceFactor);
pRoot = pNode;
return 0;
}
|
不太明白你说你想要干什么