早教吧作业答案频道 -->其他-->
设L为单链表(带头结点),其中每个结点由一个整数域data和指针域next组成,……设L为单链表(带头结点),其中每个结点由一个整数域data和指针域next组成,整数域data中的数据都是正整
题目详情
设L为单链表(带头结点),其中每个结点由一个整数域 data和指针域next组成,……
设L为单链表(带头结点),其中每个结点由一个整数域 data和指针域next组成,整数域 data中的数据都是正整数且无相同的,试设计原地将该链表整理成数据递增的有序单链表的算法。
设L为单链表(带头结点),其中每个结点由一个整数域 data和指针域next组成,整数域 data中的数据都是正整数且无相同的,试设计原地将该链表整理成数据递增的有序单链表的算法。
▼优质解答
答案和解析
呵呵,刚做完一个差不多的程序,分享下。
#include
#include
#include
typedef struct node
{
int data;
struct node *next;
}Node;
void InitList(Node **head);
void CreateList(Node **head);
void InsertList(Node **head, int key);
void DeleteList(Node **head, int key);
void PrintList(Node **head);
void paixu(Node **head);
//初始化链表
void InitList(Node **head)
{
(*head) = (Node *)malloc(sizeof(Node));
(*head)->next = NULL;
}
//创建链表
void CreateList(Node **head)
{
int i;
printf("请输入要插入的数据(以0结束):\n");
scanf("%d", &i);
while(i != 0)
{
InsertList(head, i);
scanf("%d", &i);
}
}
//插入链表
void InsertList(Node **head, int key)
{
Node *p, *q, *s;
q = (*head);
p = (*head)->next;
while(p)
{
q = p;
p = p->next;
}
s = (Node *)malloc(sizeof(Node));
s->data = key;
s->next = NULL;
q->next = s;
}
//删除链表
void DeleteList(Node **head, int key)
{
Node *p, *q;
q = (*head);
p = (*head)->next;
while(p && p->data != key)
{
q = p;
p = p->next;
}
if(p)
{
q->next = p->next;
free(p);
p = NULL;
}
}
//输出链表
void PrintList(Node **head)
{
Node *p;
p = (*head)->next;
while(p)
{
printf("%d\n", p->data);
p = p->next;
}
}
//排序链表
void paixu(Node **head)
{
Node *p,*q,*t,*s;
p=(*head)->next;
if(p==NULL) return;//没有节点
s=p->next;
if(s==NULL) return;//只有一个节点
p->next=NULL;//把链表截断成只有1个节点
while(s!=NULL)
{
t=s->next;//先记住下一个要处理的节点
s->next=NULL;//把当前节点独立出来
//从当前有序链表中查找插入点
q=(*head);
p=(*head)->next;
while(p!=NULL&&p->datadata)
{
q=p;
p=p->next;
}
//循环出来后,q是新插入节点的前驱,p是后继
q->next=s;
s->next=p;
//把之前记录的待处理节点传给s,以便循环继续处理未处理的节点
s=t;
}
}
int main(void)
{
Node *head;
int i;
InitList(&head);
CreateList(&head);
printf("删除前的数据:\n");
PrintList(&head);
printf("请输入要删除的数据:\n");
fflush(stdin);
scanf("%d", &i);
DeleteList(&head, i);
printf("删除后的数据:\n");
PrintList(&head);
paixu(&head);
printf("排序后的数据:\n");
PrintList(&head);
return 0;
}
#include
#include
#include
typedef struct node
{
int data;
struct node *next;
}Node;
void InitList(Node **head);
void CreateList(Node **head);
void InsertList(Node **head, int key);
void DeleteList(Node **head, int key);
void PrintList(Node **head);
void paixu(Node **head);
//初始化链表
void InitList(Node **head)
{
(*head) = (Node *)malloc(sizeof(Node));
(*head)->next = NULL;
}
//创建链表
void CreateList(Node **head)
{
int i;
printf("请输入要插入的数据(以0结束):\n");
scanf("%d", &i);
while(i != 0)
{
InsertList(head, i);
scanf("%d", &i);
}
}
//插入链表
void InsertList(Node **head, int key)
{
Node *p, *q, *s;
q = (*head);
p = (*head)->next;
while(p)
{
q = p;
p = p->next;
}
s = (Node *)malloc(sizeof(Node));
s->data = key;
s->next = NULL;
q->next = s;
}
//删除链表
void DeleteList(Node **head, int key)
{
Node *p, *q;
q = (*head);
p = (*head)->next;
while(p && p->data != key)
{
q = p;
p = p->next;
}
if(p)
{
q->next = p->next;
free(p);
p = NULL;
}
}
//输出链表
void PrintList(Node **head)
{
Node *p;
p = (*head)->next;
while(p)
{
printf("%d\n", p->data);
p = p->next;
}
}
//排序链表
void paixu(Node **head)
{
Node *p,*q,*t,*s;
p=(*head)->next;
if(p==NULL) return;//没有节点
s=p->next;
if(s==NULL) return;//只有一个节点
p->next=NULL;//把链表截断成只有1个节点
while(s!=NULL)
{
t=s->next;//先记住下一个要处理的节点
s->next=NULL;//把当前节点独立出来
//从当前有序链表中查找插入点
q=(*head);
p=(*head)->next;
while(p!=NULL&&p->data
{
q=p;
p=p->next;
}
//循环出来后,q是新插入节点的前驱,p是后继
q->next=s;
s->next=p;
//把之前记录的待处理节点传给s,以便循环继续处理未处理的节点
s=t;
}
}
int main(void)
{
Node *head;
int i;
InitList(&head);
CreateList(&head);
printf("删除前的数据:\n");
PrintList(&head);
printf("请输入要删除的数据:\n");
fflush(stdin);
scanf("%d", &i);
DeleteList(&head, i);
printf("删除后的数据:\n");
PrintList(&head);
paixu(&head);
printf("排序后的数据:\n");
PrintList(&head);
return 0;
}
看了 设L为单链表(带头结点),其...的网友还看了以下:
从540里减去一个整十数,得到的差再除以这个整十数,结果商是8.这个整十数十多少? 2020-04-07 …
、下面记录的是某班一次数学测验的成绩.将整理数据的结果填写在表格里.甲组:987680948894 2020-05-23 …
小明在计算一个整十数乘两位数时把整十数末尾的0漏看了,结果比正确的结果少225正确的答案是多少? 2020-07-05 …
得数取整数是四舍五入还是去掉小数部分我在算一组数的平均数时,结果是153.6,题里说得数取整数,结 2020-07-18 …
小红在做一个整十数乘7时,把整十数未尾0看丢了,结果比正确的结果少了189,正确是多少? 2020-07-18 …
小明在计算一个整十数乘两位数时,把整十数末尾的0漏掉了,结果比正确的结果少225,正确的答案是. 2020-07-19 …
如果题目要求结果保留一位小数,那么结果是整数还要保留吗?化学计算题要求计算结果保留一位小数,结果算出 2020-10-30 …
从键盘输入以-1结束的n个正整数,存入一个整形数组中,(1)先将最大的数与第一个数据交换,最小的数与 2020-11-20 …
SQL整数不变四舍五入保留2位小数cast(round(100,2)asdecimal(10,2)) 2020-12-03 …
下列语句中不正确的是()A.任何一个有理数的绝对值都不会是负数B.任何数都有立方根C.大的数减小的数 2021-01-22 …