《啊哈,算法》第二章栈、队列、链表第四节链表代码优化
题目:
第一行给出一个整数n,第二行给出n个已经从小到大排好的数,第三行给出需要往这串数中插入的数a,输出仍符合从小到大排列的新的序列。
输入样例:
9
2 3 5 8 9 10 18 26 32
6
输出样例:
2 3 5 6 8 9 10 18 26 32
书中给的代码:
#include <stdio.h>
#include <stdlib.h>
//这里创建一个结构体用来表示链表的结点类型
struct node {
int data;
struct node *next;
};
int main() {
struct node *head,*p,*q,*t;
int i,n,a;
scanf("%d",&n);
head =NULL;//头指针初始为空
for(i =1;i <=n;i++) { //循环读入n个数
scanf("%d",&a); //动态申请一个空间,用来存放一个结点,并用临时指针p指向这个结点
p=(struct node *)malloc(sizeof(struct node));
p->data =a;//将数据存储到当前结点的data域中
p->next =NULL;//设置当前结点的后继指针指向空,也就是当前结点的下一个结点为空
if(head ==NULL) {
head =p;//如果这是第一个创建的结点,则将头指针指向这个结点
}
else{
q->next =p;//如果不是第一个创建的结点,则将上一个结点的后继指针指向当前结点
}
q =p;//指针q也指向当前结点
}
scanf("%d",&a);//读入待插入的数
t =head;//从链表头部开始遍历
while(t !=NULL){ //当没有到达链表尾部的时候循环
if(t->next ==NULL ||t->next->data >a){ //如果当前结点是最后一个节点或者下一个结点的值大于待插入数的时候插入
p =(struct node *)malloc(sizeof(struct node));//动态申请一个空间,用来存放新增结点
p->data =a;
p->next =t->next;//新增结点的后继指针指向当前结点的后继指针所指向的结点
t->next =p;//当前结点的后继指针指向新增结点
break;//插入完毕退出循环
}
t =t->next;//继续下一个结点
}
//输出链表中的所有数
t =head;
while(t !=NULL){
printf("%d ",t->data);
t =t->next;//继续下一个结点
}
getchar();getchar();
return 0;
}
在我把书中的代码敲了敲后,发现该代码有一点问题,在这里我做了一些修改。原代码不足之处:
没有考虑输入的数字为最小值的情况。
比如:
9
2 3 5 8 9 10 18 26 32
1
输出结果为:
因为在执行if判断时是从当前结点的下一结点的值开始比较的,所以数列中第一个结点的数值没有与a进行比较大小,所以该情况下原码是不足以实现的。
我的代码:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
int main(){
struct node *head,*p,*q;
int i,n,a;
scanf("%d",&n);
head =NULL;
for(i =1;i <=n;i++){
scanf("%d",&a);
p =(struct node*)malloc(sizeof(struct node));
p->data =a;
p->next =NULL;
if(head ==NULL){
head =p;
}
else{
q->next =p;
}
q =p;
}
scanf("%d",&a);
q =head;
if(q->data >a){
p =(struct node*)malloc(sizeof(struct node));
p->data =a;
p->next =q;
head =p;
}
else{
while(q !=NULL){
if(q->next ==NULL ||q->next->data>a){
p =(struct node*)malloc(sizeof(struct node));
p->data =a;
p->next =q->next;
q->next =p;
break;
}
q =q->next;
}
}
q =head;
printf("插入后的数列为");
while(q !=NULL){
printf("%d ",q->data);
q =q->next;
}
getchar();getchar();
return 0;
}
在进入while循环之前首先对第一个结点的值先进行一次比较即可满足输入值为最小的情况。
输入:
9
2 3 5 8 9 10 18 26 32
6
输出:
输入:
9
2 3 5 8 9 10 18 26 32
1
输出:
输入:
9
2 3 5 8 9 10 18 26 32
88
输出: