在链表中的specifeec元素之后添加元素,并删除第一个元素

add element after specifiec element in linked list and delete first element

本文关键字:元素 删除 第一个 添加 之后 specifeec 链表      更新时间:2023-10-16

我尽力解决了这个问题,但只设法部分解决了

我在这个方法中的问题是,我需要在另一个元素之后添加一个元素:

示例: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;
    }
}