c语言链表基本操作(带有创建链表 删除 打印 插入)

所属分类: 软件编程 / C 语言 阅读数: 65
收藏 0 赞 0 分享

复制代码 代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct Student)
struct Student
{
    long num;
    float score;
    struct Student*next;
};
int n;
int main()
{
    /*-----------------------------程序描述--------------------------------------------
        题目:写出一个主函数,分别调用建立链表的函数create(),输出链表的函数print(),
              删除链表结点的函数del(),插入结点的函数insert(),一共5个函数。
        Author:KillerLegend
        Date:  2013.12.6
    ----------------------------------------------------------------------------------*/
//函数声明
    struct Student* create();//创建动态链表的函数声明,函数类型为student结构体类型,返回头指针
    struct Student* del(struct  Student* ,long);//删除指定位置结点的函数声明,参数:链表头结点+删除结点位置+返回头指针
    struct Student* insert(struct Student*,struct Student*);//插入一个Student类型数据的函数声明
    void   print(struct Student*);//输出链表中数据的函数声明,参数为链表的头指针
//定义变量
    struct Student *head,*stu;//定义动态链表的头指针与新的结点
    long del_num;
//建立链表操作
    printf("Input records:\n");
    head = create();//建立链表并返回头指针
    print(head);//输出全部结点

//删除结点操作
    printf("\nInput the deleted number:");
    scanf("%ld",&del_num);
    while(del_num!=0)//当输入学号为0时结束循环
    {
        head = del(head,del_num);//删除结点后返回链表的头地址
        print(head);//输出全部结点
        printf("Input the deleted number:");
        scanf("%ld",&del_num);
    }
//插入结点操作
    printf("\nInput the inserted number:");
    stu=(struct Student*)malloc(LEN);//每插入一个结点需要开辟一个新的结点
    scanf("%ld %f",&stu->num,&stu->score);
    while(stu->num!=0)//当输入的学号为0时结束循环
    {
        head = insert(head,stu);//返回链表的头地址
        print(head);
        printf("\nInput the inserted number:");
        stu = (struct Student*)malloc(LEN);
        scanf("%ld %f",&stu->num,&stu->score);
    }
    return 0;
}
//建立链表的函数
struct  Student* create()
{
    struct Student *head;
    struct Student *p1,*p2;
    n=0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%ld %f",&p1->num,&p1->score);
    head=NULL;
    while(p1->num!=0)
    {
        n++;
        if(n==1)head=p1;
        else p2->next=p1;//第一次执行时,这一步是将头指针指向自身,当n从2起,这一步用于使p2指向下一个元素
        p2=p1;//使p2和p1指向同一个存储区
        p1=(struct Student*)malloc(LEN);//开辟动态存储区,强制返回struct Student类型的指针
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next=NULL;
    return (head);
}

//删除结点的函数
struct Student* del(struct  Student* head,long num)
{
    struct Student *p1,*p2;
    if(head==NULL)
    {
        printf("List null!\n");
        return (head);
    }
    p1=head;
    while(num!=p1->num && p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(num==p1->num)
    {
        if(p1==head)
        {
            head=p1->next;
        }
        else
        {
            p2->next=p1->next;
        }
        printf("Delete:%ld\n",num);
        n=n-1;
    }
    else
    {
        printf("%ld not been found!",num);
    }
    return (head);
}

//插入结点的函数
struct   Student* insert(struct Student* head,struct Student * stud)
{
    struct Student *p0,*p1,*p2;
    p1=head;
    p0=stud;
    if(head==NULL)//原来的链表是空表
    {
        head=p0;p0->next=NULL;//空表时使插入的结点作为头结点
    }
    else//如果不是空表,则遍历寻找合适的插入位置
    {
        while((p0->num>p1->num)&&(p1->next!=NULL))//按学号顺序插入,如果插入的学号数字比较大,则应该向后推移
        {
            p2=p1;
            p1=p1->next;//后移
        }
    }
    if(p0->num<=p1->num)//找到插入的位置,插入的位置是p1所指向的位置之前,也就是p2指向的位置
    {
        if(head==p1)head=p0;//如果插入的位置是头位置之前,则使head指向p0
        else p2->next=p0;//如果不是头位置之前,则使p2的next指针指向插入的数据地址即p0
        p0->next=p1;//使p0的next指针指向p1,完成了数据的加入
    }
    else//插入的学号位置在最后一个
    {
        p1->next=p0;
        p0->next=NULL;
    }
    n=n+1;//记录数加一
    return(head);
}
//输出链表的函数
void print(struct Student * head)
{
    struct Student * p;
    printf("Now,These %d records are:\n",n);
    p=head;
    if(head!=NULL)
    do
    {
        printf("%ld %5.1f\n",p->num,p->score);
        p=p->next;
    }while(p!=NULL);
}

更多精彩内容其他人还在看

C++广播通信实例

这篇文章主要介绍了C++实现广播通信的方法,实例讲述了C++ socket广播通信的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C++计算ICMP头的校验和实例

这篇文章主要介绍了C++计算ICMP头的校验和的方法,代码简单实用,对于校验ICMP报文来说有不错的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++设置超时时间的简单实现方法

这篇文章主要介绍了C++设置超时时间的简单实现方法,涉及系统函数setsockopt对套接口的操作,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现ping程序实例

这篇文章主要介绍了C++实现ping程序实例,涉及C++对于ICMP数据包的发送与回显处理,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之boost::array的用法

这篇文章主要介绍了C++之boost::array的用法,以实例的形式简单讲述了静态数组的容器boost::array的使用技巧,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之Boost::array用法简介

这篇文章主要介绍了C++之Boost::array用法简介,较为详细的分析了Boost::array中的常见用法,并用实例的形式予以总结归纳,需要的朋友可以参考下
收藏 0 赞 0 分享

VC文件目录常见操作实例汇总

这篇文章主要介绍了VC文件目录常见操作实例汇总,总结了VC针对文件目录的各种常用操作,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

VC打印word,excel文本文件的方法

这篇文章主要介绍了VC打印word,excel文本文件的方法,是VC操作文本文件中非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC++获得当前进程运行目录的方法

这篇文章主要介绍了VC++获得当前进程运行目录的方法,可通过系统函数实现该功能,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC中SendMessage和PostMessage的区别

这篇文章主要介绍了VC中SendMessage和PostMessage的区别,较为全面的分析了SendMessage和PostMessage运行原理及用法上的不同之处,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多