链接列表 - 在C列表的末尾插入节点

时间:2016-02-24 19:55:38

标签: c data-structures

这是在末尾插入节点的代码。运行时我没有收到任何错误,但它表明程序已停止工作。请告诉我在哪里弄乱了吗?

enter code here

#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* next;
};

struct node* head;

void Insert(int data)
{
    struct node* temp, *temp2;
    temp=(struct node*)malloc(sizeof(struct node));
    temp->data = data;
    temp2 = head;
    while(temp2 != NULL)
    {
        temp2 = temp2->next;
    }
    temp2->next = temp;
    temp->next = NULL;

}
void print()
{
    struct node* temp = head;
    while(temp!=NULL)
    {
        printf("%d", temp->data);
    }
    printf("\n");
}
int main()
{
    head = NULL;
    Insert(1);
    Insert(2);
    Insert(3);
    Insert(4);
    Insert(5);
    print();
    return 0;
}

3 个答案:

答案 0 :(得分:0)

while(temp2 != NULL)
{
    temp2 = temp2->next;
}
temp2->next = temp;

此循环后temp2始终为NULL

答案 1 :(得分:0)

也许不是

while(temp2 != NULL)
{
    temp2 = temp2->next;
}

你打算写

while(temp2->next != NULL)
{
    temp2 = temp2->next;
}

此外,您的head变量始终为NULL。您必须在main中分配一个值,例如分配:

int main()
{
    head = (struct node*)malloc(sizeof(struct node));
    ...
}

此外,您的打印功能不会遍历列表,因此存在无限循环,而是尝试:

while(temp!=NULL)
{
    printf("%d", temp->data);
    temp = temp->next;
}

答案 2 :(得分:0)

在函数

中循环之后
temp2

指针NULL将等于temp2->next = temp; ,因为这是循环中断时的条件。

这样循环后的语句

void Insert( int data )
{
    struct node *temp = malloc( sizeof( struct node ) );

    if ( temp != NULL )
    {
        temp->data = data;
        temp->next = NULL;

        struct node **last = &head;

        while ( *last != NULL ) last = &( *last )->next;

        *last = temp;
    }
}

导致函数的未定义行为。

该功能可以写得更简单

void print( void )
           ^^^^^^
{
    struct node* temp = head;
    while(temp!=NULL)
    {
        printf("%d ", temp->data);
                ^^^
        temp = temp->next;
        ^^^^^^^^^^^^^^^^^^
    }
    printf("\n");
}

注意你忘记了函数print中的一个语句。它应该看起来像

void print( void )
{
    for ( struct node *temp = head; temp != NULL; temp = temp->next )
    {
        printf( "%d ", temp->data );
    }
    printf( "\n" );
}

而不是while循环我将使用以下for循环

public interface IValidator<T> where T : class
{
    void Validate(T entity);
}
public class ClientValidator : IValidator<Client>
{
    public void Validate(Client entity)
    {
        //Auto-generated
    }
}
public class UserValidator : IValidator<User>
{
    public void Validate(User entity)
    {
        //Auto-generated
    }
}
public class ClientValidatorDecorator : IValidator<Client> 
{
    private readonly IValidator<Client> clientValidator;

    public ClientValidatorDecorator(IValidator<Client> clientValidator)
    {
        this.clientValidator = clientValidator;
    }
    public void Validate(Client entity)
    {
        //New rules
        this.clientValidator.Validate(entity);
    }
}
public class UserValidatorDecorator : IValidator<User>
{
    private readonly IValidator<User> userValidator;

    public UserValidatorDecorator(IValidator<User> userValidator)
    {
        this.userValidator = userValidator;
    }
    public void Validate(User entity)
    {
        //New rules
        this.userValidator.Validate(entity);
    }
}
public class ValidationContext
{
    private readonly IValidator<Client> client;
    private readonly IValidator<User> user;

    public ValidationContext(IValidator<Client> client, IValidator<User> user)
    {
        this.client = client;
        this.user = user;
    }
}