69,369
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
1.链表与数组综合操作(本题满分100分,每问20分)
a) 生成一个长度100、取值范围在0-300的整数构成的链表,并打印
b) 将其按从小到大的顺序插入到另一个链表,并遍历打印
c) 将这个排好序的链表的结果写入到文件1000_numbers.txt
d) 再从文件读入数据,存储到一个数组,并打印
e) 统计出现频次最多的数字,并打印
*/
struct node
{
int num;
struct node *next;
};
struct node *creat_lianbiao()
{
//生成一个长度100,取值范围在0-300的随机整数构成的链表...Done
//int num;
srand( time(0) );
int i = 0;
struct node *head = NULL, * p = head, *newnode = NULL;
while(1)
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = rand()%301 ;
//printf("%d ",newnode->num);
if(head == NULL)
{
head = newnode;
p = head;
}
else if(head != NULL)
{
p->next = newnode;
p = p->next;
}
i++;
if(i == 100)
{
p->next = NULL;
break;
}
}
//p=head;
return head;
}
//遍历打印链表的函数
void print_lianbiao(struct node *head)
{
struct node *p = head;
int i = 0;
while(p != NULL)
{
i++;
printf("the %dth number: ", i );
printf("%d\n", p->num );
p = p->next;
}
}
/*核心部分:
说明:
1.本题不能在原链表里排序后再插入到另一个链表
2.不能使用链表进行插入
3.我的想法是:循环,找出每次循环的最小值,
把这个最小值所在的结点插入到新建的链表里,同时在原链表里删除该结点
这样在下一次循环的时候就不会找到上次的最小值
4.问题是循环无法执行下去
*/
struct node *creat_sort_lianbiao(struct node *head)
{
struct node *p = head, *head_2 = NULL ,*p_min = head ,*p_2 = head_2;
int min = p_min->num ,i=0;
while(1)//
{
p = head;
min = head->num;
while(1)//每次循环找当前链表里的最小值
{
if(min > p->num)//如果min记录的大于当前值
{
//printf("Done...\n");
min = p->num;
p_min = p;//把当前值的地址给p_min
}
if(p->next == NULL) break; //如果到尾了,break
p = p->next;
}
p = head;
i++;
while(1)//每次循环把原链表的min值所在地址赋给p_2,并将原链表的该结点删除
{
if(p->next == p_min)//循环搜索,如果匹配到p_min
{
p_2->next = (struct node *)malloc(sizeof(struct node)); //开辟节点
p_2->next->next = (struct node *)malloc(sizeof(struct node));//再开辟节点
if(head_2 == NULL)//如果head_2是空链表
{
//printf("Done...\n");
head_2 = p_min;
p->next = p->next->next; //删除原链表中的min值的节点
printf("p_min->num = %d\n",p_min->num);
break;
}
else if(head_2 != NULL)
{
p_2->next = p_min;
p_2 = p_2->next;
p_2->next = NULL;
p->next = p->next->next;
break;
}
}
p = p->next;
}
if(i == 100)//如果执行了100次就结束
{
//printf("Done...\n");
break;
}
}
//printf("Done...\n");
return head_2;
}
void main()
{
printf("第一题:\n");
struct node * head = NULL, * p = head ,* head_2=NULL;
head = creat_lianbiao();//生成一个长度100,取值范围在0-300的随机整数构成的链表...Done
print_lianbiao(head);//打印原链表...Done
putchar('\n');
putchar('\n');
head_2 = creat_sort_lianbiao(head);//生成新的链表
printf("Done...");
print_lianbiao(head_2);//打印新的链表
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
1.链表与数组综合操作(本题满分100分,每问20分)
a) 生成一个长度100、取值范围在0-300的整数构成的链表,并打印
b) 将其按从小到大的顺序插入到另一个链表,并遍历打印
c) 将这个排好序的链表的结果写入到文件1000_numbers.txt
d) 再从文件读入数据,存储到一个数组,并打印
e) 统计出现频次最多的数字,并打印
*/
struct node
{
int num;
struct node *next;
};
struct node *creat_lianbiao()
{
//生成一个长度100,取值范围在0-300的随机整数构成的链表...Done
//int num;
srand( time(0) );
int i = 0;
struct node *head = NULL, * p = head, *newnode = NULL;
while(1)
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = rand()%301 ;
//printf("%d ",newnode->num);
if(head == NULL)
{
head = newnode;
p = head;
}
else if(head != NULL)
{
p->next = newnode;
p = p->next;
}
i++;
if(i == 100)
{
p->next = NULL;
break;
}
}
//p=head;
return head;
}
//遍历打印链表的函数
void print_lianbiao(struct node *head)
{
struct node *p = head;
int i = 0;
while(p != NULL)
{
i++;
printf("the %dth number: ", i );
printf("%d\n", p->num );
p = p->next;
}
}
/*核心部分:
说明:
1.本题不能在原链表里排序后再插入到另一个链表
2.不能使用链表进行插入
3.我的想法是:循环,找出每次循环的最小值,
把这个最小值所在的结点插入到新建的链表里,同时在原链表里删除该结点
这样在下一次循环的时候就不会找到上次的最小值
4.问题是循环无法执行下去
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
struct node *creat_sort_lianbiao(struct node *head)
{
struct node *p = head, *head_2 = NULL ,*p_min = head ,*p_2 = head_2 ,*newnode_2 = NULL ,*tmpnode = NULL;
int min = p_min->num ,i = 0;
while(1)//
{
p = head;
min = head->num;
p_min = head;
while(1)//每次循环找当前链表里的最小值
{
if(min > p->num)//如果min记录的大于当前值
{
//printf("Done...\n");
min = p->num;
p_min = p;//把当前值的地址给p_min
}
printf("min=%d\n",min);
if(p->next == NULL) break; //如果到尾了,break
p = p->next;
}
p = head;
i++;
while(1)//每次循环把原链表的min值所在地址赋给p_2,并将原链表的该结点删除
{
if(p_min == head)
{
newnode_2 = (struct node *)malloc(sizeof(struct node));//开辟节点
newnode_2->num = p_min->num;
newnode_2->next = NULL;
// p_2->next->next = (struct node *)malloc(sizeof(struct node));//再开辟节点
if(head_2 == NULL)//如果head_2是空链表
{
//printf("%d",head_2);
head_2 = newnode_2;
p_2 = head_2;
p_2->next = NULL;
tmpnode = head;
head = head->next;
free(tmpnode);
p = head;
//printf("%d\n",head_2);
printf("p_2->num = %d\n", p_2->num );
break;
}
else if(head_2 != NULL)
{
//p_2 = head_2;
p_2->next = newnode_2;
p_2 = p_2->next;
p_2->next = NULL;
tmpnode = head;
head = head->next;
free(tmpnode);
p = head;
printf("p_2->num = %d\n", p_2->num );
break;
}
p = p->next;
}
else if(p_min != head)
{
if(p->next == p_min)//循环搜索,如果匹配到p_min
{
printf("I find it !\n");
newnode_2 = (struct node *)malloc(sizeof(struct node));//开辟节点
newnode_2->num = p_min->num;
newnode_2->next = NULL;
// p_2->next->next = (struct node *)malloc(sizeof(struct node));//再开辟节点
if(head_2 == NULL)//如果head_2是空链表
{
//printf("%d",head_2);
head_2 = newnode_2;
p_2 = head_2;
p_2->next = NULL;
p->next = p->next->next; //删除原链表中的min值的节点
free(p_min);
//printf("%d\n",head_2);
printf("p_2->num = %d\n", p_2->num );
break;
}
else if(head_2 != NULL)
{
p_2->next = newnode_2;
p_2 = p_2->next;
p_2->next = NULL;
p->next = p->next->next;
free(p_min);
printf("p_2->num = %d\n", p_2->num );
break;
}
}
p = p->next;
}
}
printf("The %dth round\n",i);
if(i == 100)//如果执行了100次就结束
{
//printf("Done...\n");
break;
}
}
//printf("Done...\n");
return head_2;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void main()
{
printf("This is the mid-term exam of 张圣儒 no.20344152\n");
printf("第一题:\n");
struct node * head = NULL, * p = head ,* head_2=NULL;
head = creat_lianbiao();//生成一个长度100,取值范围在0-300的随机整数构成的链表...Done
print_lianbiao(head);//打印原链表...Done
putchar('\n');
putchar('\n');
head_2 = creat_sort_lianbiao(head);//生成新的链表
//printf("Done...");
print_lianbiao(head_2);//打印新的链表
}
(a)(b)步运行结果: