#include<stdio.h>
#include<stdlib.h>
#define N sizeof(link)
typedef struct stu
{
struct stu *next;
int date;
}link;
void print(link*head)/*打印链表数据*/
{
link *p;
p=head->next;
if(p)
do
{
printf("%d\t",p->date);
p=p->next;
}while(p);
printf("\n");
}
link *creat(void)/*创建链表*/
{
link *head,*p,*s;
head=(link*)malloc(N);
head->next=p=(link*)malloc(N);
printf("请输入数据输入‘0’结束创建链表:\n");
scanf("%d",&p->date);
while(p->date)
{
p->next=(link*)malloc(N);
s=p;
p=p->next;
printf("请输入数据输入‘0’结束创建链表:\n");
scanf("%d",&p->date);
}
s->next=NULL;
return(head);
}
link *del(link *head,int m)/*删除指定数据节点*/
{
link *p,*last,*s;
last=head;
p=head->next;
while(p)
{
if(p->date==m)
{
last->next=p->next;
s=p;
p=p->next;
free(s);
}
else {
last=last->next;
p=p->next;
}
}
return head;
}
link *insert(link *head,int m)/*在m数据结点后插入结点并输入数据*/
{
link *p,*pnew;
for(p=head->next;p;p=p->next)
if(p->date==m)
{
pnew=(link*)malloc(N);
printf("请输入插入的数据:\n");
scanf("%d",&pnew->date);
pnew->next=p->next;
p->next=pnew;
}
printf("成功插入结点数据!\n");
return(head);
}
int main()
{
link*head;
int n;
head=creat();
printf("打印未处理的数据:\n");
print(head);
printf("\n输入要删除的数据结点:\n");
scanf("%d",&n);
printf("打印删除后的链表数据:\n");
print(del(head,n));
printf("\n输入在哪个链表数据后插入结点:\n");
scanf("%d",&n);
printf("打印插入后的链表数据:\n");
print(insert(head,n));
return ;
}

测试数据:

请输入数据输入‘0’结束创建链表:
1
请输入数据输入‘0’结束创建链表:
2
请输入数据输入‘0’结束创建链表:
3
请输入数据输入‘0’结束创建链表:
4
请输入数据输入‘0’结束创建链表:
5
请输入数据输入‘0’结束创建链表:
6
请输入数据输入‘0’结束创建链表:
7
请输入数据输入‘0’结束创建链表:
8
请输入数据输入‘0’结束创建链表:
9
请输入数据输入‘0’结束创建链表:
10
请输入数据输入‘0’结束创建链表:
0
打印未处理的数据:
1 2 3 4 5 6 7 8 9 10 输入要删除的数据结点:
7
打印删除后的链表数据:
1 2 3 4 5 6 8 9 10 输入在哪个链表数据后插入结点:
6
打印插入后的链表数据:
请输入插入的数据:
666666
成功插入结点数据!
1 2 3 4 5 6 666666 8 9 10
Press any key to continue

  

谭浩强C语言第四版第九章课后习题7--9题(建立,输出,删除,插入链表处理)的更多相关文章

  1. 谭浩强第四版第九章课后习题12>>>建立一个链表,每个节点包括:学号、姓名、性别、年龄。输入一个年龄,若链表 中的结点所包含的年龄等于此年龄,则删除此结点。

    #include<stdio.h> #include<stdlib.h> #define N sizeof(link) typedef struct lin { struct ...

  2. NEUQ1051: 谭浩强C语言(第三版)习题6.7

    //C代码简直难看到家,求大神知道如何写出复用性好的,维护性强的代码... //格式错误了好几次,最后发现是are和数字之间多了个空格......本来一直以为是最后的换行多了,费劲搞掉了. #incl ...

  3. NEUQ1055谭浩强C语言(第三版)习题6.11

    //迭代公式不是很理解,写出来算了.. #include <stdio.h> #include <math.h> int main() { double x0,x1; int ...

  4. NEUQ1038: 谭浩强C语言(第三版)习题4.8

    之前没做对的一道题,今天集中清理一下. //------------------- 很水的题,主要是 %.2lf 不能四舍五入,需要仅保留两位小数,用了丑陋的强制类型转换... //--------- ...

  5. 读谭浩强C语言数据结构有感(1)

    1.什么是数据结构? 数据结构,就是我们计算机内部的运算,编程语言的基础工作模式吧,个人总结的 = = !! 数据:说简单一点,就是计算机二进制机器码,然后通过一些复杂的操作,变为复杂的语言. 数据元 ...

  6. 谭浩强 c++程序设计第一章课后习题 第10题

    #include <iostream> using namespace std; int main() { int a,b,c; cout<<"请输入三个整数类型的数 ...

  7. 谭浩强 c++程序设计第一章课后习题 第7题

    #include <iostream> using namespace std; int main() { int a,b,c; int f(int x,int y,int z);//这是 ...

  8. java编程思想第四版第九章总结

    1. 策略设计模式 参考这篇文章:http://blog.csdn.net/chenjie19891104/article/details/6396458 讲的很清楚,策略设计模式.并且举了一个例子, ...

  9. java编程思想第四版第九章习题

    第三题 package net.mindview.interfaces; abstract class Base{ public Base(){ print(); } abstract void pr ...

随机推荐

  1. percona MySQL 5.7yum安装

    检查是否安装有MySQL Server: rpm -qa | grep mysql rpm -qa | grep mariadb 删除方法: rpm -e mysql #普通删除模式 rpm -e - ...

  2. win8中 用office 提示值不在预期的范围内

    原文:http://bbs.kafan.cn/thread-1401951-1-1.htmlhttp://bbs.kafan.cn/thread-1401951-1-1.html 错误如下: 名称:  ...

  3. python入门16 递归函数 高阶函数

    递归函数:函数内部调用自身.(要注意跳出条件,否则会死循环) 高阶函数:函数的参数包含函数 递归函数 #coding:utf-8 #/usr/bin/python """ ...

  4. 【转载】#458 Errors While Converting Between enum and Underlying Type

    You can convert to an enum value from its underlying type by casting the underlying type (e.g. int) ...

  5. auto create statistic script

    --生成用户创建的statistic脚本 select object_name(stout.object_id), stout.name, 'CREATE STATISTICS '+ stout.na ...

  6. 在Spring整合aspectj实现aop的两种方式

    -----------------------------基于XML配置方案目标对象接口1 public interface IUserService { public void add(); pub ...

  7. Java中获取classpath路径下的资源文件

    ClassLoader 提供了两个方法用于从装载的类路径中取得资源: public URL  getResource (String name); public InputStream  getRes ...

  8. 五、设置 IntelliJ IDEA 主题和字体的方法

    我们已经用 IntelliJ IDEA 创建了第一个 Java 项目 HelloWorld,如下图所示: 观察上图,大家有没有发现一些问题,例如,整个界面的字体是不是都太小了一点啊?不知道大家感受如何 ...

  9. 【luogu P1462 通往奥格瑞玛的道路】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1462 记住HP=0也叫死. #include <queue> #include <cstd ...

  10. 安装 centos7

    一.安装 省略前面安装操作,直接进入设置界面: 日期.键盘等设置按默认即可,主要是系统安装位置需要设置,点击 系统安装位置,进入设置 最终完成分区: 点击左上角完成按钮 接受更改 开始安装 设置roo ...