在使用链表的程序中,c中未显示任何输出

问题描述

我正在使用C语言编写一个程序,在该程序中我必须使用链表,并且在该程序中,如果用户传递了场所的值,则必须在链表的开头插入新节点0,并且如果用户 choice 变量中传递了位置1的值,则还将新节点插入到链表的末尾。但是我没有在控制台上获得任何输出,我的程序仅通过编写 Output 结束 我找不到我的代码中的问题,这是我的代码

/* 
  program for making nodes and adding them in memory as per 0
  and 1

  0 means that insert the number at front,in other words insert number after head 

  1 means insert number at the last place

  First you need to input a number and then enter the place you want to insert it by giving input as 0 and 1 
  
  *Recall what does 0 and 1 mean by looking at line 5-7 respectively. 
  
  Just like 

  5 0 6 1 7 0 8 1

*/

#include <stdio.h>
#include <stdlib.h>

// declaring struct with typedef for ease of use
typedef struct node
{
  int data;
  struct node *next;
}node;

// declarations of functions use for this program respectively
void free_node(struct node *head);
void insert_at_beg(int num,struct node *head);
void insert_at_end(int num,struct node *head);
void print_node(struct node *head);

int main(void)
{
  struct node *head = NULL;
  int n;
  // taking input
  printf("Input number of nodes: ");
  scanf("%d",&n);

  int num,choice;

  printf("\nInput data for nodes->\n");
  // loop which takes value and choice 
  for (int i = 0; i < n; i++)
  {
    num = 0,choice = 0;
    printf("\nInput data for the %d node: ",i+1);
    scanf("%d",&num);

    do
    {
      printf("Input place for the %d node: ",i+1);
      scanf("%d",&choice);
    }
    while (choice != 1 && choice != 0);
    
    if (choice == 0)
    {
      // function to insert node at front of head
      insert_at_beg(choice,head);

    }
    else 
    {
      // function to insert node at last place
      insert_at_end(choice,head);

    }
  }
  
  // function to print nodes
  print_node(head);
  
  // function to free memory made by malloc()
  free_node(head);
}

// function to free the nodes 
void free_node(struct node *head)
{
  struct node *temp = head;
  while(temp != NULL)
  {
    free(temp);
    temp = temp->next;
  }
}

// function for inserting number at front
void insert_at_beg(int num,struct node *head)
{ 
  struct node *new_node = malloc(sizeof(node));

  if (new_node == NULL)
  {
    printf("Can't allocate memory.");
    exit (1);
  }

  new_node->data = num;  
  new_node->next = head;
  head = new_node;
}

// function for inserting node at end
void insert_at_end(int num,struct node *head)
{
  struct node *new_node,*last_node = NULL;
  new_node = malloc(sizeof(node));

  if (new_node == NULL)
  {
    printf("Can't allocate memory.");
    exit (1);
  }
  if (head == NULL)
  {
    new_node->data = num;
    new_node->next = NULL;
    head = new_node; 
  }
  
  last_node = head;

  new_node->data = num;
  new_node->next = NULL;

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

  last_node->next = new_node;
}

//function for printing nodes
void print_node(struct node *head)
{
  printf("\nOutput: \n");
  struct node *temp = head;

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

解决方法

您需要通过引用函数insert_at_beginsert_at_end并将指针传递到头节点,并且需要函数free_node进行引用。

在C中通过引用传递意味着间接通过对象的指针传递对象。

例如,函数insert_at_beg的外观如下。请注意,这样的功能不应发出任何消息。该函数的调用者决定是否输出消息。

// function for inserting number at front
int insert_at_beg( struct node **head,int num )
{ 
    struct node *new_node = malloc( sizeof( struct node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = num;  
        new_node->next = *head;
        *head = new_node;
    }

    return success;
}

相应地,函数insert_at_end的外观如下

// function for inserting node at end
int insert_at_end( struct node **head,int num )
{
    struct node *new_node = malloc( sizeof( struct node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = num;
        new_node->next = NULL;
 
        while ( *head != NULL )
        {
            head = &( *head )->next;
        }

        *head = new_node;
    }

    return success;
}

函数free_node具有未定义的行为,因为您正在使用指针temp访问已经释放的内存。

free(temp);
temp = temp->next;

可以通过以下方式定义功能

// function to free the nodes 
void free_node( struct node **head )
{
    while( *head != NULL )
    {
        struct node *temp = *head;
        head = &( *head )->next;
        free( temp );
    }
}

可以像这样调用函数

insert_at_end( &head,num );

if ( !insert_at_end( &head,num ) )
{
    printf( "There is no enough memory to insert the value %d\n",num );
}

函数print_node的参数应带有限定符const,因为列表在函数中未更改

//function for printing nodes
void print_node( const struct node *head )
{
  printf("\nOutput: \n");
  const struct node *temp = head;
  //...
,

C是传递值的语言-复制传递给函数的值,并且对函数中的参数所做的更改不会影响调用方。因此head永远不会成为非null; head中对insert_at_end的分配是本地的,不会更新main中的head指针。