我在显示链接列表值时遇到问题。显示功能似乎工作正常......直到我在末尾插入一个新节点。我花了很多时间试图解决这个问题,但似乎没有任何效果。它只是不断显示垃圾值。 我的C源代码:
#include <stdio.h>
#include <stdlib.h>
int n=0;
typedef struct Student
{
char name[50];
int roll;
struct Student *next;
} st;
void createList(st *head)
{
st *p;
p = head;
int i = 0;
char x = ' ';
printf("Enter number of nodes: ");
scanf("%d", &n);
scanf("%c", &x);
for (i = 1; i <= n; i++)
{
printf("Enter name: ");
gets(head->name);
printf("Enter roll number: ");
scanf("%d", &(head->roll));
scanf("%c", &x);
head->next = (st *)malloc(sizeof(st));
head = head->next;
}
head->next = NULL;
}
void display(st *head)
{
int i=0;
while(head->next != NULL)
{
printf("\nName\t\t:\t%s",head->name);
printf("\nRoll no.\t:\t%d", head->roll);
head = head->next;
}
}
void insertEnd(st* head)
{
st *p, *newnode;
p = head;
char x = ' ';
newnode = (st*)malloc(sizeof(st));
printf("Enter name: ");
gets(newnode->name);
printf("Enter roll no.: ");
scanf("%d", &(newnode->roll));
scanf("%c", &x);
newnode->next = NULL;
while (p->next != NULL)
{
p = p->next;
}
p->next = newnode;
}
int main()
{
st *head;
head = (st *)malloc(sizeof(st));
createList(head);
printf("The data entered:\n");
display(head);
printf("\nInsert data for newnode at the end:\n");
insertEnd(head);
printf("The data after insertion:\n");
display(head);
return 0;
}
输出:
Enter number of nodes: 3
Enter name: Alpha
Enter roll number: 100
Enter name: Beta
Enter roll number: 200
Enter name: Gamma
Enter roll number: 300
The data entered:
Name : Alpha
Roll no. : 100
Name : Beta
Roll no. : 200
Name : Gamma
Roll no. : 300
Insert data for newnode at the end:
Enter name: Delta
Enter roll no.: 400
The data after insertion:
Name : Alpha
Roll no. : 100
Name : Beta
Roll no. : 200
Name : Gamma
Roll no. : 300
Name : peÅf╡☺
Roll no. : 1986622020
正如您在代码输出中看到的,链表显示工作正常,直到我在末尾添加新节点。我看了几个教程,我没有发现我的代码有任何问题。请帮助我找到问题,我是链表新手。
在看到“kaylum”的上述评论之前,我正在测试对代码的调整,但这基本上解决了问题。以下是您的代码的修订版,它恰好实现了 kaylum 评论中列出的想法。
#include <stdio.h>
#include <stdlib.h>
int n=0;
typedef struct Student
{
char name[50];
int roll;
struct Student *next;
} st;
void createList(st *head)
{
st *p;
p = head;
int i = 0;
char x = ' ';
printf("Enter number of nodes: ");
scanf("%d", &n);
scanf("%c", &x);
for (i = 1; i <= n; i++)
{
printf("Enter name: ");
gets(head->name);
printf("Enter roll number: ");
scanf("%d", &(head->roll));
scanf("%c", &x);
if (i == n) /* Condition the update of the next node along with allocating more memory */
{
head->next = NULL;
}
else
{
head->next = (st *)malloc(sizeof(st));
head = head->next;
} /* End of the tweaks */
}
head->next = NULL;
}
void display(st *head)
{
int i=0;
while(head != NULL) /* This provides a complete list and reacts to the other revision made to the program */
{
printf("\nName\t\t:\t%s",head->name);
printf("\nRoll no.\t:\t%d", head->roll);
printf("Pointer: %p\n", head);
printf("Next node: %p\n", head->next);
head = head->next;
}
}
void insertEnd(st* head)
{
st *p, *newnode;
p = head;
char x = ' ';
newnode = (st*)malloc(sizeof(st));
printf("Enter name: ");
gets(newnode->name);
printf("Enter roll no.: ");
scanf("%d", &(newnode->roll));
scanf("%c", &x);
newnode->next = NULL;
while (p->next != NULL)
{
p = p->next;
}
p->next = newnode;
}
int main()
{
st *head;
head = (st *)malloc(sizeof(st));
createList(head);
printf("The data entered:\n");
display(head);
printf("\nInsert data for newnode at the end:\n");
insertEnd(head);
printf("The data after insertion:\n");
display(head);
printf("\n");
return 0;
}
添加“if”块来调节“malloc”请求,消除了悬空内存分配并正确设置“下一个”指针值。另外,我调整了显示函数中的“while”测试,以配合创建函数中的调整。