鱼C论坛

 找回密码
 立即注册
查看: 1847|回复: 2

关于L2申请空间问题

[复制链接]
发表于 2020-11-13 09:35:16 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef char datatype;
  4. typedef struct node {
  5.         datatype data;
  6.         struct node *next;
  7. } linklist;

  8. linklist *creatlistafter() {
  9.         linklist *p,*s,*head;
  10.         datatype d;
  11.         head=malloc(sizeof(linklist));
  12.         head->next=NULL;
  13.         p=head;
  14.         printf("输入元素值(回车结束):");
  15.         rewind(stdin);
  16.         scanf("%c",&d);
  17.         while(d!='\n') {
  18.                 s=malloc(sizeof(linklist));
  19.                 s->data=d;
  20.                 p->next=s;
  21.                 p=s;
  22.                 scanf("%c",&d);
  23.         }
  24.         if(p!=NULL)        p->next=NULL;
  25.         return head;
  26. }

  27. linklist *spe(linklist *L1,linklist *L2,int i) {
  28.         int j;
  29.         linklist *p;
  30.         p=L1;
  31.         L2=malloc(sizeof(linklist));//L2申请空间
  32.         L2->next=NULL;
  33.         if(i<0)        printf("下标不能为负数\n");
  34.         for(j=0; j<i; j++) {
  35.                 if(p->next==NULL) {
  36.                         printf("链表长度不够!\n");
  37.                         break;
  38.                 }
  39.                 p=p->next;
  40.         }
  41.         if(p->next!=NULL) {
  42.                 L2->next=p->next;
  43.                 p->next=NULL;
  44.         }

  45. }

  46. void print(head)
  47. linklist *head;
  48. {
  49.         linklist *p;
  50.         p=head->next;
  51.         while(p!=NULL) {
  52.                 printf("%c      ",p->data);
  53.                 p=p->next;
  54.         }
  55.         printf("\n");
  56. }

  57. int main() {
  58.         linklist *L1,*L2;
  59.         int i;
  60.         datatype x;
  61.         L1=creatlistafter();
  62.         print(L1);
  63.         printf("输入要分开的位置:");
  64.         scanf("%d",&i);
  65.         spe(L1,L2,i);
  66.         printf("L1:");
  67.         print(L1);
  68.         printf("L2:");
  69.         print(L2);
  70. }
复制代码

测试了一下发现L2申请写在主函数没问题,但写在别的函数就不行,想请教各位是什么原因?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-13 10:03:03 | 显示全部楼层
用个二级指针试试看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-13 14:46:01 | 显示全部楼层
chxchxkkk 发表于 2020-11-13 10:03
用个二级指针试试看

好像不是二级指针的原因,我在单独一个函数内用一级指针也可以
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef char datatype;
  4. typedef struct node {
  5.         datatype data;
  6.         struct node *next;
  7. } linklist;

  8. linklist *creatlistafter() {
  9.         linklist *p,*s,*head;
  10.         datatype d;
  11.         head=malloc(sizeof(linklist));
  12.         head->next=NULL;
  13.         p=head;
  14.         printf("输入元素值(回车结束):");
  15.         rewind(stdin);
  16.         scanf("%c",&d);
  17.         while(d!='\n') {
  18.                 s=malloc(sizeof(linklist));
  19.                 s->data=d;
  20.                 p->next=s;
  21.                 p=s;
  22.                 scanf("%c",&d);
  23.         }
  24.         if(p!=NULL)        p->next=NULL;
  25.         return head;
  26. }

  27. void init(linklist *L2){
  28.         L2=malloc(sizeof(linklist));//L2申请空间
  29.         L2->next=NULL;
  30. }

  31. linklist *spe(linklist *L1,linklist *L2,int i) {
  32.         int j;
  33.         linklist *p;
  34.         p=L1;
  35.         if(i<0)        printf("下标不能为负数\n");
  36.         for(j=0; j<i; j++) {
  37.                 if(p->next==NULL) {
  38.                         printf("链表长度不够!\n");
  39.                         break;
  40.                 }
  41.                 p=p->next;
  42.         }
  43.         if(p->next!=NULL) {
  44.                 L2->next=p->next;
  45.                 p->next=NULL;
  46.         }

  47. }

  48. void print(head)
  49. linklist *head;
  50. {
  51.         linklist *p;
  52.         p=head->next;
  53.         while(p!=NULL) {
  54.                 printf("%c      ",p->data);
  55.                 p=p->next;
  56.         }
  57.         printf("\n");
  58. }

  59. int main() {
  60.         linklist *L1,*L2;
  61.         init(L2);
  62.        
  63.         int i;
  64.         datatype x;
  65.         L1=creatlistafter();
  66.         print(L1);
  67.         printf("输入要分开的位置:");
  68.         scanf("%d",&i);
  69.         spe(L1,L2,i);
  70.         printf("L1:");
  71.         print(L1);
  72.         printf("L2:");
  73.         print(L2);
  74. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-9 19:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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