如何删除单链表中的任意节点

发布于 2024-10-26 04:48:57 字数 1643 浏览 1 评论 0原文

这是我的代码:

#include<stdio.h>
#include<stdlib.h>

struct node
{ 
    int x;
    struct node *next;       
};

struct node *addNode(struct node *head, int y)
{
       struct node *traverser;
       traverser = head;
       if(traverser!=NULL)
       {                 
             while(traverser->next!=NULL)
             traverser=traverser->next;

             traverser -> next = malloc(sizeof(struct node));
             traverser -> next -> x = y;
             traverser -> next -> next = NULL;
       }
       else
       {
             head= malloc(sizeof(struct node));
             head -> x=y;
             head -> next = NULL;
       }    
       return head;
 }

 void display(struct node *head)
 {   
     struct node *traverser;
     traverser = head;

     while(traverser!=NULL)
     {
          printf("%d\n",traverser->x);
          traverser=traverser->next; 
     }
 }

 struct node *InitializeList(void)
 {
       return NULL;
 }

int main()
{
    struct node *head;
    head = InitializeList();
    head = addNode(head,2);
    head = addNode(head,15);
    head = addNode(head,5);
    head = addNode(head,8);
    display(head);  

    free(head);
    getchar();
}

我需要像这样删除 main 中的一个节点

struct node *head;
head = InitializeList();
head = addNode(head,2);
head = addNode(head,15);
head = addNode(head,5);
head = addNode(head,8);
display(head);  
removenode(5);
display(head);  
removenode(8); 
display(head);  
free(head);

这是我在删除程序中的特定节点时的 main() 代码。

但我该怎么办呢? removenode() 只是函数名称我应该使用什么算法?或者什么或如何删除它?

Here's my code:

#include<stdio.h>
#include<stdlib.h>

struct node
{ 
    int x;
    struct node *next;       
};

struct node *addNode(struct node *head, int y)
{
       struct node *traverser;
       traverser = head;
       if(traverser!=NULL)
       {                 
             while(traverser->next!=NULL)
             traverser=traverser->next;

             traverser -> next = malloc(sizeof(struct node));
             traverser -> next -> x = y;
             traverser -> next -> next = NULL;
       }
       else
       {
             head= malloc(sizeof(struct node));
             head -> x=y;
             head -> next = NULL;
       }    
       return head;
 }

 void display(struct node *head)
 {   
     struct node *traverser;
     traverser = head;

     while(traverser!=NULL)
     {
          printf("%d\n",traverser->x);
          traverser=traverser->next; 
     }
 }

 struct node *InitializeList(void)
 {
       return NULL;
 }

int main()
{
    struct node *head;
    head = InitializeList();
    head = addNode(head,2);
    head = addNode(head,15);
    head = addNode(head,5);
    head = addNode(head,8);
    display(head);  

    free(head);
    getchar();
}

I need to remove a node in main like this

struct node *head;
head = InitializeList();
head = addNode(head,2);
head = addNode(head,15);
head = addNode(head,5);
head = addNode(head,8);
display(head);  
removenode(5);
display(head);  
removenode(8); 
display(head);  
free(head);

That's my code for main() when it comes to delete specific node in my program.

But how can I do it? The removenode() is just function name what algorithm should I use? Or what or how to remove it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒几许 2024-11-02 04:48:57

不是一个完整的答案,只是一些一般建议

为了弄清楚这种事情,通常只需解决三种情况就足够了

  • 要删除的节点是头
  • 要删除的节点是尾
  • 要删除的节点 确保

您将遇到的最大麻烦来源是

  • 任何前面节点中的“下一个”字段得到正确重置,
  • 以确保调用者获得或保留指向 有效 指针em>新的头。

请注意,有一个递归实现(例如n.next = remove(n.next,val))使这两个问题变得相同,并且您可以将其主要转换为循环防止很长的列表上的堆栈溢出。


可能出现的一个子问题是在列表中找到要删除的节点。您可以通过将查找目标节点的部分与 remove(node* head, node* target) 分开来让您的生活变得更轻松吗?

Not quite a answer, just some general advice

For the purposes of figuring this kind of thing out, it is generally sufficient to solve exactly three cases

  • The node to be removed is the head
  • The node to be removed is the tail
  • the node to be removed is interior to the list

The the big sources of trouble you will encounter are

  • making sure that the "next" field in any preceding nodes get properly reset
  • insuring that the caller gets or retains a valid pointer to the new head.

Note that there is a recursive implementation (think n.next = remove(n.next,val)) that makes these two problems one and the same, and that you can convert it mostly to a loop to prevent stack overflows on very long lists.


A sub-problem that may crop up is that of finding the node to be removed in the list. Can you makae you life easier by separating there part about finding the target node from a remove(node* head, node* target)?

冷默言语 2024-11-02 04:48:57

算法原型需要是:

struct node * removenode(struct node *head, int y);

因为如果删除第一项,原始的“头”指针将不再有效。

该算法只是逐步浏览列表,记住前一项(和头部),然后查找数据。找到后,将前一项的下一个指针设置为当前项的下一项。

The algorithm prototype needs to be:

struct node * removenode(struct node *head, int y);

Since if you're removing the first item, the original "head" pointer will no longer be valid.

The algorithm is simply to step through the list, remembering the previous item (and the head), and looking for the data. When found, set previous item's next pointer to be the current item's next.

少钕鈤記 2024-11-02 04:48:57

从较高层次来看,要删除任何节点,您需要做的是:

1) 指向指向您要删除的节点的项目。

2) 将要删除的项目的引用设置为要删除的项目的下一个项目。

3) 删除要删除的项目。

这样,您的链就得到了维护,并且您已经从内存中释放了该元素。

就像这样:

Head -> Item1 -> Item2 -> Item3 -> NULL

如果你想删除 Item2,你可以这样做:

Head -> Item1 -> Item2 -> Item3 -> NULL
          ^       ^   (Grab pointers to these items)

将 Item1 的旁边设置为 Item2 的下一个,然后删除 Item2。

           /--------------\
Head -> Item1    Item2 -> Item3 -> NULL
          ^       ^ (Delete 2)

编辑:删除项目或项目3:

Head -> Item1 -> Item2 -> Item3 -> NULL
 ^       ^   (Grab pointers to these items)

将头部重新指向项目2,然后删除项目1:

   /--------------\
Head    Item1 -> Item2 -> Item3 -> NULL
 ^       ^ (Delete 1)

Head -> Item1 -> Item2 -> Item3 -> NULL
                    ^       ^   (Grab pointers to these items)

将头部重新指向项目2,然后删除项目1:

                    /--------------\
Head -> Item1 -> Item2   Item3 -> NULL
                   ^       ^ (Delete 3)

At a high level, to remove any node, what you need to do is:

1) Point to the item that is pointing to the node you want to delete.

2) Set the reference to the item you want to delete to the item you want to delete's next item.

3) Delete the item you want to delete.

That way, you chain is maintained and you've freed that element from memory.

Like so:

Head -> Item1 -> Item2 -> Item3 -> NULL

If you want to delete Item2, you go like this:

Head -> Item1 -> Item2 -> Item3 -> NULL
          ^       ^   (Grab pointers to these items)

Set Item1's next to Item2's next, then delete Item2.

           /--------------\
Head -> Item1    Item2 -> Item3 -> NULL
          ^       ^ (Delete 2)

EDIT: Deleting Item or Item3:

Head -> Item1 -> Item2 -> Item3 -> NULL
 ^       ^   (Grab pointers to these items)

Repoint head to Item2, then delete Item1:

   /--------------\
Head    Item1 -> Item2 -> Item3 -> NULL
 ^       ^ (Delete 1)

OR

Head -> Item1 -> Item2 -> Item3 -> NULL
                    ^       ^   (Grab pointers to these items)

Repoint head to Item2, then delete Item1:

                    /--------------\
Head -> Item1 -> Item2   Item3 -> NULL
                   ^       ^ (Delete 3)
时光病人 2024-11-02 04:48:57

看看这是否适合你,也许存储在我的电脑中的某个地方,用于这样的情况:

void RemoveNode(struct node*x)
{
    struct node *temp=x,*tempo=NULL;
    int i=0,n;
    printf("\nWould you like to delete a node?\nPress 1 for Yes 2 for No: ");
    scanf("%d",&i);
    if(i==1)
    {
        printf("Enter nth node to be deleted");
        scanf("%d",&n);
        i=0;
        if(n==1)
        {
        x=temp->next;
        free(temp);
        }
        while(i<n-2)
        {
        temp=temp->next;
        i++;
        }
        if(temp->next->next==NULL)
        {
        tempo=temp->next;
        temp->next=NULL;
        free(tempo);
        }
        else
        {
        tempo=temp->next;
        temp->next=temp->next->next;
        free(tempo);
        }
    }
    else
    printf("\n No Changes Made\nExiting....");
}

它总是最重要的。

See if this works out for you or not, perhaps was stored in my PC somewhere for a situation like this:

void RemoveNode(struct node*x)
{
    struct node *temp=x,*tempo=NULL;
    int i=0,n;
    printf("\nWould you like to delete a node?\nPress 1 for Yes 2 for No: ");
    scanf("%d",&i);
    if(i==1)
    {
        printf("Enter nth node to be deleted");
        scanf("%d",&n);
        i=0;
        if(n==1)
        {
        x=temp->next;
        free(temp);
        }
        while(i<n-2)
        {
        temp=temp->next;
        i++;
        }
        if(temp->next->next==NULL)
        {
        tempo=temp->next;
        temp->next=NULL;
        free(tempo);
        }
        else
        {
        tempo=temp->next;
        temp->next=temp->next->next;
        free(tempo);
        }
    }
    else
    printf("\n No Changes Made\nExiting....");
}

It always the Head that counts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文