早教吧作业答案频道 -->其他-->
排序(用链表实现,用c实现)将一个杂乱无序的整数序列,按照从小到大的顺序排列并输出。用第二章的相关知识实现,即先初始化一个空的单链表,然后每读入一个数据,将该数据存入结
题目详情
排序(用链表实现,用c实现)
将一个杂乱无序的整数序列,按照从小到大的顺序排列并输出。
用第二章的相关知识实现,即先初始化一个空的单链表,然后每读入一个数据,将该数据存入结点并插入单链表,当然,插入时要从头结点开始向后(或从尾结点开始向前)搜索合适的插入位置,以保证插入后仍然有序。
输出时,从头结点开始向后,顺序输出各结点的值。
输入
测试数据不止一组,每组测试数据:
1)先输入无序序列的整数个数n;(n不超过10000)
2)然后连续输入n个整数;
若n的值输入为0值,则输入结束.
输出
与每组输入的测试数据相对应,输出其按从小到大排好序后的整数序列.
注意:每组输出占一行.
将一个杂乱无序的整数序列,按照从小到大的顺序排列并输出。
用第二章的相关知识实现,即先初始化一个空的单链表,然后每读入一个数据,将该数据存入结点并插入单链表,当然,插入时要从头结点开始向后(或从尾结点开始向前)搜索合适的插入位置,以保证插入后仍然有序。
输出时,从头结点开始向后,顺序输出各结点的值。
输入
测试数据不止一组,每组测试数据:
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;
}
然后编译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实现)将...的网友还看了以下:
将分别写有abcde12345的10张纸片排成一列,要求字母按英文字母顺序表的先后顺序排列且数字左 2020-05-15 …
下表所示为对酶有关特性的研究.实验现象中,“+”表示有气泡产生,“-”表示无气泡产生,“+”的数量 2020-06-10 …
下表所示为对酶有关特性的研究.实验现象中,“+”表示有气泡产生,“-”表示无气泡产生,“+”的数量 2020-06-10 …
对的理解有误的一项是A.词人神游月宫的幻想是因在人间感到寂寞而产生的,而这寂寞感又跟中秋之夜不能跟 2020-07-05 …
用冒泡排序法将无序列{7,1,3,12,8,4,9,10}按照从大到小的顺序排列,并画出该算法的流 2020-07-23 …
EXCEL表格大写字母ABCD.咋排序表格内有FADHL...按照ABCD...从小到大排序 2020-07-23 …
顺序查找平均比较次数对长度为n的顺序表进行顺序查找,问平均比较次数是多少?答案给出的是:n/2.( 2020-07-30 …
数据结构判断题~在顺序表中进行顺序搜索时,若各元素的搜索概率不等,则各元素应按照搜索概率的降序排列存 2020-11-03 …
一个数据结构中...用二叉树表示无序序列的问题二叉树13/\3827/\/\49!796549/97 2020-12-05 …
直接插入排序+简单选择排序通过两种方法进行排序,以达到整个序列有序[基本要求](1)通过键盘输入关键 2020-12-05 …