TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/************************************************************************/
- /*
- 9. 设链表中每个结点包括学号、成绩、和指针三个字段,
- 试编程序将成绩最高的结点作为链表的第一个结点,成绩最低的结点作为尾结点。
-
- */
- /************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- typedef struct student STU;
- struct student
- {
- int num;
- int gread;
- struct student * next;
- };
-
-
- STU * Init()
- {
- STU *p=(STU*)malloc(sizeof(STU)); // 初始化链表节点
- if (p==NULL)
- {
- printf("分配内存失败");
- return NULL;
- }
- else
- {
- /* p->gread=gread;
- p->num=num;*/
- p->next=NULL;
- return p;
- }
- }
- STU * Insert(STU *head,int num,int gread) // 插入数据到链表
- {
- STU *p=(STU*)malloc(sizeof(STU));
- if (p==NULL)
- {
- printf("分配内存失败");
- return NULL;
- }
- else
- {
- STU *last=head ;
- if (last==NULL)
- {
- return NULL;
- }
- else
- {
- while(last->next!=NULL) // 找到最后链表位置
- last=last->next;
-
- p->gread=gread; // 初始化该节点
- p->num=num;
- p->next=NULL;
- last->next=p; // 让最后一个节点的下一个节点,指向新的节点,完成添加
- return p;
- }
- }
-
- }
- void PrintAll(STU *head) // 输出所有节点
- {
- STU * index=head;
- if (index==NULL)
- {
- return;
- }
- else
- {
- while(index->next!=NULL) // 第一个节点是头节点,数据没有意义,第一个节点实际是头节点的next节点
- {
- printf("%d %d | \n",index->next->num,index->next->gread);
- index=index->next;
- }
-
- }
- }
- STU * FindMax(STU *head) // 查找最大值
- {
-
- STU * index=head;
- STU * Max=head;
- if (index==NULL)
- {
- return NULL;
- }
- else
- {
- while(index->next!=NULL) // 遍历所有节点
- {
- if (Max->next->gread<index->next->gread) // 如果当前节点的值大于最大值,则将最大值置为当前节点,如此反复,遍历完链表之后,最大值也就确定了
- {
- Max=index;
- }
- index=index->next;
- }
-
- }
- return Max;
- }
- STU * FindMin(STU *head) // 查找最小值节点,跟最大值节点类似
- {
-
- STU * index=head;
- STU * Min=head;
- if (index==NULL)
- {
- return NULL;
- }
- else
- {
- while(index->next!=NULL)
- {
- if (Min->next->gread>index->next->gread)
- {
- Min=index;
- }
- index=index->next;
- }
-
- }
- return Min ;
- }
- void Adjustlist(STU * head)
- {
- STU *pmin=FindMin(head); // 查找成绩最小节点
- STU * temp=pmin->next; // temp为pmin的后一个节点
- pmin->next=temp->next; // 把temp节点删除
- STU * index=head; // 搜索尾巴节点
- while(index->next!=NULL)
- index=index->next;
- index->next=temp; // 把temp放在最尾部
- temp->next=NULL;
- STU *pmax=FindMax(head); // 再找max节点
- STU * tempmax=pmax->next; // 把max节点放到链表头
- pmax->next=tempmax->next;
- tempmax->next=head->next;
- head->next=tempmax;
- }
-
- void main()
- {
- STU *p=Init();
- Insert(p,1,10); // 初始化链表节点
- Insert(p,2,1);
- Insert(p,13,13);
- Insert(p,14,15);
- Insert(p,51,16);
-
- Insert(p,16,12);
- Insert(p,17,17);
- PrintAll(p); // 打印初始化结果
- Adjustlist(p); // 调整位置,按照题目要求
- printf("\n\n\n");
- PrintAll(p); // 打印调整位置后结果
- system("pause");
- }</pre><br><br></pre></pre>
|
|