| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 564 人关注过本帖
标题:求解,指针,链表
只看楼主 加入收藏
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
求解,指针,链表
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu{
    int num;
    char name[20];
    struct stu * next;
};
struct stu * creat();
struct stu * show(struct stu * head);
int main(void)
{
    int choice;
    struct stu * head = NULL;

    printf("1:creat link\n");
    printf("2:show link\n");
    printf("0:exit\n");
    printf("enter 0-2 !\n");
    scanf("%d",&choice);
    switch(choice){
    case 1:
        head = creat();
    case 2:
        show(head); break;
    case 0:
        break;
    }
    printf("input error!");

    return 0;
}
struct stu * creat()
{
    struct stu * head,* p,* tail;

    int num;
    char name[20];
    head = tail = NULL;

    int size = sizeof(struct stu);

    printf("input 0 exit!\n");
    printf("inout num and name:\n");
    scanf("%d%s",&num,name);
    do{
        p = (struct stu *)malloc(size);
        p->num = num;
        strcpy(p->name,name);
        if(head == NULL)
            head = p;
        else
            tail->next = p;
        tail = p;
        scanf("%d",&num);
        scanf("%s",name);
    }while(num != 0);

    return head;
}
struct stu * show(struct stu * head)
{
    struct stu * ptr;
   
    printf("输出链表:");
    if(head == NULL){
        printf("NULL!\n");
        return 0;
    }
    for(ptr = head;ptr;ptr = ptr->next)
        printf("%d   %s\n",ptr->num,ptr->name);
    return 0;
}
[local]1[/local]


发表文章 - 『 C语言论坛 』 - 编程论坛.png (178.1 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: head next include choice 
2012-03-14 17:41
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
收藏
得分:0 
帮忙a
2012-03-14 21:49
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
收藏
得分:20 
主要问题是链表没有尾巴,下面代码修改了一下,供您参考下:
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu{
    int num;
    char name[20];
    struct stu * next;
};
struct stu * creat();
struct stu * show(struct stu * head);
int main(void)
{
    int choice;
    struct stu * head = NULL;

    printf("1:creat link\n");
    printf("2:show link\n");
    printf("0:exit\n");
    printf("enter 0-2 !\n");
    scanf("%d",&choice);
    switch(choice){
    case 1:
        head = creat();
    case 2:
        show(head); break;
    case 0:
        break;
    default:
        printf("input error!");
    }

    return 0;
}
struct stu * creat()
{
    struct stu * head,* p,* tail;

    int num;
    char name[20];
    head = tail = NULL;

    int size = sizeof(struct stu);

    printf("input 0 exit!\n");
    printf("inout num and name:\n");
    scanf("%d %s",&num,name);
    do{
        p = (struct stu *)malloc(size);
        p->num = num;
        strcpy(p->name,name);
        if(head == NULL)
            head = p;
        else
            tail->next = p;
        tail = p;
        p->next=NULL;
        scanf("%d",&num);
        scanf("%s",name);
    }while(num != 0);

    return head;
}
struct stu * show(struct stu * head)
{
    struct stu * ptr;

    printf("输出链表:\n");
    if(head == NULL){
        printf("NULL!\n");
        return 0;
    }
    for(ptr = head;ptr;ptr = ptr->next)
        printf("%d   %s\n",ptr->num,ptr->name);
    return 0;
}

2012-03-15 01:44
喝醉的猫
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:68
专家分:120
注 册:2012-3-13
收藏
得分:0 
新手来学习,希望多多帮助
2012-03-15 12:32
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
收藏
得分:0 
回复 2楼 瓦力
虽然有点晚了,但还是谢谢
2012-03-19 22:45
快速回复:求解,指针,链表
数据加载中...
 
   



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

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