這是我的答案。很抱歉代碼太多!
首先,一些struct
小號
// contains the whole line as well as the series for ease of id
struct _Payload
{
char *line; // the line of text
int series; // the number in [ ] at the end of that line
int numElements; // the number of elements in the line
};
// a tree sorted by the series of the Payloads within
struct _PayloadTree
{
struct _Payload *payload;
struct _PayloadTree *left;
struct _PayloadTree *right;
};
// a tree sorted by the number of elements in the PayloadTree subtrees
struct _Tree
{
int numElements; // for sorting
struct _PayloadTree *payloadTree;
struct _Tree *left;
struct _Tree *right;
};
typedef struct _Payload Payload;
typedef struct _PayloadTree PayloadTree;
typedef struct _Tree Tree;
下,一些輔助功能:
Payload *createPayload(char *line, int series, int numElements)
{
Payload * payload = (Payload *)malloc(sizeof(Payload));
payload->line = line;
payload->series = series;
payload->numElements = numElements;
return payload;
}
PayloadTree *createPayloadTree(Payload *p)
{
PayloadTree * payloadTree = (PayloadTree *)malloc(sizeof(PayloadTree));
payloadTree->payload = p;
payloadTree->left = payloadTree->right = NULL;
return payloadTree;
}
Tree *createTree(int numElements)
{
Tree * tree = (Tree *)malloc(sizeof(Tree));
tree->numElements = numElements;
tree->payloadTree = NULL;
tree->left = tree->right = NULL;
return tree;
}
我們的東西添加到樹。我們首先找到的元素桶的數量,然後內發生在適當的系列鬥式(PayloadTree
)
void addPayloadToPayloadTree(Payload *p, PayloadTree *payloadTree)
{
// these are sorted according to the value in 'series'
if(payloadTree == NULL) return;
PayloadTree *pt = payloadTree;
while(pt != NULL)
{
// should this happen?
if(p->series == pt->payload->series) break;
else if(p->series < pt->payload->series)
{
if(pt->left == NULL) pt->left = createPayloadTree(p);
else pt = pt->left;
}
else
{
if(pt->right == NULL) pt->right = createPayloadTree(p);
else pt = pt->right;
}
}
}
// Main way to add a line to the tree
void addPayload(Payload *p, Tree **tree)
{
if(*tree == NULL) *tree = createTree(p->numElements);
Tree *t = *tree;
while(t != NULL)
{
if(t->numElements == p->numElements) break;
else if(t->numElements > p->numElements)
{
// look left
if(t->left == NULL)
{
t->left = createTree(p->numElements);
break;
}
t = t->left;
}
else
{
// look right
if(t->right == NULL)
{
t->right = createTree(p->numElements);
break;
}
t = t->right;
}
}
// now t points to the right bucket
if(t->payloadTree == NULL) t->payloadTree = createPayloadTree(p);
addPayloadToPayloadTree(p, t->payloadTree);
}
最後的印刷方法。打印從右至左:
void printPayloadTree(PayloadTree *pt)
{
if(pt == NULL) return;
printPayloadTree(pt->right);
printf("%s\n", pt->payload->line);
printPayloadTree(pt->left);
}
void printTree(Tree *t)
{
if(t == NULL) return;
printTree(t->right);
printPayloadTree(t->payloadTree);
printTree(t->left);
}
我離開了clear
功能和main
實現,因爲這是給你的,當然。這是我用來解析一行的方法。您可以將其稱爲parseLine(theLine, &series, &numElements)
,您之前在此聲明series
和numElements
的類型爲int
。
void parseLine(char *line, int *series, int *numElements)
{
char *tok = strtok(line, " ");
int n = 0;
while(tok != NULL)
{
if(tok[0] == '[')
{
// found the series
tok[ strlen(tok) - 2 ] = '\0';
*series = atoi(tok + 1);
}
else
{
n++;
}
tok = strtok(NULL, " ");
}
*numElements = n;
}
您的意思是說,您想先按每行中的_number_個元素排序? – 2010-04-30 12:17:50
請在此處和下面提供評論中所需的全部信息。 – 2010-05-10 14:27:44
不知道你是如何提供100聲望的賞金。在聲譽上你和我差不多。小於60. – Gulshan 2010-05-14 16:39:38