早教吧作业答案频道 -->其他-->
排序(用链表实现,用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实现)将...的网友还看了以下:
杭电ACM2019数列有序问题输出错误ProblemDescription有n(n<=100)个整数 2020-03-30 …
杭电ACM2019数列有序输出错误ProblemDescription有n(n<=100)个整数,已 2020-03-30 …
栈和队列数据结构各有什么特点,什么情况下用到栈,什么情况下用到队列试证明:若借助栈,由输入序列1, 2020-06-28 …
入栈与出栈顺序一个栈的入栈序列是A,B,C,D,E,则栈的不可能的输出序列是()1.EDCBA2. 2020-06-28 …
数据结构作业,请帮做下第八章1.分别用下列排序算法对关键字序列(49,7,50,5,94,16,9 2020-07-17 …
数据结构给出初始码待排序码{27,46,5,18,16,51,32,26}使用下面各种排序算法的状态 2020-11-21 …
文章的顺序分为哪几种?最近在做卷子阅读题时,经常出现这样的问题:本文是按什么顺序写的?常常弄得我摸不 2020-12-05 …
若以1234作为双端队列的输入序列,试分别求出满足以下条件的输出序列:(1)能由输入受限的双端队列得 2020-12-05 …
单链表操作1.从键盘输入顺序任意的5个整数,生成第一个有序单链表,将该链表输出显示。2.再从键盘输入 2020-12-05 …
直接插入排序+简单选择排序通过两种方法进行排序,以达到整个序列有序[基本要求](1)通过键盘输入关键 2020-12-05 …