免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1004 | 回复: 5
打印 上一主题 下一主题

:oops: :oops: 链表!创建的问题! [复制链接]

论坛徽章:
1
15-16赛季CBA联赛之四川
日期:2016-02-15 17:12:32
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-05-27 21:57 |只看该作者 |倒序浏览
  
  1. # include "stdio.h"
  2. # include "stdlib.h"

  3. struct st{
  4.         int a;
  5.         struct st *next;
  6. }
  7. main ()
  8. {
  9.         int n;
  10.         struct st *p,*q,*head;
  11.         head=p=q=NULL;
  12.         scanf ("%d",&n);
  13.         clrscr();
  14.         while (n!=0) {
  15.                 p=(struct st*)malloc(sizeof(struct st));
  16.                 p->;a=n;
  17.                 if (head==NULL) p=head;
  18.                 else {
  19.                         q=p;
  20.                         p=q->;next;
  21.                 }
  22.                 p->;next=NULL;
  23.                 scanf ("%d",&n);
  24.         }
  25.         p=head;
  26.         while (p!=NULL) {
  27.                 printf ("%2d",p->;a);
  28.                 p=p->;next;
  29.         }
  30. }
复制代码


有什么问题?为什么不打印啊!

论坛徽章:
0
2 [报告]
发表于 2004-05-27 22:11 |只看该作者

:oops: :oops: 链表!创建的问题!

if (head==NULL) p=head;
怀懝你是不是想写个恒等式呀?

编程要细心的。还有,建议使用 prev,next,current,guard 等作指针,便于阅读。

论坛徽章:
0
3 [报告]
发表于 2004-05-27 22:22 |只看该作者

:oops: :oops: 链表!创建的问题!

  1. # include <stdio.h>;
  2. # include <stdlib.h>;
  3. # include <string.h>;

  4. typedef struct st {
  5.    int a;  
  6.    struct st *next;
  7. } ST;

  8. int main (int argc, char *argv[] )
  9. {
  10.    int n;
  11.    struct st *p,*q,*head;
  12.    head=p=q=NULL;

  13.    printf ( "Input no:" );
  14.    scanf ("%d",&n);

  15. //   clrscr();
  16.    while (n!=0)
  17.    {
  18.       p=(struct st*)malloc(sizeof(struct st));
  19.           memset ( p, 0, sizeof(ST) );
  20.       p->;a=n;
  21.           p->;next = NULL;

  22.       if (head==NULL) head = q = p;//NOT : p = head;
  23.       else                         // q->;p->;
  24.           {
  25.                  q->;next = p;
  26.          q=p;
  27.       }
  28.           
  29.       //p->;next=NULL; WRONG

  30.           printf ( "Input no:" );
  31.           scanf ("%d",&n);
  32.    }

  33.    ST *tmp = NULL;
  34.    p=head;
  35.    while (p!=NULL) {                      // 空间申请了别忘了释放
  36.       printf ("%2d",p->;a);
  37.           tmp = p;
  38.       p=p->;next;
  39.           free(tmp);
  40.    }

  41.    return 0;
  42. }
复制代码

论坛徽章:
1
15-16赛季CBA联赛之四川
日期:2016-02-15 17:12:32
4 [报告]
发表于 2004-05-28 18:29 |只看该作者

:oops: :oops: 链表!创建的问题!

temp是用来干什么的啊

论坛徽章:
0
5 [报告]
发表于 2004-05-28 18:42 |只看该作者

:oops: :oops: 链表!创建的问题!

  1. ST *tmp = NULL;
  2.    p=head;
  3.    while (p!=NULL) {                      // 空间申请了别忘了释放
  4.       printf ("%2d",p->;a);
  5.      tmp = p;
  6.       p=p->;next;
  7.      free(tmp);
  8.    }

  9.    
复制代码


tmp 用来存储当前需要释放的指针地址。
多看看数据结构或算法的书。

论坛徽章:
1
15-16赛季CBA联赛之四川
日期:2016-02-15 17:12:32
6 [报告]
发表于 2004-05-28 19:19 |只看该作者

:oops: :oops: 链表!创建的问题!

tmp 在这的作用是释放掉p指针的空间 这样是不是把链表给删掉了?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP