gpt4 book ai didi

c - 删除条目和在链接列表中插入条目功能对错误条目进行操作

转载 作者:行者123 更新时间:2023-11-30 18:53:01 26 4
gpt4 key购买 nike

下午好。我正在从 Stephen G. Kochan 所著的《C 语言编程第三版》一书中学习 C 语言。我编写了一些代码,应该从列表中插入和删除某些条目,它确实做到了,问题是,它没有删除正确的条目,并且它没有完全将条目插入到正确的位置。代码如下。任何帮助将不胜感激!

//Insert and Remove entry functions using doubly linked lists
#include <stdio.h>

struct Entry
{
int Value;
struct Entry *Previous;
struct Entry *Next;
};


int main()
{
void InsertEntry (struct Entry *InsertPosition, struct Entry EntryToInsert);
void RemoveEntry (struct Entry *EntryToRemove);
struct Entry N1, N2, N3, N4, N5, Insert, *Start = &N1;

//set initial values
N1.Value = 10;
N2.Value = 20;
N3.Value = 20;
N4.Value = 30;
N5.Value = 40;
Insert.Value = 35;

//link the list

N1.Next = &N2;
N2.Next = &N3;
N3.Next = &N4;
N4.Next = &N5;
N5.Next = (struct Entry *) 0;
//Link again

N1.Previous = &N1;
N2.Previous = &N1;
N3.Previous = &N2;
N4.Previous = &N3;
N5.Previous = &N4;

InsertEntry(&N4, Insert);
RemoveEntry(&N2);

//Display the Lists
while (Start->Next != (struct Entry *) 0)
{
printf("Previous: %i, Current: %i, Next: %i\n", Start->Previous->Value, Start->Value, Start->Next->Value);
Start = Start->Next;
}

return 0;
}

void InsertEntry (struct Entry *InsertPosition, struct Entry EntryToInsert)
{

EntryToInsert.Previous = InsertPosition->Previous;
EntryToInsert.Next = InsertPosition;
InsertPosition->Previous->Next = &EntryToInsert;

}

void RemoveEntry (struct Entry *EntryToRemove)
{
EntryToRemove->Previous->Next = EntryToRemove->Next;
}

最佳答案

代码中存在几个问题。

将 InsertEntry 和 RemoveEntry 的声明移至 main() 之前。

初始化Previous字段时,列表头需要Previous = NULL。

打印列表时,需要避免分别打印列表头和尾部的 Previous->Value 和 Next->Value。

在 InsertEntry() 函数中,代码按值传递 EntryToInsert 结构,将副本插入到列表中。我猜您打算传递指针,将原始结构插入列表中。

在InsertEntry()函数中,还需要设置InsertPosition->Next->Previous。

在InsertEntry()函数中设置InsertPosition->Previous->Next和InsertPosition->Next->Previous之前,需要检查是否分别位于列表的头部和尾部。

在RemoveEntry()函数中,还需要设置EntryToRemove->Next->Previous。

在RemoveEntry()函数中,在设置EntryToRemove->Previous->Next和EntryToRemove->Next->Previous之前,需要检查是否分别位于列表的头部和尾部。

在RemoveEntry()函数中,还需要设置EntryToRemove->Previous = NULL和EntryToRemove->Next = NULL。

我建议您尝试自行解决上述每个问题。如果您在修复给定问题时遇到问题,我已包含完整的代码以及我建议的修复方案供您查看。

struct Entry
{
int Value;
struct Entry *Previous;
struct Entry *Next;
};

void InsertEntry(struct Entry *InsertPosition, struct Entry *EntryToInsert);
void RemoveEntry(struct Entry *EntryToRemove);

int main()
{
struct Entry N1, N2, N3, N4, N5, Insert, *Start = &N1;

//set initial values
N1.Value = 10;
N2.Value = 20;
N3.Value = 20;
N4.Value = 30;
N5.Value = 40;
Insert.Value = 35;

//link the list

N1.Next = &N2;
N2.Next = &N3;
N3.Next = &N4;
N4.Next = &N5;
N5.Next = NULL;
//Link again

N1.Previous = NULL;
N2.Previous = &N1;
N3.Previous = &N2;
N4.Previous = &N3;
N5.Previous = &N4;

InsertEntry(&N4, &Insert);
RemoveEntry(&N2);

//Display the Lists
while (Start != (struct Entry *) 0)
{
printf("Previous: ");
if (Start->Previous != NULL)
{
printf("%i", Start->Previous->Value);
}
else
{
printf("NULL");
}
printf(", Current: %i, Next: ", Start->Value);
if (Start->Next != NULL)
{
printf("%i", Start->Next->Value);
}
else
{
printf("NULL");
}
printf("\n");
Start = Start->Next;
}

return 0;
}

void InsertEntry(struct Entry *InsertPosition, struct Entry *EntryToInsert)
{
EntryToInsert->Previous = InsertPosition->Previous;
EntryToInsert->Next = InsertPosition;
if (InsertPosition->Previous != NULL)
{
InsertPosition->Previous->Next = EntryToInsert;
}
InsertPosition->Previous = EntryToInsert;

}

void RemoveEntry(struct Entry *EntryToRemove)
{
if (EntryToRemove->Previous != NULL)
{
EntryToRemove->Previous->Next = EntryToRemove->Next;
}
if (EntryToRemove->Next != NULL)
{
EntryToRemove->Next->Previous = EntryToRemove->Previous;
}
EntryToRemove->Previous = NULL;
EntryToRemove->Next = NULL;
}

关于c - 删除条目和在链接列表中插入条目功能对错误条目进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713395/

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