C-链表的一些基本操作【创建-删除-打印-插入】
#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时结束循环
{
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时结束循环
{
head = insert(head,stu);//返回链表的头地址
print(head);
printf("\nInput the inserted number:");
stu = (struct Student*)malloc(LEN);
scanf("%ld %f",&stu->num,&stu->score);
}
return ;
}
//建立链表的函数
struct Student* create()
{
struct Student *head;
struct Student *p1,*p2;
n=;
p1=p2=(struct Student *)malloc(LEN);
scanf("%ld %f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=)
{
n++;
if(n==)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-;
}
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+;//记录数加一
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);
}
注:CodeBlocks上调试通过。
C-链表的一些基本操作【创建-删除-打印-插入】的更多相关文章
- Python实现单链表数据的添加、删除、插入操作
Python实现单链表数据的添加.删除.插入操作 链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结 ...
- 谭浩强C语言第四版第九章课后习题7--9题(建立,输出,删除,插入链表处理)
#include<stdio.h> #include<stdlib.h> #define N sizeof(link) typedef struct stu { struct ...
- 二叉排序树(BST)创建,删除,查找操作
binary search tree,中文翻译为二叉搜索树.二叉查找树或者二叉排序树.简称为BST 一:二叉搜索树的定义 他的定义与树的定义是类似的,也是一个递归的定义: 1.要么是一棵空树 2.如果 ...
- 在后台对GameObject进行"创建"||"删除"动作
在后台对GameObject进行"创建"||"删除"动作 建立 public GameObject Pre;//在编辑器中用来绑定的Prefabs public ...
- Linux基础命令---lprm删除打印任务
lprm lprm指令用来删除当前打印队列上的任务,如果没有指定,那么就删除当前打印任务.您可以指定一个或多个职务ID编号来取消这些职务,或者使用选项”-”取消所有作业. 此命令的适用范围:RedHa ...
- C语言实现单链表的遍历,逆序,插入,删除
单链表的遍历,逆序,插入,删除 #include<stdio.h> #include<stdlib.h> #include <string.h> #define b ...
- SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
- hive 学习系列二(数据库的创建删除修改) 拿走,不谢。
database 相当于一个目录或者命名空间,用来更好地进行表的管理 在hdfs 的目录位置大致如下: [root@iZbp12vtv76y9q3d633bh6Z /]# hadoop fs -ls ...
- Oracle 删除用户和表空间////Oracle创建删除用户、角色、表空间、导入导出、...命令总结/////Oracle数据库创建表空间及为用户指定表空间
Oracle 使用时间长了, 新增了许多user 和tablespace. 需要清理一下 对于单个user和tablespace 来说, 可以使用如下命令来完成. 步骤一: 删除user drop ...
随机推荐
- POJ 3308 Paratroopers(最小割EK(邻接表&矩阵))
Description It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the ...
- JS浮点数运算BUG破法
JS里,0.3*3 = 0.8999999999999999 破法1:((0.3*10)*3)/10 破法2:(0.3*3).toFixed(2)//保留两位小数 原因:js浮点数用的是IEEE754 ...
- CPPUTest 单元测试框架(针对 C 单元测试的使用说明)
CPPUTest 虽然名称上看起来是 C++ 的单元测试框架, 其实它也是支持测试 C 代码的. 本文主要介绍用CPPUTest来测试 C 代码. (C++没用过, 平时主要用的是C) C++相关的内 ...
- FusionCharts for Flex报错
1.昨天,我新建了一个Flex项目,并将FusionCharts组件添加进去了,但是Flex应用程序还是报错 具体错误如下: 2.但是,Flex项目已经导入FusionCharts.swc
- 洛谷P2727 01串 Stringsobits
P2727 01串 Stringsobits 24通过 55提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 这题的思路是啥啊!!!跪求- 题目背景 考虑 ...
- 复习下 AJAX
什么是AJAx:Asynchronous Javascript and XML中文意思:异步JavaScript 和XML批一种创建交互式网页应用的网页开发技术.AJAX优点1.Ajax 在本质上是一 ...
- hdu 3336【Count the string】(KMP)
一道字符串匹配的题目,仅仅借此题练习一下KMP 因为这道题目就是要求用从头开始的n个字符串去匹配原来的字符串,很明显与KMP中求next的过程很相似,所以只要把能够从头开始匹配一定个数的字符串的个数加 ...
- 详解Ossim 4.3控制台
在Ossim 4.2以后的发行版中增加了控制台功能下面加以详细说明,当Ossim 启动之后,在控制台以root身份登录系统后会执行ossim-setup脚本,然后显示如下图1所示,了解这些功能,以及操 ...
- 【ITOO 1】将List数据导出Excel表
需求描述:在课表导入的时候,首先给用户提供模板(excel),然后将用户填写好的数据读取到list集合中.再进行判空处赋值处理,以及去重处理.这篇博客,主要介绍读取excel表和导出excel表的方法 ...
- 问 如何使用css将select的边框以及右边的小三角形去掉?
最好css2,css3都给出解决方案,效果如下: CSS2 只能使用div和ul进行模拟了,结构很简单,具体可参考Alice的 button-dropdownCSS3 可以使用CSS3的属性appea ...