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 1018 Communication System
点击打开链接 Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21007 Acc ...
- iOS中FMDB的使用【单例】
DYDB.h Objective-C 12345678910111213141516 #import <Foundation/Foundation.h> #import <FMDB/ ...
- html 去掉input 获取焦点时的边框
html中,当input标签获取焦点的时候(例如,当光标放在input框中准备输入值时), input标签外围会出现边框,有的时候我们需要去掉这个边框,可以使用css的outline:none;属性将 ...
- leetcode 349:两个数组的交集I
Problem: Given two arrays, write a function to compute their intersection. 中文:已知两个数组,写一个函数来计算它们的交集 E ...
- python-appium练习编写脚本时遇到问题
遇到问题: 1.安卓4.2及以下系统无法识别resource-id属性 只能用text属性识别 2.输入中文无法识别 脚本最顶部增加#coding=utf-8 3.对象无法识别resource-id属 ...
- C#如何在事件中获得GridView里面TextBox的值
GridView设置如下: <asp:GridView ID="GridViewlb" runat="server" AutoGenerateColumn ...
- json 和 pickel 详解
一.json json,用于字符串 和 python数据类型间进行转换 Json模块提供了四个功能:dumps.dump.loads.load 1.json.loads()用于将字符串形式的字典,列表 ...
- CENTOS7设置显示中文
vi /etc/locale.conf LANG="zh_CN.UTF-8" LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN&quo ...
- Java导出数据为EXCEL的两种方式JXL和POI
JXL和POI导出数据方式的比较 POI支持excel2003和2007,而jxl只支持excel2003. 下面为测试代码: public class TestCondition { /** * 生 ...
- javascript代码复用模式(三)
前面谈到了javascript的类式继承.这篇继续部分类式继承,及一些现代继承. 类式继承模式-代理构造函数 这种模式通过断开父对象与子对象之间原型之间的直接链接关系,来解决上次说到的共享一个原型所带 ...