打印

C语言程序关于单项链表的问题

[复制链接]
1263|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
q672730275|  楼主 | 2015-2-16 19:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 q672730275 于 2015-2-19 19:04 编辑

/*关于单项链表的问题*/

#include<stdio.h>                                       //建立一个单项链表的学生信息
#include<stdlib.h>
struct stu                                            //学生信息的结构体
{int num;
char name[20];
float score;
struct stu *next;
};
void main()                                //主函数
{
                struct stu *creat();                                    //建立链表的函数
                struct stu *delet(struct stu *head,int key);//删除链表的某一节点
                struct stu *insert(struct stu *head,struct stu *s);//插入某一节点的函数         
                void  output(struct stu *);                                  //输出链表的信息
                struct stu *head,*s;
                int key;
                printf("建立学生档案\n");
                head=creat();
                output(head);printf("\n");
                printf("删除学生档案\n");
            scanf("%d",&key);
                head=delet(head,key);
                output(head);
                printf("\n");
                printf("插入学生成绩\n");
                s=(struct stu*)malloc(sizeof(struct stu));
                scanf("%d%s%f",&s->num,s->name,&s->score);
                head=insert(head,s);
                output(head);
}

struct stu *creat()                    //创建链表的函数体
{               
            struct stu  *head,*last,*p;
                int num;
                head=last=NULL;
                printf("please input num name score:\n");
                scanf("%d",&num);
                while(num>0)
                {               
                            p=(struct stu*) malloc(sizeof(struct stu));
                                p->num=num;p->next=NULL;
                                scanf("%s%f",p->name,&p->score);
                                if(head==NULL)
                                  head=p;
                                else
                  last->next=p;
                                last=p;
                                printf("please int num name:\n");
                                scanf("%d",&num);
                }
                return(head);
}   



void  output(struct stu * head)                                         //输出链表的函数体
{      
            struct stu *p;
                if(head!=NULL)                                  //判断是否为空链表
                {
                            printf ("学生记录为:");            
                                p=head;
                                while(p!=NULL)                     //输出学生信息
                                                {
                                                        printf("学号: %d 姓名: %s  成绩:%.2f \n",p->num,p->name,p->score);
                                                    p=p->next;
                                                }
                }
                else printf("空链表");
}


struct stu * delet(struct stu *head,int key)                   //删除链表的函数体
{

                struct stu *p1,*p2;                     //建立两个结构体指针
                if(head!=NULL)                              //判断是否为空链表
                {
                    p1=head;                                    
                                        while(p1->num!=key&&p1->next!=NULL)           //查找要删除的节点的位置
                        {
                                {
                                                                        p2=p1;                           
                                                                        p1=p1->next;
                                }
                                                               
                                                                        if(p1->num==key)               //判断节点位置的情况(调试程序是这里出了问题,其他都没问题,希望各位帮忙看一下)
                                                                        {
                                                                                if(p1==head)               //判断节点是否为链表头
                                             head=p1->next;
                                                                                else
                                                                                    p2->next=p1->next;             //进行节点的删除
                                                                                free(p1);                               //释放内存

                                                                        }
                                                                        else
                                                                                printf("没有找到要删除的点\n");
                                                               
                        }   
                }
                else printf("空链表!\n");
                return head;
}



struct stu *insert(struct stu *head,struct stu *s)                     //插入节点的函数体
{
struct stu *p1,*p2;
p1=head;
if(p1==NULL)
{head=s;head->next=NULL;}
else
{   while((s->num>p1->num)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;}
if(s->num<=p1->num)
{if(head==p1)
head=s;
else
p2->next=s;
s->next=p1;
}
else{p1->next=s;
s->next=NULL;
}

}
return head;
}


相关帖子

沙发
q672730275|  楼主 | 2015-2-16 19:19 | 只看该作者
求高手看看哪里出了问题。。

使用特权

评论回复
板凳
NE5532| | 2015-2-16 22:26 | 只看该作者
想干什么,遇到啥问题,一概不说。程序不写注释,改正态度就有希望了。

使用特权

评论回复
地板
加油吧小鱼儿| | 2015-2-17 11:57 | 只看该作者
祝楼主好运!

使用特权

评论回复
5
q672730275|  楼主 | 2015-2-19 19:08 | 只看该作者
NE5532 发表于 2015-2-16 22:26
想干什么,遇到啥问题,一概不说。程序不写注释,改正态度就有希望了。

刚刚改了下

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

9

帖子

0

粉丝