标题:求解,指针,链表
取消只看楼主
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
结帖率:100%
已结贴  问题点数:20 回复次数:2 
求解,指针,链表
#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: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
得分:0 
回复 2楼 瓦力
虽然有点晚了,但还是谢谢
2012-03-19 22:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-363172-1-1.html




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

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