添加时保留链表的开头

时间:2011-12-25 21:10:15

标签: c

我想在下面的代码中保留链表的开头。我认为我的代码没有任何问题,但是当我添加两个节点并调用print时,它会显示第二个节点的第一个名称。

编辑:它现在没有显示!它很空虚

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
struct node {
char Number[10];
char FirstName[10];
char LastName[10];
char FatherName[10]; 
char Email[20];
char SiteName[30];
struct node *next;
};
void print( struct node* list)
{
    printf("print1");
        printf(list->FirstName);
        printf("print2");
}
void addNode(struct node *head)
{
    struct node *current = head;
puts("*******Now you can insert a new person****");
    struct  node *newNode = malloc(sizeof(struct node));
            printf("FIRSTNAME: ");     
        gets(newNode->FirstName);
        printf("LASTNAME: ");      
        gets(newNode->LastName);
           printf("FATHERNAME: ");    
        gets(newNode->FatherName);
           printf("EMAIL: ");      
        gets(newNode->Email);
           printf("SITENAME: ");   
          gets(newNode->SiteName);
      //create new node

     newNode->next = 0;  // Change 1
        //check for first insertion
        if(current->next == 0){
       current->next = newNode;
       printf("added at beginning\n");
    }
    else
    {
        //else loop through the list and find the last
        //node, insert next to it
  while (current->next != 0) {
    current = current->next;
  }
  current->next = newNode;
  printf("added later\n");
    }
    }
//*************************************************************************
int main()
{
    /* This won't change, or we would lose the list in memory */
    struct node *root;   

    /* This will point to each node as it traverses the list */
    struct node *conductor;  
    root = malloc( sizeof(struct node) );  
    root->next = 0;   
       addNode(root);
    addNode(root);
    conductor = root; 
    //*********************************
    print(root);
    if ( conductor != 0 ) {
        while ( conductor->next != 0)
        {
            conductor = conductor->next;
        }
                        }
    /* Creates a node at the end of the list */
    conductor->next = malloc( sizeof(struct node) );  
    conductor = conductor->next; 
    if ( conductor == 0 )
    {
        printf( "Out of memory" );
        return 0;
    }
    /* initialize the new memory */
conductor = root;
if ( conductor != 0 ) {
 /* Makes sure there is a place to start */
    while ( conductor->next != 0 ) {
      puts( conductor->FirstName );
puts( conductor->LastName );
        conductor = conductor->next;
    }
    puts( conductor->FirstName );
}
    return 0;
}

2 个答案:

答案 0 :(得分:1)

编辑:您的打印功能现在不输出任何内容,因为根节点为空。这就是你的清单:

+------+        +------------------+       +-------------------+
| root |  --->  | first user entry | --->  | second user entry |
+------+        +------------------+       +-------------------+

所以,如果你替换

print(root);

print(root->next);

它将打印用户的第一个条目。


OLD ANSWER :在这一行:

gets(current->FirstName);

使用新值覆盖current->FirstName的值。由于current指向head,因此您将覆盖第一个节点的值。

要解决此问题,首先 malloc新节点,然后 gets将值添加到newNode中,而不是current进入newNode->FirstName)。不要忘记为gets和其他字段分配足够的空间,否则{{1}}会溢出缓冲区。实际上,please don't use gets at all

答案 1 :(得分:1)

gets(current->FirstName);

current指向与struct node相同的head,头节点传递给addNode。因此,您将覆盖传递给struct node的{​​{1}}的值,而不是使用上述代码创建的新节点。

我假设您应该在addNode指向的节点和 newNode中存储名字,姓氏等。

使用current,您的代码也容易受到缓冲区溢出的影响;考虑使用gets。我也希望fgetsFirstName等被定义为LastName而不是char[],所以他们为它们分配了内存以存储字符串。