BST与链接队列输出错误

时间:2014-06-24 00:51:33

标签: c file-io linked-list binary-search-tree

在将新节点分配给动态节点列表时遇到问题,实际上我只能在那里获得第一个节点。一旦检测到重复,我或者我没有正确打印它们(不太可能)或者它们没有分配。一切都打印得很好,只有插入队列的第一个数字才会显示。

typedef struct lineList
{
    int lineNum;
    LIST *next;
}LIST;

typedef struct nodeTag{
   char data[80];
   LIST *lines;
   struct nodeTag *left;
   struct nodeTag *right;
} NODE;

致电addtolist

    mover = (*root)->lines;
    printf("Node already in the tree!\n");
    while(mover)
        mover = mover->next;
    mover = addToList();  //allocate memory for new node
    mover->lineNum = line; //set data

添加到列表

LIST *addToList()
{
    LIST *pnew;
    pnew = (LIST *) malloc(sizeof (LIST)); //memory for LIST
    if (!pnew)
    {
        printf("Fatal memory allocation error in insert!\n");
        exit(3);
    }
    pnew->next = NULL; //set next node to NULL

    return pnew;
}

树输出到文件(我在这里有一些小问题以及一些节点 打印两次)

void treeToFile(NODE *root, FILE *fp)
{
    if(root->left)
    {
      treeToFile(root->left, fp);
      fprintf(fp, "%-15s",  root->data);
      printList(root->lines, fp);
      fprintf(fp, "\n");
   }
   if(root->right)
   {
      treeToFile(root->right, fp);
      fprintf(fp, "%-15s",  root->data);
      printList(root->lines, fp);
      fprintf(fp, "\n");
   }
   return;
}

打印列表

void printList(LIST *myList, FILE *fp)
{
    LIST *mover;
    mover = myList;
    while(mover) //while mover
    {
        fprintf(fp, "%5d", mover->lineNum); //line nums where string occurs
        mover = mover->next; //move to next node
    }
}

1 个答案:

答案 0 :(得分:1)

您正在丢失链接,请查看此代码:

while(mover)
    mover = mover->next;
mover = addToList();  //allocate memory for new node
mover->lineNum = line; //set data

你的同时没有意义......当你'mover'== NULL时你就离开了,你想做的是拥有最后一个节点(下一个节点为null)

将您的代码更改为以下

// the IF below is for the case when the queue is empty, so you won't try to dereference a NULL
// in the while condition
if(mover)               
    while(mover->next)
        mover = mover->next;

if(mover) // make sure you have at least one element in the queue
{
    mover->next = addToList();  //allocate memory for new node
    mover->next->lineNum = line; //set data
}
else // if the queue is empty, then lines will return NULL and you 
       //are inserting the first element
{
    mover = addToList();
    mover->lineNum = line;
    (*root)->lines = mover; // here you are putting the new element in the first position 
                          // of your queue (It is necessary to do this because 
                          // it is currently empty!
}

上面的代码是修复代码的方法。根据队列的含义,我有2条建议。

首先:你需要订购这个队列吗?元素的顺序对你来说很重要吗?

如果是,那么你需要一个队列,但是你可以有一个像这样的结构,而不是遍历每个插入的整个列表:

struct queue
 {
    LIST *first;
    LIST *last;
 }

你将在last-> next中使用新元素(第一个元素是不同的,因为last在那里将为null ...你需要使下一个和第一个指向该元素)

或者,如果您的订单无关紧要,只需在列表的开头添加新元素

mover = addToList();
mover->lineNum = line;
mover->next = *(root)->lines; //supposing you are using the right side correctly in your code,  
                       // you are adding the current list as being the next of your new element

*(root)->lines = mover; //here you are saying that your list starts now at your new element