删除元素在一个单一的链表 [英] delete elements in a singular linked list

查看:122
本文介绍了删除元素在一个单一的链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此code我正在删除元素链表

11-> 12-> 13-> 14-> 15-> 12-> 16

如果我想删除它12只删除首次出现的元素即O / P WLL是

11-> 13-> 14-> 15-> 12-> 16

但我要删除12所有的发生,该怎么做?

任何人都可以给我一些投入?

 #包括LT&;&stdio.h中GT;
    #包括LT&;&stdlib.h中GT;
    无效insertbeg();
    无效delpos();
    无效显示();
    结构节点
    {
            诠释信息;
            结构节点*链接;
    } *第一= NULL;    结构节点*创建();
    INT项目,重点;
    主要()
    {
            INT选择;
            而(1)
            {
                            的printf(\\ nchoices为。\\ n);
                            的printf(\\ n1.Insertbeg \\ n 2.delpos \\ n 3.显示\\ n一,进入\\ n);
                            的printf(请输入U'r选择:);
                            scanf函数(%d个,&安培;选择);
                            开关(选择)                            {
                                            案例1:insertbeg();打破;
                                            案例2:delpos();打破;
                                            案例3:显示器();打破;
                                            情况4:出口(1);
                          默认:输出(无效选项再试一次的\\ n);
                           }
            }
    }
    结构节点*创建()
    {
            结构节点*新的;
            新=(结构节点*)malloc的(的sizeof(结构节点));
            返回(新);
    }
    无效insertbeg()
    {
            结构节点*新的;
            新创建=();
            的printf(输入元件被插入:);
            scanf函数(%d个,&安培;项目);
            如果(第一== NULL)
            {
                            新建 - >信息=项目;
                            新建 - >链接= NULL;
                            第一=新的;
            }
            其他
            {
                            新建 - >信息=项目;
                            新建 - >链接=第一;
                            第一=新的;
            }
    }
    无效delpos()
    {
            INT关键;
            结构节点*温度,* preV;
            如果(第一== NULL)
            {
            的printf(列表为空\\ n);
                            返回;
            }
            其他
            {
                            TEMP =第一;
                            的printf(请输入关键因素是要被删除:);
                            scanf函数(%d个,&安培;键);
                       / *而(TEMP-GT&;信息=键和放大器;!&安培; TEMP-GT&;!链接= NULL)
                    {
                                            preV =温度;
                                            TEMP = TEMP-GT&;联系;
                            }
                            如果(TEMP-GT&;信息==键)
                     {
                                            preV-GT&;链接= TEMP-GT&;联系;
                                            免费(TEMP);
                            }
                            其他
                                          的printf(列表中的\\ n没有找到关键要素);
            * /                而(TEMP-GT&;!链接= NULL)
                    {
                        如果(TEMP-GT&;信息==键)
                        {
                        preV-GT&;链接= TEMP-GT&;联系;
                        免费(TEMP);
                        TEMP = preV-GT&;联系;
                        TEMP = TEMP-GT&;联系;
                        }
                        其他
                            TEMP = TEMP-GT&;联系;
                    }
            }
    }    无效显示()
    {
            结构节点*温度;
            TEMP =第一;
            如果(临时== NULL)
            {
                            的printf(列表为空\\ n);
                            返回;
            }
            其他
            {
                            的printf(元素链表:);            而(温度!= NULL)
                            {
                                           的printf(%D->中,TEMP-GT&;信息);
                                            TEMP = TEMP-GT&;联系;
                            }
            }
    }


解决方案

我能找到两个问题你code,但没有人会表现出与您的样品输入一个问题。

1

 而(TEMP-GT&;!链接= NULL)

 而(温度!= NULL)

2- TEMP = TEMP-GT&;链接; 是多余的。

 如果(TEMP-GT&;信息==键)
{
   preV-GT&;链接= TEMP-GT&;联系;
   免费(TEMP);
   TEMP = preV-GT&;联系;
   TEMP = TEMP-GT&;联系;
}

和跳过一个元素。

in this code i am deleting the element in the linked list

11->12->13->14->15->12->16

if i want to delete 12 it deletes only the first time occurrence element i.e o/p wll be

11->13->14->15->12->16

but i want to delete all the occurrence of 12, how to do that?

can anyone give me some inputs?

    #include<stdio.h>
    #include<stdlib.h>
    void insertbeg();
    void delpos();
    void display();
    struct node
    {
            int info;
            struct node *link;
    }*first=NULL;

    struct node *create();
    int item,key;
    main()
    {
            int choice;
            while(1)
            {
                            printf("\nchoices are:\n");
                            printf("\n1.Insertbeg\n 2.delpos\n 3.display\n 4.exit\n");
                            printf("Enter U'r choice: ");
                            scanf("%d",&choice);
                            switch(choice)

                            {
                                            case 1: insertbeg(); break;
                                            case 2: delpos(); break;
                                            case 3: display(); break;
                                            case 4: exit(1);
                          default: printf("INVALID CHOICE TRY AGAIN\n");
                           }
            }
    }
    struct node *create()
    {
            struct node *new;
            new=(struct node*)malloc(sizeof(struct node));
            return(new);
    }
    void insertbeg()
    {
            struct node *new;
            new=create();
            printf("Enter element to be inserted: ");
            scanf("%d",&item);
            if(first==NULL)
            {
                            new->info=item;
                            new->link=NULL;
                            first=new;
            }
            else
            {
                            new->info=item;
                            new->link=first;
                            first=new;
            }
    }


    void delpos()
    {
            int key;
            struct node *temp,*prev;
            if(first==NULL)
            {
            printf("LIST IS EMPTY\n");
                            return;
            }
            else
            {
                            temp=first;
                            printf("Enter the KEY element which is to be deleted: ");
                            scanf("%d",&key);
                       /*     while(temp->info!=key&&temp->link!=NULL)
                    {
                                            prev=temp;
                                            temp=temp->link;
                            }
                            if(temp->info==key)
                     {
                                            prev->link=temp->link;
                                            free(temp);
                            }
                            else
                                          printf("key element not found in the list\n");
            */

                while(temp->link != NULL)
                    {
                        if(temp->info == key)
                        {
                        prev->link = temp->link;
                        free(temp);
                        temp = prev->link;
                        temp = temp->link; 
                        }
                        else
                            temp = temp->link;
                    }
            }
    }

    void display()
    {
            struct node *temp;
            temp=first;
            if(temp==NULL)
            {
                            printf("LIST IS EMPTY\n");
                            return;
            }
            else
            {
                            printf("Elements in Linked Lists: ");       

            while(temp!=NULL)
                            {
                                           printf("%d->",temp->info);
                                            temp=temp->link;
                            }
            }
    }

解决方案

I can find two problems with your code but none of them would exhibit a problem with your sample input.

1-

while(temp->link != NULL)

Should be

while(temp!=NULL)

2- The temp = temp->link; is superfluous in the

if(temp->info == key)
{
   prev->link = temp->link;
   free(temp);
   temp = prev->link;
   temp = temp->link; 
}

and skips one element.

这篇关于删除元素在一个单一的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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