孤寡小白学计科 2021-10-05 21:16 采纳率: 100%
浏览 85
已结题

数据结构——单链表长度的计算并将其结构存放于头节点的数据域中

请编写一个完整的程序
1.计算单链表的长度,并将结果存放在头节点的数据域中,然后输出单链表。(在下面的代码中应该怎么做?)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
    struct Node{
        int Data;
        struct Node*Next;
    };
    void Print(struct Node *L){
        struct Node *q;
        q=L->Next;
        while(q!=NULL){
            printf("%d  ",q->Data);
            q=q->Next;
        }
    }

    void Insert(struct Node*L,int n){    //插入数据 
        struct Node*p,*q;
        p=L;
        q=(struct Node*)malloc(sizeof(struct Node));
        while(p->Next!=NULL&&p->Next->Data<n){
            p=p->Next;
        }
        q->Data=n;
        q->Next=p->Next;
        p->Next=q;    
        
    }

    void length(Node *L) //计算单链表长度 
{
    Node *p;
    *p=*L->Next;
    int j=0;
    while(p!=NULL)
    {
        p=p->Next;
        j++;
    }
    
    printf("%d",j); //为什么在运行结果中无法显示?
    printf("\n");
}
    int main(){
        struct Node *L;
        L=(struct Node*)malloc(sizeof(struct Node));
        L->Next=NULL;
        srand((int)time(NULL));
        int i;
        for(i=1;i<20;i++){
    
            Insert(L,rand()%200); 
        }
        Print(L);
        return 0;
    }



  • 写回答

2条回答 默认 最新

  • 关注

    主要是

    *p = *L->Next;

    写错了,应该是

    p = L->Next;
    

    length(struct Node *L)函数没有调用啊
    你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    struct Node
    {
        int Data;
        struct Node *Next;
    };
    void Print(struct Node *L)
    {
        struct Node *q;
        q = L->Next;
        while (q != NULL)
        {
            printf("%d  ", q->Data);
            q = q->Next;
        }
    }
    void Insert(struct Node *L, int n)
    { //插入数据
        struct Node *p, *q;
        p = L;
        q = (struct Node *)malloc(sizeof(struct Node));
        while (p->Next != NULL && p->Next->Data < n)
        {
            p = p->Next;
        }
        q->Data = n;
        q->Next = p->Next;
        p->Next = q;
    }
    void length(struct Node *L) //计算单链表长度
    {
        struct Node *p;
        p = L->Next;
        int j = 0;
        while (p != NULL)
        {
            p = p->Next;
            j++;
        }
        L->Data = j;
        printf("%d", j); //为什么在运行结果中无法显示?
        printf("\n");
    }
    int main()
    {
        struct Node *L;
        L = (struct Node *)malloc(sizeof(struct Node));
        L->Next = NULL;
        srand((int)time(NULL));
        int i;
        for (i = 1; i < 20; i++)
        {
            Insert(L, rand() % 200);
        }
        length(L);
        Print(L);
        return 0;
    }
    

    img

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

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月8日
  • 已采纳回答 10月5日
  • 创建了问题 10月5日

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c