gpt4 book ai didi

c - 链表反转功能导致无限打印循环

转载 作者:行者123 更新时间:2023-11-30 18:52:49 25 4
gpt4 key购买 nike

我正在编写一个 C 代码来反转链接列表。我遇到了一个问题。如果我不将 next 指针设置为 NULL,我的反向函数可以正常工作,但如果我将其设置为 null,则链接列表始终在 while 循环中保持打印。

下面是正确的程序,运行良好。但是如果我设置*next = NULL,显示函数将在while循环中继续打印。

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

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

/*************************************************************/
/* */
/* create - Function to create Nodes and add them at last */
/* */
/*************************************************************/
int create(int data)
{
struct node *temp,*ptr = NULL;
//int data = 0;

ptr = head;

//Printf(" Enter the Data for Node : ");
//scanf(" %d ", data);

temp = (struct node *)malloc(sizeof(struct node));

if (ptr == NULL) {
// this is the first node
temp->next = NULL;
temp->data = data;
head = temp;
} else {
// this is not the first node
while (ptr != NULL) {
if (ptr->next == NULL) {
temp->next = NULL;
temp->data = data;
ptr->next = temp;
break;
}
ptr = ptr->next;
}
}

return 0;
}

/*************************************************************/
/* */
/* create_in_front - Function to add Node in Front */
/* */
/*************************************************************/
int create_in_front(int data)
{
struct node *temp,*ptr = NULL;
ptr = head;

temp = (struct node *)malloc(sizeof(struct node));

if (ptr == NULL) {
// this is the first node
temp->next = NULL;
temp->data = data;
head = temp;
} else {
// this is not the first node
temp->next = ptr->next;
temp->data = data;
head = temp;
}
return 0;
}

/*************************************************************/
/* */
/* create_in_between - Function to add Node in between nodes*/
/* */
/*************************************************************/

int create_in_between(int data,int pos)
{
struct node *temp, *ptr = NULL;
int i = 0;

ptr = head;

temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;

for (i = 0; i < pos; i++) {
if (i == pos-1) {
temp->next = ptr->next;
ptr->next = temp;
}
ptr = ptr->next;
}
return 0;
}


/*************************************************************/
/* */
/* delete_in_between - Function to add Node in between nodes*/
/* */
/*************************************************************/

delete_in_between(int pos)
{
struct node *ptr, *prev = NULL;
ptr = head;
int i = 0;

for (i = 0; i < pos; i++) {
if (i == pos-1) {
prev = ptr->next;
free(ptr);
break;
}
prev = ptr;
ptr = ptr->next;
}
return 0;
}

/*************************************************************/
/* */
/* reverse - Function to reverse link list */
/* */
/*************************************************************/

int reverse()
{
struct node *prev = NULL;
struct node *curr = head;
struct node *next = NULL;

curr = head;

while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;

return 0;
}

/*************************************************************/
/* */
/* display - Function to diplay link list */
/* */
/*************************************************************/
// Function to display Link List
int display()
{
struct node *temp = head;

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

int main()
{
create(10);
create(20);
create(30);
create(40);
create(50);
create_in_front(34);
create_in_between(55,2);
//delete_in_between(4);
reverse();
display();
return 0;
}

让我知道这背后的逻辑。

最佳答案

函数create_in_front()是假的:temp->next = ptr->next;应更改为temp->next = ptr;

create_in_between()不处理 pos==0 的情况.

delete_in_between()完全功能失调:该节点已释放,但其前任节点仍然指向它。

reverse()对我来说似乎是正确的,可以这样简化:

int reverse() {
struct node *prev = NULL;
struct node *curr = head;

while (curr != NULL) {
struct node *next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;

return 0;
}

该问题似乎与您修改 reverse() 无关函数,可能是其他函数中错误的副作用。

关于c - 链表反转功能导致无限打印循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34305910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com