在错误的条目上删除链接条目并在链接列表功能中插入条目 [英] Remove entry and Insert Entry in Linked Lists functions operating on the wrong entries

查看:68
本文介绍了在错误的条目上删除链接条目并在链接列表功能中插入条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Good afternoon. I'm learning the C language from a book called Programming in C Third Edition by Stephen G. Kochan. I wrote some code that is supposed to insert and remove certain entries from a list, which it does, the problem is, it does not remove the right entry, and it does not quite insert the entry in the right spot. The code is below. Any help would be greatly appreciated!

//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()之前.

Move the declarations for InsertEntry and RemoveEntry to before the main().

在初始化上一个"字段时,列表的头部必须具有上一个= NULL".

When initializing Previous fields, head of the list needs to have Previous = NULL.

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

When printing out list, need to avoid printing out Previous->Value and Next->Value for head and tail of list, respectively.

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

In InsertEntry() function, code is passing EntryToInsert struct by value which inserts a copy into the list. I am guessing you intended to pass by pointer which would insert the original stucture into the list.

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

In InsertEntry() function, need to also set InsertPosition->Next->Previous.

在InsertEntry()函数中,设置InsertPosition-> Previous-> Next和InsertPosition-> Next-> Previous之前,需要检查您是否不在列表的开头和结尾.

In InsertEntry() function before setting InsertPosition->Previous->Next and InsertPosition->Next->Previous, need to check that you are not at head and tail of list, respectively.

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

In RemoveEntry() function, need to also set EntryToRemove->Next->Previous.

在设置EntryToRemove-> Previous-> Next和EntryToRemove-> Next-> Previous之前的RemoveEntry()函数中,需要检查您分别不在列表的开头和结尾.

In RemoveEntry() function before setting EntryToRemove->Previous->Next and EntryToRemove->Next->Previous, need to check that you are not at head and tail of list, respectively.

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

In RemoveEntry() function, need to also set EntryToRemove->Previous = NULL and EntryToRemove->Next = NULL.

我建议您尝试自行解决上述每个问题.如果您在解决给定问题时遇到问题,我将在建议的修复程序中包含完整的代码,供您查看.

I would suggest you attempt to fix each of the above mentioned issues on your own. If you run into problems fixing a given issue, I have included the complete code with my suggested fixes for you to look at.

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;
}

这篇关于在错误的条目上删除链接条目并在链接列表功能中插入条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆