| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 343 人关注过本帖
标题:链表问题,检测通过,但运行有问题,求助
只看楼主 加入收藏
daming1
Rank: 1
来 自:开平
等 级:新手上路
帖 子:21
专家分:0
注 册:2011-11-26
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:4 
链表问题,检测通过,但运行有问题,求助
#include<stdio.h>
#include<malloc.h>
typedef struct lnode
{char data;
struct lnode *next;
}listnode,*linklist;

struct lnode * creat()
{
   struct lnode *head,*p1,*p2;  
   int n,i;
   printf("请输入结点个数:");
   scanf("%d",&i);
   head=p2=NULL;
   for(n=1;n<=i;n++)
   {
      p1=(struct lnode*)malloc(sizeof(struct lnode));
      printf("请输入第%d个结点值:",n);
      scanf("%c",&p1->data);
      if(n==1) head=p1;
      else  p2->next=p1;
      p2=p1;      
    }
   p2->next=NULL;   
   return(head);
}


linklist Demo(linklist L)
{listnode *Q,*p;
if(L&&L->next)
{
    Q=L;L->next;p=L;
    while(p->next)
        p=p->next;
    p->next=Q;Q->next=NULL;
}
return L;
}

void listpint(linklist L)
{
    linklist p;
    int i=0;
    p=L;
    while(p!=NULL)
    {i++;
    printf("第%d个元素是:",i);
    printf("%c\n",p->data);
    p=p->next;
    }
}


void main()
{
    linklist head;
    head=(linklist)malloc(sizeof(lnode));
    head=creat();
    listpint(head);
    printf("经过Demo函数处理后:");
    head=Demo(head);
    listpint(head);
}
搜索更多相关主题的帖子: include next head 
2012-03-10 15:50
adgvcxz
Rank: 2
等 级:论坛游民
帖 子:23
专家分:52
注 册:2009-6-26
收藏
得分:8 
回复 楼主 daming1
程序代码:
#include<stdio.h>
#include<malloc.h>
typedef struct lnode
{
    char data;
    struct lnode *next;
}listnode,*linklist;

struct lnode * creat()
{
   struct lnode *head,*p1,*p2; 
   int n,i;
   printf("请输入结点个数:");
   scanf("%d",&i);
   head=p2=NULL;
   for(n=1;n<=i;n++)
   {
      p1=(struct lnode *)malloc(sizeof(struct lnode));
      printf("请输入第%d个结点值:",n);
      scanf(" %c",&p1->data);
      if(n==1) head=p1;
      else  p2->next=p1;
      p2=p1;     
    }
   printf("a%c",p2->data);
   p2->next=NULL;   
   return(head);
}


linklist Demo(linklist L)
{listnode *Q,*p;
if(L&&L->next)
{
    Q=L;L->next;p=L;
    while(p->next)
        p=p->next;
    p->next=Q;Q->next=NULL;
}
return L;
}

void listpint(linklist L)
{
    linklist p;
    int i=0;
    p=L;
    while(p!=NULL)
    {i++;
    printf("第%d个元素是:",i);
    printf("%c\n",p->data);
    p=p->next;
    }
}


void main()
{
    linklist head;
    head=(linklist)malloc(sizeof(linklist));
    head=creat();
    listpint(head);
    printf("经过Demo函数处理后:");
    head=Demo(head);
    listpint(head);
}
2012-03-10 16:23
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:2 
编译器给你了调试功能  为啥不用呢

                                         
===========深入<----------------->浅出============
2012-03-10 16:51
daming1
Rank: 1
来 自:开平
等 级:新手上路
帖 子:21
专家分:0
注 册:2011-11-26
收藏
得分:0 
2楼的确实可以,但是我将我的代码改成跟他一样了(真的一样的),就是我的不行,真奇怪呀,我晕,同一代码,竟然不同结果。,我的就能输入第2结点。这是我的:
#include<stdio.h>
#include<malloc.h>
typedef struct lnode
{char data;
struct lnode *next;
}listnode,*linklist;

struct lnode * creat()
{
   struct lnode *head,*p1,*p2;  
   int n,i;
   printf("请输入结点个数:");
   scanf("%d",&i);
   head=p2=NULL;
   for(n=1;n<=i;n++)
   {
      p1=(struct lnode*)malloc(sizeof(struct lnode));
      printf("请输入第%d个结点值:",n);
      scanf("%c",&p1->data);
      if(n==1) head=p1;
      else  p2->next=p1;
      p2=p1;      
    }
   p2->next=NULL;   
   return(head);
}


linklist Demo(linklist L)
{listnode *Q,*p;
if(L&&L->next)
{
    Q=L;L->next;p=L;
    while(p->next)
        p=p->next;
    p->next=Q;Q->next=NULL;
}
return L;
}

void listpint(linklist L)
{
    linklist p;
    int i=0;
    p=L;
    while(p!=NULL)
    {i++;
    printf("第%d个元素是:",i);
    printf("%c\n",p->data);
    p=p->next;
    }
}


void main()
{
    linklist head;
    head=(linklist)malloc(sizeof(linklist));
    head=creat();
    listpint(head);
    printf("经过Demo函数处理后:");
    head=Demo(head);
    listpint(head);
}
2012-03-10 17:44
daming1
Rank: 1
来 自:开平
等 级:新手上路
帖 子:21
专家分:0
注 册:2011-11-26
收藏
得分:0 
是scanf(" %c",&p1->data);哈哈,明白了,还是我不够细心
2012-03-10 19:20
快速回复:链表问题,检测通过,但运行有问题,求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012469 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved