OJ 嘻唰唰:熟悉题型——填空题(删除线性表节点)

Description

给出一串具体长度的数据,删除指定数据。

Input

输入 n:6

输入数据:1 2 3 4 5 6

输入 item:5

Output

输出:1 2 3 4 6

Sample Input

10
1 2 3 4 5 6 7 8 9 10
8

Sample Output

1 2 3 4 5 6 7 9 10 

HINT

只提交注释的部分,即填写的部分。


填写的部分可能不止一行,根据自己的判断添加。


填空题添加的代码一般很短。

#include<iostream>
using namespace std;
struct Linklist
{
    int num;
    Linklist *next;
};
Linklist *creat(int l,int n)
{
    Linklist *t=new Linklist;
    t->num=l;//每个节点编号
    if(n==1)
    {
        t->next=NULL;
        return t;
    }
    cin>>l;
    t->next=creat(l,n-1);
    return t;
}
void printf(Linklist *p,int n)
{
    if(p==NULL)
        return ;
    cout<<p->num;
    if(n!=1)
        cout<<" ";
    printf(p->next,n--);
}
Linklist *dellink(Linklist *head,int num)
{
    Linklist *p1,*p2;
    p1=head;
    while(num!=p1->num&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    /************************************************/
    if(num==p1->num)//当找到要删除的num时
    {
        if(p1==head)//要删除的数值为第一个数时
            head=p1->next;
        else p2->next=p1->next;//否则的话,将下一结点地址赋给前一结点(即跳过num,指向下一个数值)
    }
    /************************************************/
    return head;
}
int main()
{
    int n,l,m;
    cin>>n;
    Linklist *head;
    cin>>l;
    head=creat(l,n);
    cin>>m;
   head= dellink(head,m);
    printf(head,n);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值