早教吧作业答案频道 -->其他-->
排序(用链表实现,用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实现)将...的网友还看了以下:
某学校的学生要去参观科技馆,346入排成两路纵队,相邻两排前后相距0.5米,队伍每分钟走65米,现 2020-04-26 …
用直接插入排序法对1,2,3,4,8,5,7,6,9,13,21,…共21个数按从小到大排序最多需 2020-05-13 …
对排序文件的初始状态不做任何要求的排序方法是()。A.直接插入排序和快速排序B.直接插入和归并排 2020-05-23 …
某局排队机为B排,座席配套AIT板为CQ03板,APC卡为CQ02卡。现在有台座席无法签入排队机,小 2020-05-31 …
在世博园第6号入园口入园的人排成了一列队伍.小芳在其中,她发现在她前,后的人数分别是这队人数的九分 2020-06-04 …
某校高二年级共有六个班级,现从外地转入4名学生,要安排到该年级的两个班级且每班安排2名,则不同的安 2020-06-10 …
某校初二年级共有6个班,现从外地转入4名学生,要安排到该年级的两个班,一个班安排一个人,另一个班安 2020-06-10 …
某校高二年级共有六个班级,现从外地转入4名学生,要安排到该年级的两个班级且每班安排2名,则不同的安 2020-06-10 …
一个水池安装着若干根排水量相等的排水管.现在有一根进水管不停地往水池里注水,每分注入的水量相等.过 2020-06-11 …
电视片《舌尖上的中国》展现了丰富的中华美食文化,当一盘色香味俱佳的酱排骨端到餐桌时,酱排骨的美味香 2020-07-07 …