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

排序(用链表实现,用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实现)将...的网友还看了以下: