猪头少年在哪里 2020-03-07 18:20 采纳率: 50%
浏览 199
已采纳

请各位帮帮忙:指定一个数据,如果双向非循环链表中的有n个重复数据,往其中插入n个新的节点。打印节点数据后就死循环了?

//*******************双向非循环链表**********************//

#include"myhead.h"
```typedef struct dou_list_node
{
    int data;
    struct dou_list_node *next;
    struct dou_list_node *prev;
}dou_list,*dou_link;
```dou_link init_list()
{
    dou_link head_ptr=malloc(sizeof(dou_list));
    head_ptr->prev=NULL;
    head_ptr->next=NULL;
    return head_ptr;
}
```dou_link creat_node(int data)
{
    dou_link newnode_ptr=malloc(sizeof(dou_list));
    newnode_ptr->data=data;//数据域
    newnode_ptr->next=NULL;//指针域
    newnode_ptr->prev=NULL;//指针域
    return newnode_ptr;
}
```int insert_tailnode(int data,dou_link head_ptr)//尾增
{
    dou_link newnode_ptr=creat_node(data);
    if(newnode_ptr==NULL)
    {
        puts("新节点传创建失败");
        return -1;
    }
    dou_link p=head_ptr;//寻找尾节点
while(p->next)
{
    p=p->next;
}
//后增数据
newnode_ptr->prev=p;
p->next=newnode_ptr;
return 0;}
int PriDouList(dou_link head_ptr)
{
    dou_link p=head_ptr->next;
    if(p==NULL)
    {
        puts("链表为空");
        return -1;
    }
    puts("链表数据如下(顺序):");
    while(1)
    {
        printf("%d\n",p->data);
        if(p->next==NULL)
            break;
        p=p->next;

    }

    puts("链表数据如下(逆序):");
    while(p)
    {
        printf("%d\n",p->data);
        p=p->prev;
        if(p==head_ptr)
            break;
    }
    return 0;
}
```int NumOfListNode(dou_link head_ptr)//计算链表的节点数量(不包括头节点)
{
    dou_link p=head_ptr->next;
    if(p==NULL)
    {   puts("链表为空");
        return -1;
    }
  int count=1;
    while(p=p->next)
    {
        count++;
    }
    return count;

}
```int NumOfSpecifiedData(int des_data,dou_link head_ptr)//计算链表中指定数据的数量(不包括头节点)
{
    int count=0;
    dou_link p=head_ptr;
    if(p->next==NULL)
    {
        puts("链表为空");
        return -1;
    }

    while(p=p->next)
    {
        if(p->data==des_data)
        {
            count++;
        }

    }
    return count;
}

int insert2_midnode(int des_data,int newdata,dou_link head_ptr)
{
while(head_ptr->next==NULL)
{
puts("无法对指定位置插入数据,链表为空");
return -1;
}

int num_spe_data=NumOfSpecifiedData(des_data,head_ptr);//寻找目标数据在链表中的数量
if(num_spe_data==0)
{
    puts("找不到指定数据");
    return -1;
}
else
{
    //创建对应数量的新节点
    dou_link newnode_ptr[num_spe_data];
    for(int i=0;i<num_spe_data;i++)
    {
        newnode_ptr[i]=creat_node(newdata);
        //printf("%p\n%d\n",newnode_ptr[i],newnode_ptr[i]->data);
    }

    //找到目标位置并插入数据
    dou_link p=head_ptr->next;
    while(1)
    {
        int i=0;
        if(p->data==des_data&&p->next!=NULL)
        {
            newnode_ptr[i]->next=p->next;
            newnode_ptr[i]->prev=p;
            p->next=newnode_ptr[i];
            newnode_ptr[i]->next->prev=newnode_ptr[i];
            i++;
            p=p->next;
        }
        else if(p->data==des_data&&p->next==NULL)
        {
            newnode_ptr[i]->prev=p;
            p->next=newnode_ptr[i];
            return 0;
        }
        p=p->next;
        if(p==NULL)
            return 0;
    }
}

}
```int main()
{
dou_link head_ptr=init_list();//初始化

for(int i=1;i<4;i++)
{
    insert_tailnode(i,head_ptr);
}
insert_tailnode(2,head_ptr);

PriDouList(head_ptr);




int des_data,newdata;
puts("输入目标节点数据和新节点数据");
scanf("%d%d",&des_data,&newdata);
insert2_midnode(des_data,newdata,head_ptr);
PriDouList(head_ptr);

}

上面的代码希望指定一个数据,在链表中找到相同的数据节点,并往后插入一个数据,如果指定数据在链表中有多个,则插入相应数量的新节点。

情况:编译无报错,如果指定的数据在链表中只有一个,打印出的链表无误。如果有多个,打印出来的链表就死循环了。

弄了好久了,希望热心人帮忙解答,谢谢。

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-03-07 20:51
    关注

    问题解决的话,请点下采纳

    i = 0的位置错了
    导致循环里每次插入的都是第一个节点

    完整的程序如下

    // Q1057585.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <stdlib.h>
    #include <stdio.h>
    
    //*******************双向非循环链表**********************//
    
    typedef struct dou_list_node
    {
        int data;
        struct dou_list_node *next;
        struct dou_list_node *prev;
    }dou_list,*dou_link;
    dou_link init_list()
    {
        dou_link head_ptr=(dou_link)malloc(sizeof(dou_list));
        head_ptr->prev=NULL;
        head_ptr->next=NULL;
        return head_ptr;
    }
    dou_link creat_node(int data)
    {
        dou_link newnode_ptr=(dou_link)malloc(sizeof(dou_list));
        newnode_ptr->data=data;//数据域
        newnode_ptr->next=NULL;//指针域
        newnode_ptr->prev=NULL;//指针域
        return newnode_ptr;
    }
    int insert_tailnode(int data,dou_link head_ptr)//尾增
    {
        dou_link newnode_ptr=creat_node(data);
        if(newnode_ptr==NULL)
        {
            puts("新节点传创建失败");
            return -1;
        }
        dou_link p=head_ptr;//寻找尾节点
        while(p->next)
        {
            p=p->next;
        }
        //后增数据
        newnode_ptr->prev=p;
        p->next=newnode_ptr;
        return 0;}
    int PriDouList(dou_link head_ptr)
    {
        dou_link p=head_ptr->next;
        if(p==NULL)
        {
            puts("链表为空");
            return -1;
        }
        puts("链表数据如下(顺序):");
        while(1)
        {
            printf("%d\n",p->data);
            if(p->next==NULL)
                break;
            p=p->next;
    
        }
    
        puts("链表数据如下(逆序):");
        while(p)
        {
            printf("%d\n",p->data);
            p=p->prev;
            if(p==head_ptr)
                break;
        }
        return 0;
    }
    int NumOfListNode(dou_link head_ptr)//计算链表的节点数量(不包括头节点)
    {
        dou_link p=head_ptr->next;
        if(p==NULL)
        {   puts("链表为空");
        return -1;
        }
        int count=1;
        while(p=p->next)
        {
            count++;
        }
        return count;
    
    }
    int NumOfSpecifiedData(int des_data,dou_link head_ptr)//计算链表中指定数据的数量(不包括头节点)
    {
        int count=0;
        dou_link p=head_ptr;
        if(p->next==NULL)
        {
            puts("链表为空");
            return -1;
        }
    
        while(p=p->next)
        {
            if(p->data==des_data)
            {
                count++;
            }
    
        }
        return count;
    }
    int insert2_midnode(int des_data,int newdata,dou_link head_ptr)
    {
        while(head_ptr->next==NULL)
        {
            puts("无法对指定位置插入数据,链表为空");
            return -1;
        }
        int num_spe_data=NumOfSpecifiedData(des_data,head_ptr);//寻找目标数据在链表中的数量
        if(num_spe_data==0)
        {
            puts("找不到指定数据");
            return -1;
        }
        else
        {
            //创建对应数量的新节点
            dou_link * newnode_ptr = (dou_link *)malloc(sizeof(dou_link) * num_spe_data);
            for(int i=0;i<num_spe_data;i++)
            {
                newnode_ptr[i]=creat_node(newdata);
                //printf("%p\n%d\n",newnode_ptr[i],newnode_ptr[i]->data);
            }
    
            //找到目标位置并插入数据
            dou_link p=head_ptr->next;
            int i=0; //!!!放在这里
            while(1)
            {
                if(p->data==des_data&&p->next!=NULL)
                {
                    newnode_ptr[i]->next=p->next;
                    newnode_ptr[i]->prev=p;
                    p->next=newnode_ptr[i];
                    newnode_ptr[i]->next->prev=newnode_ptr[i];
                    i++;
                    p=p->next;
                }
                else if(p->data==des_data&&p->next==NULL)
                {
                    newnode_ptr[i]->prev=p;
                    p->next=newnode_ptr[i];
                    return 0;
                }
                p=p->next;
                if(p==NULL)
                    return 0;
            }
        }
    
    }
    
    
    
    int main()
    {
        dou_link head_ptr=init_list();//初始化
        for(int i=1;i<4;i++)
        {
            insert_tailnode(i,head_ptr);
        }
        insert_tailnode(2,head_ptr);
    
        PriDouList(head_ptr);
    
        int des_data,newdata;
        puts("输入目标节点数据和新节点数据");
        scanf("%d%d",&des_data,&newdata);
        insert2_midnode(des_data,newdata,head_ptr);
        PriDouList(head_ptr);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集