免费注册 查看新帖 |

Chinaunix

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

求助---向双链表中插入数据问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-11 22:20 |只看该作者 |倒序浏览
  1. #include <stdio.h>
  2. typedef char elemtype;
  3. typedef struct node
  4. {
  5.    elemtype data;
  6.    struct node *next,*prior;
  7. }lnode,*linklist;

  8. lnode *creat()          /*建立双链表*/
  9. {
  10.    elemtype x;
  11.    lnode *h,*p,*t;
  12.    h=(lnode *)malloc(sizeof(lnode));
  13.    h->next=h;
  14.    t=h;
  15.    while((x=getchar())!='\n')
  16.    {
  17.       p=(lnode *)malloc(sizeof(lnode));
  18.       p->data=x;
  19.       p->next=h;
  20.       p->prior=h;
  21.       t->next=p;
  22.       t=p;
  23.    }
  24.    return(h);
  25. }


  26. int insert(lnode *h,int i,elemtype x)         /*向第i个元素前插入S结点*/
  27. {  int j=1;
  28.    lnode *p,*s;
  29.    p=h->next;
  30.    while(p!=h&&j<i)
  31.    {  ++j;
  32.       p=p->next;
  33.     }
  34.     if(i>0&&i==j)
  35.     {
  36.        s=(lnode *)malloc(sizeof(lnode));
  37.        s->data=x;
  38.        s->prior=p->prior;
  39.        p->prior->next=s;
  40.        s->next=p;
  41.        p->prior=s;
  42.        return(1);
  43.      }
  44.      else
  45.      return(0);
  46. }


  47. int                                             /*主函数*/
  48. main(void)
  49. {  int j;
  50.    elemtype c='a',d='b';
  51.    lnode *h,*p;
  52.    h=creat();
  53.    p=h->next;
  54.    while(p!=h)
  55.    {
  56.       printf("%c",p->data);
  57.       p=p->next;
  58.     }
  59.     printf("\n");
  60.     if(insert(h,3,c))
  61.     {
  62.      while(p!=h)
  63.    {
  64.       printf("%c",p->data);
  65.       p=p->next;
  66.     }
  67.     }
  68.        getch();
  69.     return(0);
  70. }
复制代码
感觉没什么问题,但是不能输出插入后的新表???大家帮忙看看,谢谢!!!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2006-04-11 22:41 |只看该作者
楼主的是双向循环链表吗?
你看这里是不是要改一下。

  1. lnode *creat()          /*建立双链表*/
  2. {
  3.    elemtype x;
  4.    lnode *h,*p,*t;
  5.    h=(lnode *)malloc(sizeof(lnode));
  6.    h->next=h;
  7.    t=h;
  8.    while((x=getchar())!='\n')
  9.    {
  10.       p=(lnode *)malloc(sizeof(lnode));
  11.       p->data=x;
  12.       p->next=h;
  13.       p->prior=t;//here
  14.       t->next=p;
  15.       t=p;
  16.    }
  17.    return(h);
  18. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2006-04-12 13:10 |只看该作者
哦,对,那里是不对,呵呵,但改完还是不能完成插入啊??
还是要感谢lenovo大哥给我看程序!!!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2006-04-12 14:00 |只看该作者

  1.     if(insert(h,3,c))
  2.     {
  3.         p = h -> next; //here
  4.        while(p!=h)
  5.       {
  6.            printf("%c",p->data);
  7.           p=p->next;
  8.       }
  9.     }
复制代码

论坛徽章:
0
5 [报告]
发表于 2006-04-12 14:06 |只看该作者
哦,终于弄懂了,还是基础不牢啊,呵呵!
再次谢谢lenovo大哥!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP