| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 494 人关注过本帖
标题:请教
只看楼主 加入收藏
s_k_y
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2008-2-2
结帖率:44.44%
收藏
 问题点数:0 回复次数:2 
请教
问题一:这是创建一个单链表,并往里边输入值并打印出这个链表的数值,问题是再输入时结束不了,不知问题出在哪里了?
请大家帮忙看一看。谢谢了。
#include "stdlib.h"
#include "malloc.h"
typedef struct linknode
{
  int data;
  struct linknode *next;   
   
}node;

node *creat(void)
{
  node *head,*p,*n;
  int x,y;
  y=1;
  head=(node *)malloc(sizeof(node));
  p=head;
  while(y)
  {
    scanf("%d",x);   
      if(x!=0)
       {
         n=(node *)malloc(sizeof(node));
         n->data=x;
         p->next=n;
         p=n;   
       }
      else
          y=0;
  }   
    p->next=NULL;
    return head;
}

void print(node *head)
{
   node *p;

   p=head;

   if(head==NULL)
      {
           printf("the list is null!!");     
      }
    else
        {
            printf("print the numbers: ");
            while(p->next!=NULL)
            {
                printf(" %d",p->data);
                p=p->next;
               
            }
            
        }

}

int main(void)
{ node *head;
 creat();
 print(head);

 return 0;   
}
问题二:这样可以建立空链表吗?
struct node
{
    int data;
    struct node *next;
   
};



struct node *create_list()
{
   
  struct node *head;
  head=(struct node*)malloc(sizeof(struct node));   
    if(head!=NULL)
    {
      printf("成功建立");
      head->next=NULL;   
    }
    else
        {
         printf("建立失败");
        }
            return head;
}
搜索更多相关主题的帖子: include 
2008-10-06 20:33
learnerboy
Rank: 2
等 级:论坛游民
帖 子:246
专家分:22
注 册:2007-11-11
收藏
得分:0 
/*  HELLO.C -- Hello, world */

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "malloc.h"
typedef struct linknode
{
  int data;
  struct linknode *next;   
   
}node;

node *creat(void)
{
  node *head,*p,*n;
  int x,y;
  y=1;
  head=(node *)malloc(sizeof(node));
  p=head;
  while(y)
  {
    scanf("%d",&x);
      if(x!=0)
       {
         n=(node *)malloc(sizeof(node));
         n->data=x;
         p->next=n;
         p=n;   
       }
      else
          y=0;
  }   
    p->next=NULL;
    return head;
}

void print(node *head)
{
   node *p;

   p=head->next;

   if(p==NULL)
      {
           printf("the list is null!!");     
      }
    else
        {
            printf("print the numbers: ");
            while(p!=NULL)
            {
                printf("  %d",p->data);
                p=p->next;
            }
            
        }

}

int main(void)
{
    node *head;
    head=creat();
    print(head);
    getch();
    return 0;   
}
2008-10-06 21:24
s_k_y
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2008-2-2
收藏
得分:0 
谢谢
这样可以建立空链表吗?这样写有问题吗?
struct node
{
    int data;
    struct node *next;
   
};



struct node *create_list()
{
   
  struct node *head;
  head=(struct node*)malloc(sizeof(struct node));   
    if(head!=NULL)
    {
      printf("成功建立");
      head->next=NULL;   
    }
    else
        {
         printf("建立失败");
        }
            return head;
}
2008-10-06 23:05
快速回复:请教
数据加载中...
 
   



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

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