鱼C论坛

 找回密码
 立即注册
查看: 1236|回复: 1

链表问题

[复制链接]
发表于 2020-4-18 12:04:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct LNode
{
        int data;
        struct LNode *next;
};

struct LNode* CreateList(int n);
void  print(struct LNode* s);
void main()
{
   struct LNode* Head=NULL;
   int n;
   printf("please input the num of  data:");
   scanf("%d",&n);
   Head= CreateList(n);
   print(Head);printf("\n");


}
struct LNode * CreateList(int n)
{   
        struct LNode* L,p,q;
        int i;
        p=malloc(sizeof(struct LNode));
        if(!p) return 0;
        p->next=NULL;
        q=p;
        for(i=1;i<=n;i++)
        {
                printf("please input the %d nun:",i);
                        scanf("%d",&(q->data));
                L=malloc(sizeof(struct LNode));
                L->next=q;
                q=L;
        }
        return L;
}
void print(struct LNode* s)
{
        struct LNode p=s->next;
        while(p!=NULL)
        {
                printf("%d",p->data);
                p=p->next;
        }
}


各位告诉帮我看看 哪块有问题 (想自己敲出链表的精神小伙)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-18 14:28:45 | 显示全部楼层

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4. struct LNode
  5. {
  6.         int data;
  7.         struct LNode *next;
  8. };

  9. struct LNode* CreateList(int n);
  10. void  print(struct LNode* s);
  11. int main()
  12. {
  13.         struct LNode* Head = NULL;
  14.         int n;
  15.         printf("please input the num of  data:");
  16.         scanf("%d", &n);
  17.         Head = CreateList(n);
  18.         print(Head); printf("\n");
  19.         return 0;

  20. }
  21. struct LNode * CreateList(int n)
  22. {
  23.         struct LNode* pHead, *p, *q;//你想怎么定义链表头?
  24.         int i,a;
  25.         pHead =p=q= NULL;
  26.         for (i = 1; i <= n; i++)
  27.         {
  28.                 //主要是这里面的逻辑,你才理一理才行
  29.                 p = (struct LNode*) malloc(sizeof(struct LNode));//申请内存注意强制转换
  30.                 if (!p) return NULL;
  31.                 printf("please input the %d nun:", i);
  32.                 scanf("%d", &(p->data));
  33.                 if (pHead == NULL) {
  34.                         pHead = p;
  35.                         q = p;
  36.                 }
  37.                 else {
  38.                         q->next = p;
  39.                         q = p;
  40.                 }
  41.         }
  42.         q->next = NULL;
  43.         return pHead;
  44. }
  45. void print(struct LNode* s)
  46. {
  47.         struct LNode* p = s;

  48.         while (p != NULL)
  49.         {
  50.                 printf("%d", p->data);
  51.                 p = p->next;
  52.         }
  53. }
复制代码

稍做修改了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 08:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表