【问题标题】:Linked Lists (very intro) InsertBack链表(非常介绍)InsertBack
【发布时间】:2020-04-20 09:34:19
【问题描述】:
#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *next;
};
typedef struct node node;

node* insertFront(node* head, int d);
node* insertBack(node* head, int d);
void print(node* head);
int max(node* head);
int min(node* head);
int locInList(node* head, int x);

int main()
{
    node* head = NULL;
    node* temp = NULL;

    head = malloc(sizeof(node));

    head = insertBack(head, 5); 
    head = insertFront(head, 4);
    head = insertFront(head, 3);
    head = insertBack(head, 6);
    head = insertBack(head, 7);
    print(head);

    printf("\nMax: %d\n", max(head));
    printf("Min: %d\n", min(head));
    printf("locInList 5: %d\n", locInList(head, 5));
    printf("locInList 9: %d\n", locInList(head, 9));    
    return 0;
}

node* insertFront(node* head, int d)
{
    node *tmp = NULL;

    tmp = malloc(sizeof(node));
    tmp->data = d;
    tmp->next = head;
    head = tmp;

    return head;
}

node* insertBack(node* head, int d)
{
    node *ptr;
    ptr->data=d;
    ptr->next = NULL;

    if(head==NULL)
    {
        head->data=d;
        head->next=NULL;
    }
    else
    {
        node *temp=head;
        while(temp->next != NULL)
        {
            temp=temp->next;
        }

        temp->next=ptr;

    }
    return head;
}

void print(node* head)
{
    node *tmp = head;

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

}

int max (node* head)
{
 int max;
 while (head != NULL)
 {
     if (max > head->data)
     max = head->data;

 }
 return max;
}

int min (node* head)
{
 int min;
 while (head != NULL)
 {
     if (min < head->data)
     min = head->data;

 }
 return min;
}

int locInList(node* head, int x)
{

}

我的 InsertBack 函数有问题,我想将 d 的值添加到 head 的末尾。

我用这段代码得到的当前输出是:

3 4 0 7 7 7 7 7 7 7...重复

输出应该是这样的

34567 最大:7 最小:3

任何帮助将不胜感激。我对链表也很陌生。所以任何帮助将不胜感激!

【问题讨论】:

  • 你的代码有几个问题,一个是你在分配后没有初始化head,另一个是你取消引用一个NULL指针(if (head==NULL) head-&gt;data = ...

标签: c linked-list singly-linked-list push-back


【解决方案1】:

这里

node* insertBack(node* head, int d)
{
    node *ptr;
    ptr->data=d;       // Dereference uninitialized pointer !!
    ptr->next = NULL;  // Dereference uninitialized pointer !!

    if(head==NULL)
    {
        head->data=d;      // Dereference NULL pointer !!
        head->next=NULL;   // Dereference NULL pointer !!
    }

你有一个大问题。你不分配内存!所以你正在取消引用一个未初始化的指针。这很糟糕。

试试:

node* insertBack(node* head, int d)
{
    node *ptr = malloc(sizeof *ptr);
    ptr->data=d;
    ptr->next = NULL;

    if(head==NULL) return ptr;

    ...

【讨论】:

    【解决方案2】:

    对于初学者来说,这条语句在 main 的开头

    head = malloc(sizeof(node));
    

    没有意义。您创建了一个未初始化的节点。因此,如果您尝试对列表执行任何操作,除了释放分配的内存,程序已经调用了未定义的行为。

    删除此声明。

    您忘记为函数insertBack 中的节点分配内存。

    node* insertBack(node* head, int d)
    {
        node *ptr;
        ptr->data=d;
        ptr->next = NULL;
        //...
    

    也是 if 语句的主体

    if(head==NULL)
    {
        head->data=d;
        head->next=NULL;
    }
    

    没有意义。

    如果让函数声明保持原样,那么它的定义可以如下所示

    node * insertBack( node *head, int d )
    {
        node *ptr = malloc( sizeof( node ) );
    
        ptr->data = d;
        ptr->next = NULL;
    
        if ( head == NULL )
        {
            head = ptr;
        }
        else
        {
            node *temp = head;
            while ( temp->next != NULL )
            {
                temp = temp->next;
            }
    
            temp->next = ptr;
        }
    
        return head;
    }
    

    还有这些功能

    int max (node* head)
    {
     int max;
     while (head != NULL)
     {
         if (max > head->data)
         max = head->data;
    
     }
     return max;
    }
    
    int min (node* head)
    {
     int min;
     while (head != NULL)
     {
         if (min < head->data)
         min = head->data;
    
     }
     return min;
    }
    

    无效,因为至少变量 max 和 min 没有被初始化。此外,它们有一个无限循环,例如函数 max 没有在列表中找到最大值。:)

    最好像这样声明它们

    int max ( node* head, int *value );
    

    在这种情况下,函数 max 的定义可能如下所示

    int max( node* head, int *value )
    {
        int success = head != NULL );
    
        if ( success )
        {    
            *value = head->data;
    
            while ( ( head = head->next ) != NULL )
            {
                if ( *value < head->data ) *value = head->data;
            }
        }
    
        return success;
    } 
    

    函数可以像这样调用

    int max_value;
    
    if ( max( head, &max_value ) )
    {
        printf( "The maximum value is %d\n", max_value );
    }
    

    函数 min 可以用同样的方式声明和定义。

    如果保持函数声明不变,那么您至少需要将变量初始化为 0。例如

    int max (node* head)
    {
        int max = head == NULL ? 0 : head->data;
    
        for ( ; head != NULL; head = head->next )
        {
             if ( max < head->data ) max = head->data;
        }
    
        return max;
    }
    

    类似的方式可以定义函数min。虽然正如我指出的那样,按照我上面显示的方式定义函数会更好。

    别忘了写一个函数来释放所有分配的内存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 2014-03-12
      • 2013-05-06
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多