C语言实现单链表指定结点的插入

这篇博客介绍了如何使用C语言实现单链表中指定节点的插入操作。通过两个测试案例展示了插入过程:第一个例子将7插入到1和2之间,链表变为1 7 2 3 4 5;第二个例子将2插入到两个1之间,链表变为1 2 1 2 3 4。
摘要由CSDN通过智能技术生成
#include <bits/stdc++.h>

using namespace std;

typedef struct node
{
    int data;
    struct node *next;
}no;

int main()
{
    no *head,*tail,*p,*q;
    head=new no;
    head->next=NULL;
    tail=head;
    int n;
    printf("一共要输入的数:");
    cin>>n;
    int k;
    cin>>k;
    for(int i=0;i<n;i++)
    {
        p=new no;
        p->data=k;
        p->next=NULL;
        tail->next=p;//因为tail=head,所以tail没有数值,tail->next才有数值
        tail=p;
        cin>>k;
    }
    printf("输入要插入的数:");
    int m;
    cin>>m;
    q=new no;//生成一个结点来存放这个数
    q->data=m;
    q->next=NULL;
    printf("要插在哪个数和哪个数之间:");
    int a,b;
    cin>>a>>b;
    p=head;
    while(p->data!=m&&p->next!=NULL)
    {
       p=p->next;
       if(p->data==a&&p->next->data=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值