| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 519 人关注过本帖
标题:链表的基本操作
只看楼主 加入收藏
松涛雨露
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2011-4-18
结帖率:50%
收藏
 问题点数:0 回复次数:0 
链表的基本操作
请哪位高手帮我修改一下,有几个错误,我实在找不到,谢谢!
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)//求结构体的长度,sizeof为“求字节数运算符”
#define NULL 0
struct student{                   //用结构体定义结点
    long num;
    float score;
    struct student * next;
};
int n;//定义一个全局变量n
struct student *creat ();//创建链表函数的声明,此函数的返回值是一个指针,带回一个指向链表头的指针
void printf  (struct student *head);//输出链表函数
struct student del(struct student * head ,long num);//删除函数
struct student * insert (struct student *head,struct student *stud);//插入函数
int main ()
{
    struct student *Head,stu;
    long Num;
    printf ("建立一个链表:\n");
    Head= creat();//创建链表的函数调用
    printf (Head);//调用输出函数
    printf ("\n输入要删除的学号:");
    scanf("%ld",&Num);//输入要删除的学号
    Head=del(Head,Num);//调用删除函数
    printf(Head);//调用输出函数
    printf("\n输入要插入的结点:");
    scanf("%ld,%f",&stu.num,&stu.score);//输入要插入的结点
    Head=insert(Head,&stu);//调用插入函数
    printf(Head);//调用输出函数
    return 0;
}
struct student *creat ()//创建链表函数的定义,此函数的返回值是一个指针,带回一个指向链表头的指针
{
    struct student *head,*p1,*p2;
    n=0;
    p1=p2=(struct student *)malloc(LEN);//动态的开辟一个新单元
    scanf("%ld,%f",&p1->num,&p1->score);//输入数据
    head=NULL;
    while(p1->num!=0)
    {
        n+=1;
        if(n==1)
            head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);//动态的开辟一个新单元
        scanf("%ld,%f",&p1->num,&p1->score);//输入数据
    }
    p2->next=NULL;
    return head;
}
void printf  (struct student *head)//输出链表函数的定义
{
    struct student *p;
    printf ("\n输出%d个数据:\n",n);
    p=head;
    if(head!=NULL)
    do{
        printf("\n%ld %5.1f\n",p->num,p->score);
        p=p->next;
    }while (p!=NULL);
}
struct student del (struct student *head ,long num)//删除函数的定义
{
    struct student *p1,*p2;
    if (head==NULL)
   
        printf ("\n此链表为空!\n");
        
   
    p1=head;
    while (num!=p1->num && p1->next!= NULL)//p1指向的不是所要找的结点,并且后面还有结点
    {
        p2=p1;
        p1=p1->next;//p2指向p1的当前位置,p1后一一个结点
    }
    if(num==p1->num)//找到了要删除的数
    {
        if(p1==head)
            head=p1->next;//若p1指向的是首结点,把第二个结点的地址赋给head
        else
            p2->next=p1->next;//否则将下一个结点的地赋给前一个结点的地址
        printf("删除%ld\n",num);
        n-=1;//结点个数减一
    }
    else printf ("没有找到要删除的数据%ld\n",num);//否则就没有找到结点

    free (p1);
    return head;
}
struct student * insert (struct student *head,struct student *stud)//插入函数的定义
{
struct student *p0,*p1,*p2;
p1=head;//使p1指向第一个结点
p0=stud;//p0指向要插入的结点
if (head==NULL)//如果原来的节点为空,则p0指向的结点作为头结点
{
    head=p0;
    p0->next=NULL;
}
else {
    while ((p0->num > p1->num) && (p1->next!=NULL))
    {
        p2=p1;//使p2指向当前p1的指向的结点
        p1=p1->next;// p1后一一个位置
    }
    if(p0->num <= p1->num)//找到了要插入的位置
    {
        if (head == p1) head=p0;//插入到原来第一个结点之前
        else p2->next=p0;//插入到p2指向的结点之后
        p0->next=p1;
    }
    else{                                 //插入到最后一个结点之后
        p1->next=p0;
        p0->next=NULL;
    }
}
n+=1;                  //结点数加一
return head;
}
搜索更多相关主题的帖子: 结构体 
2011-04-20 23:50
快速回复:链表的基本操作
数据加载中...
 
   



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

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