简体   繁体   English

在C ++中的链接列表中的任意位置插入/删除

[英]Insert/Delete Anywhere in a Linked List in C++

I have been learning C++ for a while, and just started looking at linked lists recently. 我学习C ++已有一段时间了,最​​近才开始查看链接列表。 I can construct a template class List with the usual functions insert/remove from back/front. 我可以使用从后/前插入/删除常用功能构造一个模板类List。 Now I came up with an exercise that asks me to write a function to handle insertions/deletions anywhere in the list. 现在我想出了一个练习,要求我编写一个函数来处理列表中任何位置的插入/删除。

Please bear in mind that my questions are very basic. 请记住,我的问题是非常基本的。

The problem I have is that I see the question as ambiguous. 我的问题是我认为这个问题模棱两可。 What kind of information does the function require? 该功能需要什么样的信息? For instance, for deletion, I can come up with several candidates: 1) delete the first node that has a particular value (argument: value) 2) delete all nodes with a particular value (argument: value) 3) delete a particular node (argument: pointer to that node) 1) and 2) I can easily code. 例如,对于删除,我可以提出几个候选对象:1)删除具有特定值(参数:值)的第一个节点2)删除具有特定值(参数:值)的所有节点3)删除特定节点(参数:指向该节点的指针)1)和2)我可以轻松编写代码。 3) is harder but I can also do it. 3)比较难,但我也可以做到。 I just don't see the point in 3). 我只是看不到3)中的要点。 Is it usual to manipulate nodes (outside the list definition) when using lists? 使用列表时,通常会操纵节点(在列表定义之外)吗? As in, is it usual for a program using lists to actually manipulate pointers to the nodes? 像这样,使用列表的程序通常实际操作指向节点的指针吗?

What is the usual meaning of "delete anywhere" in this setting? 在此设置中“删除任何地方”的通常含义是什么?

Similarly, for "insert anywhere", the wording is strange. 同样,对于“在任何地方插入”,措辞很奇怪。 What does "anywhere" mean? “任何地方”是什么意思? Is the place in the linked list supposed to be given by a particular node? 链表中的位置是否应该由特定节点指定?

In linked list you have constant time access to first element. 在链接列表中,您可以恒定时间访问第一个元素。 So delete/insert anywhere means the place that exists between first and last element. 因此,在任何地方删除/插入都意味着第一个元素与最后一个元素之间存在的位置。 Basically you need to have 2 iterators. 基本上,您需要有2个迭代器。 When you find the place you want remove/insert a element, you should refer to the object just before it. 当找到要删除/插入元素的位置时,应在该对象之前引用该对象。 because you have not access to the prev element, only to the next one: Let's say our linked list looks like this: 因为您无权访问prev元素,而只能访问下一个元素:假设我们的链接列表如下所示:
E0->E1->E2->E3->E4 E0-> E1-> E2-> E3-> E4

If you want remove E3, you need to have iterator set on E2 so that you could correct pointers for E2->next. 如果要删除E3,则需要在E2上设置迭代器,以便可以更正E2-> next的指针。

Good reference is the book Standard Library wrote by Nicolai M. Josuttis. 很好的参考书是Nicolai M. Josuttis撰写的Standard Library一书。 The problem you have encountered is widely described there. 您所遇到的问题在此处得到了广泛描述。

node*insert(node*head,int d){

    node*temp=new node;
    temp->data=d;
    if(head==NULL){
        temp->next=NULL;
        head=temp;}
    else 
         {node*curr=head,*pre=NULL;
    while (curr!=NULL && curr->data<temp->data)
    {
        pre=curr;
        curr=curr->next;
    }
    temp->next=curr;
    if(pre==NULL)
        head=temp;
    pre->next=temp;
    }
    return head;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM