| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 328 人关注过本帖
标题:链表的输入与输出
只看楼主 加入收藏
我勒个去
Rank: 2
等 级:论坛游民
帖 子:21
专家分:20
注 册:2012-3-14
结帖率:83.33%
收藏
已结贴  问题点数:10 回复次数:3 
链表的输入与输出
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct number)
struct number
{
    int num;
    struct number  *next;
};
int n;
struct number *creat(int k)
{
    struct number *head,*p1,*p2;
    n=0;
    p1=p2=(struct number *)malloc(LEN);
    scanf("%d ",&p1->num);
    head=NULL;
    while(n<k-1)
    {
        n++;
        if(n==1)head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct number *)malloc(LEN);
        scanf(" %d",&p1->num);
    }
    p2->next=NULL;
    return(head);
}
void print(struct number *head,int k)
{
    struct number *p;
    n=0;
    p=head;
    if(head!=NULL)
        do
        {
            printf("%d ",p->num);
            p=p->next;
        }while(p!=NULL);
}
int main()
{
    int N,M;
    struct number *pt1,*pt2;
    scanf("%d",&N);
    pt1=creat(N);
    scanf("%d",&M);
    pt2=creat(M);
    print(pt1,N);
    print(pt2,M);
    printf("\n");
    return 0;
}
咋结果为:
输入:
5
1 2 3 4 5
6
11 22 33 44 55 66
输出:
1 2 3 4 11 22 33 44 55
咋是5和66没输出啊!!
搜索更多相关主题的帖子: head next include number return 
2012-05-03 18:24
ln6265431
Rank: 5Rank: 5
等 级:职业侠客
帖 子:66
专家分:325
注 册:2012-3-29
收藏
得分:5 
struct number *creat(int k)


{

   
struct number *head,*p1,*p2;

  
  n=0;

   
 p1=p2=(struct number*)malloc(LEN);

    scanf("%d ",&p1->num);

  
  head=NULL;

   
 while(n<k)

   
 {

        
n++;

      
  if(n==1)head=p1;

   
    else p2->next=p1;

     
   p2=p1;

   
    p1=(struct number*)malloc(LEN);

        scanf(" %d",&p1->num);

  
 }

   
p2->next=NULL;

   return(head);


}
2012-05-03 19:58
我勒个去
Rank: 2
等 级:论坛游民
帖 子:21
专家分:20
注 册:2012-3-14
收藏
得分:0 
k-1改为k的话输入有多了一组
2012-05-03 20:50
巴克
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:93
专家分:199
注 册:2012-2-8
收藏
得分:5 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct number)
struct number
{
    int num;
    struct number  *next;
};
int n;
struct number *creat(int k)
{
    struct number *head,*p1,*p2;
    n=0;
    p1=p2=(struct number *)malloc(LEN);
    scanf("%d",&p1->num);
    head=NULL;
    while(n<k-1)
    {
        n++;
        if(n==1)head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct number *)malloc(LEN);
        scanf("%d",&p1->num);
    }
    /*关键在下面两行*/
    p2->next = p1;
    p2 = p1;
    /*在上面的循环中最后一个输入数据没有加入到链表中(lz自己体会)*/
    p2->next=NULL;
    return(head);
}
void print(struct number *head,int k)
{
    struct number *p;
    n=0;
    p=head;
    if(head!=NULL)
        do
        {
            printf("%d ",p->num);
            p=p->next;
        }while(p!=NULL);
}
int main()
{
    int N,M;
    struct number *pt1,*pt2;
    
    scanf("%d",&N);
    pt1=creat(N);
    
    print(pt1,N);
    printf("\n");
    
    scanf("%d",&M);
    pt2=creat(M);
    
    print(pt2,M);
    printf("\n");
    return 0;
}
2012-05-04 07:51
快速回复:链表的输入与输出
数据加载中...
 
   



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

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