早教吧 育儿知识 作业答案 考试题库 百科 知识分享

排序(用链表实现,用c实现)将一个杂乱无序的整数序列,按照从小到大的顺序排列并输出。用第二章的相关知识实现,即先初始化一个空的单链表,然后每读入一个数据,将该数据存入结

题目详情
排序(用链表实现,用c实现)
将一个杂乱无序的整数序列,按照从小到大的顺序排列并输出。
用第二章的相关知识实现,即先初始化一个空的单链表,然后每读入一个数据,将该数据存入结点并插入单链表,当然,插入时要从头结点开始向后(或从尾结点开始向前)搜索合适的插入位置,以保证插入后仍然有序。
输出时,从头结点开始向后,顺序输出各结点的值。
输入
测试数据不止一组,每组测试数据:
1)先输入无序序列的整数个数n;(n不超过10000)
2)然后连续输入n个整数;
若n的值输入为0值,则输入结束.
输出
与每组输入的测试数据相对应,输出其按从小到大排好序后的整数序列.
注意:每组输出占一行.
▼优质解答
答案和解析
本人书写并调试近2小时,已经调试通过;将下面代码分别保存为data.h data.c user.c
然后编译user.c data.c即可;
/*
* data.h
* */
#ifndef __DATA_H__
typedef struct node {
int id;
struct node *p_next;
} node, *ll_head;
ll_head ll_create(void); /* create a linked list */
node *ll_input(node *);
void ll_insert(ll_head, node *); /* insert a node to the linked list */
void ll_delete(ll_head, node *); /* delete a node from the linked list */
node *ll_locate(ll_head, int); /* return if the node exists */
void ll_free(ll_head); /* free memory */
void ll_traversal(ll_head); /* traversal the linked list */
#endif /* __DATA_H__*/
/*
* data.c
* */
#include
#include
#include "data.h"
/* create a linked list */
ll_head ll_create(void)
{
node *new = malloc(sizeof(struct node));
if (new == NULL) {
perror("malloc");
return NULL;
}
new->p_next = NULL;
//printf("create linked list successfully !");
return new;
}
/* insert a node to the linked list */
void ll_insert(ll_head head, node * tmp)
{
if (ll_locate(head, tmp->id)) {
printf("the id(%d) has exited", tmp->id);
return;
}
node *new = malloc(sizeof(struct node));
if (new == NULL) {
perror("malloc");
return;
}
new->id = tmp->id;
new->p_next = NULL;
node *this = head;
for (this = head; this; this = this->p_next) {
/* insert id to link list in order */
if (this->p_next == NULL) {
this->p_next = new;
break;
} else if (this->p_next->id > tmp->id) {
new->p_next = this->p_next;
this->p_next = new;
break;
}
}
}
/* delete a node from the linked list */
void ll_delete(ll_head head, node *tmp)
{
node *this = head;
node *fro = this;
int flag = 0;
for (; this;) {
fro = this;
this = this->p_next;
if (this->id == tmp->id) {
fro->p_next = this->p_next;
free(this);
this = fro;
flag = 1;
printf("delete id(%d)", tmp->id);
}
}
if (this == NULL && flag == 0) {
printf("the node not exists");
}
}
/* free the memory of the linkedlist */
void ll_free(ll_head head)
{
node *fro = head;
node *this = head;
for (; this->p_next;) {
fro = this;
this = this->p_next;
fro->p_next = this->p_next;
//printf("free(%02d,%02d) ", this->x, this->y);
free(this);
this = fro;
}
//printf("");
}
/* return if the node exists */
node *ll_locate(ll_head head, int id)
{
node *this = head;
while (this = this->p_next) {
if (this->id == id) {
return this;
}
}
return NULL;
}
/* traversal the linked list */
void ll_traversal(ll_head head)
{
node *this = head;
while (this = this->p_next) {
printf("%d ", this->id);
}
printf("");
}
/*
* user.c
* */
#include
#include
#include "data.h"
int main()
{
node *ll_head = ll_create();
node tmp;
int num = 0;
while (num++ <= 10000) {
printf("please input a num: ");
scanf("%d", &tmp.id);
if (tmp.id == 0) {
break;
}
printf("id = %d", tmp.id);
ll_insert(ll_head, &tmp); //插入
}
ll_traversal(ll_head); //遍历链表,顺序打印
ll_free(ll_head); //释放空间
return 0;
}
看了排序(用链表实现,用c实现)将...的网友还看了以下:

1.描述下列实验现象,并列出相应的化学反应方程式:向过量AgNO3溶液中滴入少量Na2S2O3溶液  2020-05-14 …

词语顺序错乱排列例题要求把几个词语错误的题目分正确.比如:地球、中国、宇宙.排列成从小到大中国-地  2020-05-14 …

下列实验操作正确的是()A.用剩的药品不能乱丢,要放回原瓶B.将用剩的药品倒入下水道C.实验中不慎  2020-05-17 …

下列实验操作正确的是()A.用剩的药品不能乱丢,要放回原瓶B.将用剩的药品倒入下水道C.实验中不慎  2020-05-17 …

下列实例不属于稳态失衡引起机体功能紊乱的是()A.尿毒症B.寒冷中的战栗C.急性胃肠炎D.过敏反应  2020-06-08 …

用五个相同的小正方体能摆出多少种不同的形状?(最好有个图解,肿么摆出来勒?)(非诚别乱吵)实在不行  2020-06-26 …

关于安史之乱,下列描述中符合历史史实的是()①安史之乱的根源在于节度使权力过大,王朝统治内轻外重②  2020-07-28 …

语句排列六、将下列排列错乱出话重新整理,在括号里写上序号.()于是,他请人在弓上雕刻了各种各样的花纹  2020-11-26 …

《汉书》曰:“自周衰,官失而百职乱,战国并争,各变异”材料反映出的实质问题是A.周代分封...《汉书  2021-01-16 …

中共八大和十一届三中全会的共同点有①都体现了实事求是的思想路线②都明确了经济建设的主要任务③都提出了  2021-02-05 …