C语言 打印链表的内容只打印第一个节点



这是代码的更新版本。我试图在一个链接列表中添加一些信息,每次客户端发送消息到服务器(它可以是多个客户端)。每次新消息到达时,该函数都会检查其时间,以便根据当前时间将其添加到前一个节点之前或之后的列表中。服务器必须按照到达的顺序打印消息。如果有相等的时间戳,那么它应该使用服务器的id来对相等的时间戳进行排序。

下面是列表的结构体:

   'typedef struct trade_list {
    char* trader_msg;
    u_int32_t id_of_sender;
    int sender_timer;
    int local_time;
    struct trade_list *next;
}trade_list;
trade_list *head = NULL;

下面是插入列表并按时间排序的函数:

  void add_transaction (char* received_msg_IN, int curr_time_IN, u_int32_t my_id_IN, int elapsedIn)
{ 
 /* Find the node with the smallest time >= curr_time_IN.  'found' starts as NULL, then
        is always the node before 'cur'.  'cur' moves through the list until its time is
        less than 'curr_time_IN'.  So at the end, 'cur' is the first node that's too 
        far in, and 'found' is either NULL or the node we insert after. */
    trade_list *newnode, *cur, *found;

     found = NULL;
  for (cur = head; cur && cur->sender_timer <= curr_time_IN; cur = cur->next)
     found = cur;

    if (found) {
      /* If 'found' isn't NULL, we're inserting after it*/
            /* Times match: Sort out in another way*/
      } else {
            newnode = malloc(sizeof(*newnode));
            newnode->trader_msg = malloc(strlen(received_msg_IN)*sizeof(received_msg_IN));
            strcpy(newnode->trader_msg,received_msg_IN);
            newnode->sender_timer = curr_time_IN;
            newnode->id_of_sender = my_id_IN;
            newnode->local_time = elapsedIn;
            newnode->next = found->next;
            found->next = newnode;
            }
    } else {                
        /* No node with more recent time found -- inserting at 'head' */
       newnode = malloc(sizeof(*newnode));
       newnode->trader_msg = malloc(strlen(received_msg_IN)*sizeof(received_msg_IN));
       strcpy(newnode->trader_msg,received_msg_IN);
       newnode->sender_timer = curr_time_IN;
       newnode->id_of_sender = my_id_IN;
       newnode->local_time = elapsedIn;
       newnode->next = head;
       head = newnode;
         }   

编辑后的新问题

我后来使用排序方法对列表进行了排序。现在我的列表已经排好序了。它打印得很好,现在出现的新问题是,我想在打印完当前节点后删除它。打印出来之后,它就被删除了。我使用了下面的函数,但是我的应用程序崩溃了。

void deletefirst (struct trade_list *head) {
    struct trade_list *tmp = *head;         
    if (tmp == NULL) return;            
    *head = tmp->next;                  
    free (tmp);                        
}

我从打印函数中调用这个函数:

void print_trades()
{
    trade_list * newnode = head;
        while (newnode) {
            if ((elapsed - newnode->local_time >= 8)) 
            {
            printf ("%sn", newnode->trader_msg);
            newnode = newnode->next;
            deletefirst(newnode);
            }
          }
}

我如何删除当前节点并继续前进?

只要至少有一个节点,那么您打印列表的方式应该可以正常工作。我建议将do { ... } while()更改为while() { ... },以便在列表为空并且headNULL时仍然有效:

trade_list *currentnode = head;
while (currentnode) {
  printf ("Trade: %s Time: %d ID: %dn", 
      currentnode->trader_msg,
      currentnode->sender_timer,
      currentnode->id_of_sender);
  currentnode = currentnode->next;
}

但是,添加列表的方式有一些问题。当head不为空时,您将删除其到列表其余部分的链接:

if (head == NULL) 
    {
   ... /* skipped for brevity */
    }
else
    {
    currentnode->next = NULL;
    head = currentnode;
    }

由于此时currentnode == head,您将head->next指向NULL(如果有另一个节点,您将扔掉它),然后将head分配给自己。

您的插入代码通常看起来不正确。如果您只想在列表的开头插入,您只需要像这样:

trade_list *newnode = malloc(sizeof(trade_list));
/* *** Fill out newnode's fields here *** */
newnode->next = head;
head = newnode;

如果您想在任意节点之后插入,您必须首先通过遍历列表来找到它,然后执行以下操作(将稍后的时间保留在列表的头部):

trade_list *newnode, *cur, *found;
/* Find the node with the smallest time >= curr_time_IN.  'found' starts as NULL, then
   is always the node before 'cur'.  'cur' moves through the list until its time is
   less than 'curr_time_IN'.  So at the end, 'cur' is the first node that's too far in,
   and 'found' is either NULL or the node we insert after. */
found = NULL;
for (cur = head; cur && cur->sender_timer >= curr_time_IN; cur = cur->next)
  found = cur;
if (found) {
  /* If 'found' isn't NULL, we're inserting after it (or skipping if the times match,
     since that seems to be what the original code was trying to do) */
  if (found->sender_timer == curr_time_IN) {
    /* Times match: skip it */
    printf("SKIPPEDn");
  } else {
    /* inserting after 'found' */
    newnode = malloc(sizeof(*newnode));
    /* *** Fill out newnode's fields here *** */
    newnode->next = found->next;
    found->next = newnode;
  }
} else {
  /* No node with more recent time found -- inserting at 'head' */
  newnode = malloc(sizeof(*newnode));
  /* *** Fill out newnode's fields here *** */
  newnode->next = head;
  head = newnode;
}

编辑后注释:

要将列表更改为按时间降序排序,然后在时间匹配时按ID升序排序,只需要进行一些更改。

编辑由于查找节点的原始for循环继续到符合标准的最后一个节点,我们只需要在循环中添加一个测试,以便在我们处于正确的节点时中断…也就是说,如果下一个节点具有相同的时间和更高的ID,则中断,因为在这种情况下,我们希望在之前插入。(之前的编辑有缺陷……很抱歉)。

此外,之后的if (found->sender_timer == curr_time_IN)检查不再需要,因为没有跳过,并且按ID排序由新的for循环处理。

所以这段代码变成了:

/* original search */
for (cur = head; cur && cur->sender_timer >= curr_time_IN; cur = cur->next) {
  /* *** added condition for ID sort */
  if (cur->sender_timer == curr_time_IN && cur->id_of_sender >= my_id_IN) break;
  found = cur;
}
if (found) {
  /* If 'found' isn't NULL, we're inserting after it */
  /* CHANGED: no need to skip when times are equal */
  newnode = malloc(sizeof(*newnode));
  /* *** Fill out newnode's fields here *** */
  newnode->next = found->next;
  found->next = newnode;
} else {
   /* No node with more recent time found -- inserting at 'head' */
  newnode = malloc(sizeof(*newnode));
  /* *** Fill out newnode's fields here *** */
  newnode->next = head;
  head = newnode;
}

问题不在于打印代码,而在于添加。

看起来在添加一个新节点时,您分配了该节点,但没有将其连接到头部或任何其他节点。

必须有这样的代码添加到列表的开头:

new_node->next = head;
head= new_node;

同时,你使用的逻辑是模糊的,你有很多重复的代码。

添加到链表只有两种选择:列表的开头(头)或任何其他节点,您不需要所有这些逻辑。

相关内容

  • 没有找到相关文章

最新更新