在使用链表的程序中,c 中没有显示输出
In a program using linked-list no output is being shown in c
我正在用 c 语言开发一个程序,在这个程序中我必须使用链表,在这个程序中,如果用户传递值,我必须在链表的开头插入新节点如果用户在 choice 变量中传递位置 1 的值,则位置 0 并在链表的末尾插入新节点。但是我没有在控制台上得到任何输出,我的程序只写了 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;
}
}
C 是一种 pass-by-value 语言 -- 传递给函数的值被复制,并且对函数中参数的更改不会影响调用者。所以 head
在 main 中永远不会变成 non-null; insert_at_end
中对 head
的赋值是局部的,不会更新 main.
中的 head
指针
您需要通过引用您的函数 insert_at_beg
和 insert_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 语言开发一个程序,在这个程序中我必须使用链表,在这个程序中,如果用户传递值,我必须在链表的开头插入新节点如果用户在 choice 变量中传递位置 1 的值,则位置 0 并在链表的末尾插入新节点。但是我没有在控制台上得到任何输出,我的程序只写了 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;
}
}
C 是一种 pass-by-value 语言 -- 传递给函数的值被复制,并且对函数中参数的更改不会影响调用者。所以 head
在 main 中永远不会变成 non-null; insert_at_end
中对 head
的赋值是局部的,不会更新 main.
head
指针
您需要通过引用您的函数 insert_at_beg
和 insert_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;
//...