在链表中的specifeec元素之后添加元素,并删除第一个元素
add element after specifiec element in linked list and delete first element
我尽力解决了这个问题,但只设法部分解决了
我在这个方法中的问题是,我需要在另一个元素之后添加一个元素:
示例:add 5 1
5是链表中的一个元素,但我想在5后面加1。
示例:让链表包含以下元素:2 3 7
我调用方法在3之后加1,add 3 1
,所以结果假设为2 3 1 7
,但使用我的方法,结果是2 1 3 7
,这是我的问题。
第二个问题是我无法处理第一个元素:
示例:add 2 1
它的作用就好像第一个元素不存在:
void addNodeAtPos(link *head, int pos,int addelement)
{
link prev=NULL;
link curr =*head;
link newNode = (link)malloc(sizeof(node));
newNode->data = addelement;
while(curr->next != NULL )
{
prev = curr;
curr = curr->next;
if(curr->data == pos)
{
newNode->next = curr;
prev->next = newNode;
break;
}
}
}
我的问题是,我无法删除第一个元素:
void deletenode(link *head,int s){
bool found = false;
node *curr = *head, *prev=NULL;
while(curr != NULL){
// match found, delete
if(curr->data == s){
found = true;
// found at top
if(prev == NULL){
link temp = *head;
curr->next= prev;
delete(temp);
// found in list - not top
}else{
prev->next = curr->next;
delete(curr);
} }
// not found, advance pointers
if(!found){
prev = curr;
curr = curr->next; }
// found, exit loop
else curr = NULL; }
}
以下是第一个问题的解决方案
if(curr->data == pos)
{
// tempNode = curr->next;
// improvement as suggested by @Rerito
newNode->next = curr->next;
curr->next = newNode;
break;
}
您使用的似乎是非圆形双链表。因此,列表的两端都标记有NULL
。现在,在我看来,你以一种非常C风格的方式使用C++。。。(NULL
不会在C++中使用,有nullptr
关键字)。
我将处理您的问题,假设您使用的是C而不是C++。
// Note that I pass a link **ptr, NOT a link *ptr ...
void addNodeAtPos(link **head, int pos, int addelement) {
// I am assuming head will be a valid pointer, if not, please add the appropriate checks.
link *newNode = NULL, *cur = *head;
if (NULL == (newNode = malloc(sizeof(link)))
return;
newNode->data = addelement;
while (cur != NULL) {
if (cur->data == pos || NULL == cur->next) {
newNode->next = cur->next;
newNode->prev = cur; // remove this line if there is no prev pointer.
cur->next = newNode;
if (NULL != newNode->next) { // remove this if clause if there is no prev pointer
newNode->next->prev = newNode;
}
break;
}
cur = cur->next;
}
}
如果找不到"位置",您没有指定应该做什么,我假设在这种情况下,您只是在列表的末尾添加元素。
现在,考虑删除第一个元素的问题:
void deleteNode(link **head, int el)
{
// I assume you wont pass a `NULL` ptr as @head
link *cur = *head, *prev = NULL;
while (cur != NULL) {
if (cur->data == el) {
next = cur->next;
prev = cur->prev;
free(cur);
if (NULL != next)
next->prev = prev;
if (NULL != prev)
prev->next = next;
else
*head = next;
break;
}
cur = cur->next;
}
}
为什么需要传递link **head
而不是link *head
?因为当你删除列表的头时,你必须确保它不再被访问,因此你需要更新你在其他地方使用的头指针。这就是上面函数中*head = next;
语句中所做的内容。
如果您使用的是单链表(只有指向下一个元素的指针,而不是前一个),则解决方案如下:
void deleteNode(link **head, int el)
{
// I assume you wont pass a `NULL` ptr as @head
link *cur = *head, *prev = NULL, *next = NULL;
while (cur != NULL) {
if (cur->data == el) {
if (NULL != prev)
prev->next = cur->next;
else
*head = cur->next;
free(cur);
break;
}
prev = cur;
cur = cur->next;
}
}
相关文章:
- C++如何通过用户输入删除列表元素
- 使用函数"remove"删除重复元素
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- 如何从存储在std::映射中的std::集中删除元素
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 删除列表中的第n个元素
- 从矢量中删除元素后出现隔离错误
- 删除映射和分割错误中的一个过去结束元素
- 如何使用remove_if从矢量中删除元素
- 如何从使用 for 循环中的矢量大小的矢量中删除元素
- 从矢量中删除空元素
- 关于比较两个无序数组并删除 a[] 中可以在 b[] 中找到的元素的问题
- 删除指针数组 (C++) 中的元素
- 删除列表 c++ 中的最后 3 个元素
- 比较 2 个向量并从第二个向量中删除在第一个 - c++ 中找不到的元素
- 为什么通过 vector<reference_wrapper> 的元素删除引用的值<T>不会使向量无效?
- 从std::multiset的第i个元素删除到末尾
- 删除 [] 是否等于每个元素删除.C++
- std::列出元素删除后节点的内存回收