如何将新创建的节点添加到链表?

时间:2011-06-01 07:00:12

标签: c linked-list

这是我的代码,我只能将节点添加到链接列表的头部,但是如何将尾部附加到链接列表?感谢

struct recordNode {
  char name[256];  
  char event[128];  /
  float time;
  struct recordNode* next;
};

struct recordNode* temp;
struct recordNode* aRecordPointer = NULL;

struct recordNode* createRecord(char* name, char* event, float time) { 

  temp = (struct recordNode*)malloc(sizeof(struct recordNode));
  strcpy(temp->name, name);
  strcpy(temp->event, event);
  temp->time = time;

  /* link up */
  if (aRecordPointer == NULL) {
     aRecordPointer = temp;
     temp->next = NULL;
  } else {
     temp->next = aRecordPointer;
     aRecordPointer = temp;
  }

  return  aRecordPointer;

}

int main() {

  struct recordNode* record = createRecord("1abc", "abc", 12.25);
  record = createRecord("2abc", "abc", 25.98);
  record = createRecord("3abc", "abc", 52.60);  

}

/* now result:

   3abc abc 12.25
   2abc abc 25.98
   1abc abc 52.60

   needed result:

   1abc abc 52.6
   2abc abc 25.98
   3abc abc 12.25

*/

3 个答案:

答案 0 :(得分:1)

假设aRecordPointer是指向list的head(first)元素的指针,则需要从头到尾迭代列表(last)。

struct recordNode* createRecord(char* name, char* event, float time){ 
  temp = (struct recordNode*)malloc(sizeof(struct recordNode));
  strcpy(temp->name, name);
  strcpy(temp->event, event);
  temp->time = time;
  temp->next = NULL;

  if (aRecordPointer == NULL) {
     aRecordPointer = temp;
  }
  else {
    struct recordNode* pLast = aRecordPointer;

    /* find the last element */
    while(pLast->next != NULL) {
      pLast = pLast->next;
    }

    /* get linked */
    pLast->next = temp;
  }

  return aRecordPointer;
}

答案 1 :(得分:0)

假设aRecordPointer是指向最后一个节点的指针,请修改您的代码,如下所示:

struct recordNode* createRecord(char* name, char* event, float time) 
{ 
  temp = (struct recordNode*)malloc(sizeof(struct recordNode));
  strcpy(temp->name, name);
  strcpy(temp->event, event);
  temp->time = time;

  //I have added this line as well.
  temp->next = NULL;

  /* link up */
  if (aRecordPointer == NULL) 
  {
     aRecordPointer = temp;
  } 
  else 
  {
     aRecordPointer->next = temp;
     aRecordPointer = temp;
  }
  return aRecordPointer;
}

另请注意,您应始终使用指向Head(First Node)的指针,以便稍后遍历链接列表。

答案 2 :(得分:0)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void Traverse();

struct recordNode {
  char name[256];  
  char event[128];  
  float time;
  struct recordNode* next;
};

struct recordNode* temp;
struct recordNode* aRecordPointer = NULL;

struct recordNode* createRecord(char* name, char* event, float time) { 

  struct recordNode * ToTraverseTheLinkedList;
  temp = (struct recordNode*)malloc(sizeof(struct recordNode));
  strcpy(temp->name, name);
  strcpy(temp->event, event);
  temp->time = time;
  temp->next = NULL;

  /* link up */
  if (aRecordPointer == NULL) {
     aRecordPointer = temp;
     //temp->next = NULL;
  }     
    else
    {
    ToTraverseTheLinkedList = aRecordPointer;
    while(ToTraverseTheLinkedList->next != NULL)
        {
             ToTraverseTheLinkedList =  ToTraverseTheLinkedList ->next;

        }
    ToTraverseTheLinkedList->next = temp;
    }
  return  aRecordPointer;

}

int main() {

  createRecord("1abc", "abc", 12.25);
  //Traverse();
  createRecord("2abc", "abc", 25.98);
  createRecord("3abc", "abc", 52.60);  
        Traverse();
}

void Traverse()
{
struct recordNode * temp;
temp = aRecordPointer;
while(temp != NULL)
{
printf("%s\n",temp->name);
printf("%s\n",temp->event);
printf("%f\n",temp->time);
temp = temp->next;
}
}