m0_74539947 2022-11-05 00:44 采纳率: 100%
浏览 29
已结题

纯C语言实现单链表操作

#include <stdio.h>
#include <malloc.h>
#include 
typedef int ElemType;

typedef struct node {
    int data;        //数据域    
    struct node *next;    //指针域
} SLinkNode ;                //单链表类型

void InitList(SLinkNode *&L1)
{
    //初始化单链表L
    L1 = (SLinkNode )malloc(sizeof(SLinkNode));    //创建头结点L
    L1->next = NULL;
}

void DispList(SLinkNode *L1)        //输出单链表
{    SLinkNode *p=L1->next;
    while (p!=NULL)
    {    printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}

void  CreateListF(SLinkNode *&L1, int a[],int n) {
    //整体创建单链表
    L1 = (SLinkNode *)malloc(sizeof(SLinkNode));
    L1->next = NULL; //置空
    int b;
    SLinkNode *s;//s节点
        for (b = 0; b < n; b++)
    {
        //头插法
    s = (SLinkNode *)malloc(sizeof(SLinkNode));
    s->data = a[b];
        s->next = L1->next; //置于L之后 并且将s->next置空
    L1->next = s; //
    }
}
void      basic_action()
{
    int a[11];
    int n;  
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
     SLinkNode *L1;
    InitList(L1);
    CreateListF(L1,a,n); 
    DispList(L1);

}

int main()

{
    void basic_action();
    return 0;
}

编译运行的话 什么都没有
有没有人看看这个是什么原因

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2022-11-05 10:38
    关注

    改动处见注释,供参考:

    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    typedef int ElemType;
    
    typedef struct node {
        int data;        //数据域
        struct node *next; //指针域
    } SLinkNode ;          //单链表类型
    
    void InitList(SLinkNode *&L1)
    {
        //初始化单链表L
        L1 = (SLinkNode*)malloc(sizeof(SLinkNode));//创建头结点L
        L1->next = NULL;
    }
    
    void DispList(SLinkNode *L1)//输出单链表
    {    SLinkNode *p=L1->next;
        while (p!=NULL)
        {    printf("%d ",p->data);
            p=p->next;
        }
        printf("\n");
    }
    
    void  CreateListF(SLinkNode *&L1, int a[],int n) {
        //整体创建单链表
                   //L1 = (SLinkNode *)malloc(sizeof(SLinkNode)); 修改
                   //L1->next = NULL; //置空                      修改
        int b;
        SLinkNode *s;//s节点
        for (b = 0; b < n; b++)
        {
            //头插法
            s = (SLinkNode *)malloc(sizeof(SLinkNode));
            s->data = a[b];
            s->next = L1->next;//置于L之后 并且将s->next置空
            L1->next = s;
        }
    }
    void basic_action()
    {
        int a[11];
        int n;  
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        SLinkNode *L1;
        InitList(L1);
        CreateListF(L1,a,n);
        DispList(L1);
    
    }
    int main()
    {
        basic_action();       //void basic_action(); 修改
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月14日
  • 已采纳回答 11月6日
  • 创建了问题 11月5日

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line