英文:
How to add a node at the end of a linked list?
问题
首先,让我看一下你的代码。以下是你提供的代码的翻译:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num;
struct node *next;
} Tnode;
Tnode *head;
typedef Tnode *Tlist;
void insert(int);
void printlist();
Tnode *makenode(int x);
Tlist MakeList();
void InsertAtEnd(Tlist list, int x);
void infoPrint(int info);
void PrintList(Tlist list);
int main(int argc, char **argv) {
int n, i, x;
Tlist list = MakeList();
printf("Creiamo una lista; quanti elementi vuoi inserire ? "); // 翻译:"创建一个列表;你想要插入多少个元素?"
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\n Inserisci valore da inserire "); // 翻译:"插入要素"
scanf("%d", &x);
InsertAtEnd(list, x);
PrintList(list);
}
return (EXIT_SUCCESS);
}
Tlist MakeList() {
return NULL;
}
void insert(int x) {
Tnode *temp = makenode(x);
temp->next = head;
head = temp;
}
Tnode *makenode(int x) {
Tnode *new = malloc(sizeof(Tnode));
if (new == NULL)
return NULL;
new->num = x;
new->next = NULL;
printf(".");
return new;
}
void infoPrint(int info) {
printf(" %d ", info);
}
void PrintList(Tlist list) {
Tnode *node = list;
while (node != NULL) {
infoPrint(node->num);
node = node->next;
}
}
void InsertAtEnd(Tlist head, int x) {
Tnode *newNode, *tmp;
newNode = makenode(x);
tmp = head;
while (tmp->next != NULL) {
tmp = tmp->next;
tmp->next = newNode;
}
}
在运行你的程序时,它在插入列表的第一个值后立即停止。要使其正常工作,你需要更正 InsertAtEnd
函数的错误。请将以下代码段:
while (tmp->next != NULL) {
tmp = tmp->next;
tmp->next = newNode;
}
替换为:
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = newNode;
这样,你的插入操作将会按预期工作,而不会导致程序停止。希望这可以帮助你解决问题!
英文:
First of all before y'all tell me I checked the other questions on this site and I followed the instructions. Still my program has bugs .
here's the code i've made but it doesn't start at all
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num;
struct node *next;
}Tnode;
Tnode *head;
typedef Tnode *Tlist;
void insert(int);
void printlist();
Tnode *makenode(int x);
Tlist MakeList ();
void InsertAtEnd (Tlist list,int x);
void infoPrint (int info);
void PrintList(Tlist list);
int main(int argc, char** argv) {
int n,i,x;
Tlist list=MakeList();
printf("Creiamo una lista; quanti elementi vuoi inserire ? ");//translation "how many elements in the list ?"
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\n Inserisci valore da inserire ");//translation "insert the element"
scanf("%d",&x);
InsertAtEnd(list,x);
PrintList(list);
}
return (EXIT_SUCCESS);
}
Tlist MakeList (){
return NULL;
}
void insert(int x){
Tnode *temp= makenode(x);
temp->next=head;
head=temp;
}
Tnode *makenode(int x){
Tnode *new=malloc(sizeof(Tnode));
if (new==NULL)
return NULL;
new->num=x;
new->next=NULL;
printf(".");
return new;
}
void infoPrint (int info) {
printf (" %d ", info);
}
void PrintList(Tlist list){
Tnode *node=list;
while(node!=NULL){
infoPrint(node->num);
node=node->next;
}
}
void InsertAtEnd (Tlist head,int x){
Tnode *newNode,*tmp;
newNode=makenode(x);
tmp=head;
while(tmp->next!=NULL){
tmp=tmp->next;
tmp->next=newNode;
}
}
When i build it there's 0 problems. When I run it it stops as soon as i insert the fisrt value of the list.
How do i make it work?
答案1
得分: 1
你的程序有一个名为MakeList的函数,它返回NULL。
这个NULL被赋给了list。
然后你将list发送到insertAtEnd函数。
在评估“while语句”时,它会导致核心转储(分段错误),程序退出。
这就是你的程序突然退出的原因。
进行以下修改:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num;
struct node *next;
} Tnode;
Tnode *head;
typedef Tnode *Tlist;
void insert(int);
void printlist();
Tnode *makenode(int x);
Tlist MakeList();
Tlist InsertAtEnd(Tlist list, int x); // 修改了函数原型
void infoPrint(int info);
void PrintList(Tlist list);
int main(int argc, char** argv) {
int n, i, x;
Tlist list = MakeList();
printf("创建一个列表; 你想插入多少个元素?");//翻译:"创建一个列表; 你想插入多少个元素?"
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\n 插入要插入的值 ");//翻译:"插入要插入的值"
scanf("%d", &x);
list = InsertAtEnd(list, x); // 修改了函数调用...
PrintList(list);
}
return (EXIT_SUCCESS);
}
Tlist MakeList() {
return NULL;
}
void insert(int x) {
Tnode *temp = makenode(x);
temp->next = head;
head = temp;
}
Tnode *makenode(int x) {
Tnode *new = malloc(sizeof(Tnode));
if (new == NULL)
return NULL;
new->num = x;
new->next = NULL;
printf(".");
return new;
}
void infoPrint(int info) {
printf(" %d ", info);
}
void PrintList(Tlist list) {
Tnode *node = list;
while (node != NULL) {
infoPrint(node->num);
node = node->next;
}
}
Tlist InsertAtEnd(Tlist head, int x) {
Tnode *newNode, *tmp;
newNode = makenode(x);
tmp = head;
if (tmp == NULL)
head = newNode;
else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = newNode;
}
return head;
}
英文:
You program has MakeList function which is returning NULL.
This NULL is being assigned to list.
You are then sending the list to insertAtEnd function.
While evaluating the while statement
, it dumps core (segmentation fault) and the program exits.
That's the reason your program is abruptly exiting.
Make the below mods --
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num;
struct node *next;
}Tnode;
Tnode *head;
typedef Tnode *Tlist;
void insert(int);
void printlist();
Tnode *makenode(int x);
Tlist MakeList ();
Tlist InsertAtEnd (Tlist list,int x); // changed prototype
void infoPrint (int info);
void PrintList(Tlist list);
int main(int argc, char** argv) {
int n,i,x;
Tlist list=MakeList();
printf("Creiamo una lista; quanti elementi vuoi inserire ? ");//translation "how many elements in the list ?"
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\n Inserisci valore da inserire ");//translation "insert the element"
scanf("%d",&x);
list = InsertAtEnd(list,x); // changed call to function...
PrintList(list);
}
return (EXIT_SUCCESS);
}
Tlist MakeList (){
return NULL;
}
void insert(int x){
Tnode *temp= makenode(x);
temp->next=head;
head=temp;
}
Tnode *makenode(int x){
Tnode *new=malloc(sizeof(Tnode));
if (new==NULL)
return NULL;
new->num=x;
new->next=NULL;
printf(".");
return new;
}
void infoPrint (int info) {
printf (" %d ", info);
}
void PrintList(Tlist list){
Tnode *node=list;
while(node!=NULL){
infoPrint(node->num);
node=node->next;
}
}
Tlist InsertAtEnd (Tlist head,int x){
Tnode *newNode,*tmp;
newNode=makenode(x);
tmp=head;
if (tmp == NULL)
head = newNode;
else
{
while(tmp->next!=NULL){
tmp=tmp->next;
}
tmp->next = newNode;
}
return head;
}
答案2
得分: 0
函数InsertAtEnd
有问题。
您没有处理第一个元素的情况。为此,函数定义需要修改。您可以将双指针传递给head
到这个函数。
另外,tmp->next=newNode;
的赋值应该放在for循环之后。
您应该避免创建指针类型TList
,只需传递指向Tnode
的指针会更清晰和更容易。
void InsertAtEnd(Tnode **head, int x) {
Tnode *newNode, *tmp;
newNode = makenode(x);
tmp = *head;
if (tmp == NULL) {
*head = newNode;
} else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = newNode;
}
}
// 在主函数中调用它
InsertAtEnd(&list, x);
英文:
The function InsertAtEnd
is the problem.
You are not taking care of the case of the first element. For this the function definition needs to be modified. You can pass a double pointer to the head
to this function.
Also, the assignment of tmp->next=newNode;
should be after the for loop.
You should avoid creating a pointer type TList
It is much cleaner and easier to just pass a pointer to the Tnode
void InsertAtEnd (Tnode ** head,int x){
Tnode *newNode,*tmp;
newNode=makenode(x);
tmp=*head;
if (tmp == NULL)
{
*head = newNode;
}
else
{
while(tmp->next!=NULL){
tmp=tmp->next;
}
tmp->next=newNode;
}
}
// in main call it with
InsertAtEnd(&list,x);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论