| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 483 人关注过本帖
标题:关于学生信息链表的操作,运行时
只看楼主 加入收藏
the_second
Rank: 2
等 级:论坛游民
帖 子:115
专家分:80
注 册:2015-9-13
结帖率:78.95%
收藏
已结贴  问题点数:20 回复次数:3 
关于学生信息链表的操作,运行时
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stud_node
{
    int num;
    char name[20];
    int score;
    struct stud_node *next;
};
struct stud_node *Create_Stu_Doc();
struct stud_node *InsertDoc(struct stud_node *head,struct stud_node *stud);
struct stud_node *DeleteDoc(struct stud_node *head,int num);
void Print_Stu_Doc(struct stud_node *head);

int main(void)
{
    struct stud_node *head,*p;
    int choice,num,score;
    char name[20];
    int size=sizeof(struct stud_node);
    do
    {
        printf("1:Create 2:Insert 3:Delete 4:Print 0:Exit\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                head =Create_Stu_Doc();
                break;
            case 2:
                printf("Input num,name and score:\n");
                scanf("%d%s%d",&num,name,&score);
                p=(struct stud_node *)malloc(size);
                p->num=num;
                strcpy(p->name,name);
                p->score=score;
                head=InsertDoc(head,p);
                break;
            case 3:
                printf("Input num:\n");
                scanf("%d",num);
                head=DeleteDoc(head,num);
                break;
            case 4:
                Print_Stu_Doc(head);
                break;
            case 0:
                break;
               
        }
    }while(choice !=0);
    return 0;
}


struct stud_node *Create_Stu_Doc()   
{
    struct stud_node *head,*p;
    int num,score;
    char name[20];
    int size=sizeof(struct stud_node);
    head=NULL;
    printf("Input num,name and score:");
    scanf("%d%s%d",&num,name,&score);
    while(num!=0)
    {
        p=(struct stud_node *)malloc(size);
        p->num=num;
        strcpy(p->name,name);
        p->score=score;
        head=InsertDoc(head,p);
        scanf("%d%s%d",&num,name,&score);
    }
    return head;   
}
struct stud_node *InsertDoc(struct stud_node *head,struct stud_node *stud)
{
    struct stud_node *ptr,*ptr1,*ptr2;
    ptr2=head;
    ptr=stud;
    if(head==NULL)
    {
        head=ptr;
        head->next=NULL;
    }
    else
    {
        while((ptr->num>ptr2->num)&&(ptr2->next!=NULL))
        {
            ptr1=ptr2;
            ptr2=ptr2->next;
        }
        if(ptr->num<=ptr->num)
        {
            if(head==ptr2)
                head=ptr;
            else
                ptr1->next=ptr;
            ptr->next=ptr2;
        }
        else
        {
            ptr2->next=ptr;
            ptr->next=NULL;
        }
    }
    return head;   
}

struct stud_node *DeleteDoc(struct stud_node *head,int num)
{
    struct stud_node *ptr1,*ptr2;
    while(head!=NULL&&head->num==num)
    {
        ptr2=head;
        head=head->next;
        free(ptr2);
    }
    if(head==NULL)
    {
        return NULL;
    }
    ptr1=head;
    ptr2=head->next;
    while(ptr2!=NULL)
    {
        if(ptr2->num==num)
        {
            ptr1->next=ptr2->next;
            free(ptr2);
        }
        else
        {
            ptr1=ptr2;
        }
        ptr2=ptr1->next;
    }
    return head;
}

void Print_Stu_Doc(struct stud_node *head)
{
    struct stud_node *ptr;
    if(head==NULL)
    {
        printf("\nNo Record\n");
        return;
    }
    printf("\nThe Students'Record Are:\n");
    printf("Num\t Name\t Score\n");
    for(ptr=head;ptr!=NULL;ptr=ptr->next)
    {
        printf("%d\t%s\t%d\n",ptr->num,ptr->name,ptr->score);
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: 信息 include choice 
2015-09-16 21:49
the_second
Rank: 2
等 级:论坛游民
帖 子:115
专家分:80
注 册:2015-9-13
收藏
得分:0 
不知道为什么会出现要多处输入choice才能到输入学生信息
2015-09-16 21:50
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:14 
你连scanf()的特性都没弄明白就写那么多代码上去?先把菜单调试好了再写别的吧,你的问题就集中在那里,一个一个弄,把所有代码都放上来,反而没人看你的。

授人以渔,不授人以鱼。
2015-09-16 22:11
the_second
Rank: 2
等 级:论坛游民
帖 子:115
专家分:80
注 册:2015-9-13
收藏
得分:0 
我最近在复习大一关于链表的,这是书上的内容
昨天刚打完运行了一下以为有错,今天调试的时候可以的
谢了
2015-09-17 10:21
快速回复:关于学生信息链表的操作,运行时
数据加载中...
 
   



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

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