链表实现 最大值放在头 最小值放在尾

  1 //成绩最高结点放在第一个结点 最低放在最后一个结点
  2 //
  3 //2017.3.9
  4 //
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8 
  9 typedef struct student su;
 10 struct student 
 11 {
 12     int num;//学号
 13     int score;//分数
 14     struct student *next;
 15 };
 16 //初始化链表
 17 su* init()
 18 {
 19     su *p = (su*)malloc(sizeof(su));//开辟空间
 20     if (NULL == p)//养成习惯 判断
 21     {
 22         return NULL;
 23     }
 24     else
 25     {
 26         p->next = NULL;//第一个结点 不需要下一个地址 也没有目前
 27     }
 28     return p;//这样做的原因是保证多有的调用一致性
 29 }
 30 
 31 su* insert(su *head, int num, int score)
 32 {
 33     su *p = (su*)malloc(sizeof(su));//开辟空间
 34     if (NULL == p)
 35     {
 36         printf("");
 37         return NULL;
 38     }
 39     else
 40     {
 41         su *pp = head;//从头开始
 42         if (pp==NULL)
 43         {
 44             return NULL;
 45         }
 46         else
 47         {
 48             while (pp->next != NULL)
 49             {
 50                 pp = pp->next;//向后移动
 51             }
 52             p->score = score;
 53             p->num = num;
 54             p->next = NULL;
 55             pp->next = p;//与前一个建立链接
 56         }
 57     }
 58 }
 59 
 60 //输出
 61 void print(su *head)
 62 {
 63     su *index = head;
 64     if (NULL == index)
 65     {
 66         return;
 67     }
 68     else
 69     {
 70         while (index->next != NULL)
 71         {
 72             printf("%d    %d   **
", index->next->num, index->next->score);
 73             index = index->next;
 74         }
 75         {
 76 
 77         }
 78     }
 79 }
 80 
 81 //查找最大值
 82 su *findmax(su *head)
 83 {
 84     su *index = head;
 85     su *max = head;//假设开始为最大
 86     if (index == NULL)
 87     {
 88         return NULL;
 89     }
 90     else
 91     {
 92         while (index->next!=NULL)
 93         {
 94             if (max->next->score < index->next->score)
 95             {
 96                 max = index;//最大值结点
 97             }
 98             index = index->next;
 99 
100         }
101     }
102     return max;
103 }
104 
105 //查找最小值
106 su *findmin(su *head)
107 {
108     su *index = head;
109     su *min = head;
110     if (index == NULL)
111     {
112         return NULL;
113     }
114     else
115     {
116         while (index->next != NULL)
117         {
118             if (min->next->score > index->next->score)
119             {
120                 min = index;//最小值结点
121             }
122             index = index->next;
123 
124         }
125     }
126     return min;
127 }
128 void    Adjustlist(su * head)
129 {
130     su *pmin = findmin(head);  // 查找成绩最小节点
131     su * temp = pmin->next;       // temp为pmin的后一个节点
132     pmin->next = temp->next;       // 把temp节点删除
133     su * index = head;           // 搜索尾巴节点
134     while (index->next != NULL)
135         index = index->next;
136     index->next = temp;          // 把temp放在最尾部
137     temp->next = NULL;
138     su *pmax = findmax(head);     // 再找max节点
139     su * tempmax = pmax->next;   // 把max节点放到链表头
140     pmax->next = tempmax->next;
141     tempmax->next = head->next;
142     head->next = tempmax;
143 }
144 
145 void main()
146 {
147     su *p = init();
148     insert(p, 1, 10);  // 初始化链表节点
149     insert(p, 2, 1);
150     insert(p, 18, 13);
151     insert(p, 27, 14);
152     insert(p, 51, 16);
153 
154     insert(p, 18, 11);
155     insert(p, 19, 17);
156     print(p);      // 打印初始化结果
157     Adjustlist(p);     // 调整位置,按照题目要求
158     printf("


");
159     print(p);      // 打印调整位置后结果
160     system("pause");
161 }
View Code
原文地址:https://www.cnblogs.com/lanjianhappy/p/6533408.html