寂寞梧桐221 2021-12-28 00:38 采纳率: 60%
浏览 33
已结题

C语言双向链表类型例子

有一个双向链表,将它所有的正数都加倍
比如1 2 -3 -4变为1 1 2 2 -3 -4

  • 写回答

2条回答 默认 最新

  • 关注

    你题目的解答代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct line{
        struct line * prior;
        int data;
        struct line * next;
    }line;
    
    line* initLine(line * head,int a[], int n);
    line * insertLine(line * head,int data,int add);
    line * delLine(line * head,int data);
    void display(line * head);
    int main() {
        int a[] = {1,2,-3,-4,5};
        line * head=NULL;
        head=initLine(head, a,5);
        display(head);
        line * temp=head;
        int i = 0;
        while (temp) {
            i++;
            if (temp->data>0)
                head=insertLine(head, temp->data,i++);
            temp=temp->next;
        }
        display(head);
    
        return 0;
    }
    line* initLine(line * head,int a[], int n){
        head=(line*)malloc(sizeof(line));
        head->prior=NULL;
        head->next=NULL;
        head->data=a[0];
        line * list=head;
        for (int i=1; i<n; i++) {
            line * body=(line*)malloc(sizeof(line));
            body->prior=NULL;
            body->next=NULL;
            body->data=a[i];
    
            list->next=body;
            body->prior=list;
            list=list->next;
        }
        return head;
    }
    line * insertLine(line * head,int data,int add){
        //新建数据域为data的结点
        line * temp=(line*)malloc(sizeof(line));
        temp->data=data;
        temp->prior=NULL;
        temp->next=NULL;
        //插入到链表头,要特殊考虑
        if (add==1) {
            temp->next=head;
            head->prior=temp;
            head=temp;
        }else{
            line * body=head;
            //找到要插入位置的前一个结点
            for (int i=1; i<add-1; i++) {
                body=body->next;
            }
            //判断条件为真,说明插入位置为链表尾
            if (body->next==NULL) {
                body->next=temp;
                temp->prior=body;
            }else{
                body->next->prior=temp;
                temp->next=body->next;
                body->next=temp;
                temp->prior=body;
            }
        }
        return head;
    }
    
    //输出链表的功能函数
    void display(line * head){
        line * temp=head;
        while (temp) {
            printf("%d ",temp->data);
            temp=temp->next;
        }
        printf("\n");
    }
    

    img

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月5日
  • 已采纳回答 12月28日
  • 创建了问题 12月28日

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值