当前位置:编程学习 > C/C++ >>

怎么对链表里的数据进行排列 算平均分 和最高分 最低分

给个例子来 书本没得参考 初级C语言的 我大学2年级
答案:给个例子
 
#define newnode (node *)malloc(sizeof(node))
typedef struct node
{
 int v;
 struct node *next;
}node;
node * head;

 //插在头结点后
void insert(int x)
{
 node *p = newnode;
 p->v = x;
 p->next = head->next;
 head->next = p;
}
//直接遍历即可
void max_min_avg()
{
 node *p = head->next;
 int max, min, n = 1;
 double avg;
 
 if(!p) return;
 max = min = avg = p->v;
 for(p = p->next; p; p=p->next)
 {
  if(max < p->v)max = p->v;
  if(min > p->v)min = p->v;
  avg += p->v;
  ++n;
 }
 avg /= n;
 printf("MAX:%d\tMIN:%d\tAVG:%g\n", max, min, avg);
}
 
void listsort() //简单插入排序
{
 node *p = head->next, *q, *pnext;
 
 head->next = NULL;
 while(p)
 {
  for(q = head; q->next; q = q->next)
   if(q->next->v > p->v)break;  //寻找插入位置
  pnext = p->next;
  p->next = q->next; //p 插入 q 后面
  q->next = p; 
  p = pnext;
 }
 
 for(p=head->next; p; p=p->next)
  printf("%d ", p->v);
}
 
int main()
{
 head = newnode;
 head->next = NULL;        //头结点,使链表操作方便
 
 insert(2);
 insert(3);
 insert(1);
 
 max_min_avg();
 
 listsort();}
其他:从头到尾遍历一遍就知道了啊 排序用qsort函数

在compare函数中记录 最高分,最低分,总分,总数
然后计算平均分 

上一个:C -VC-#C的关键性区别?
下一个:谁有Visual C++ 6.0, Win7可兼容使用的,发到443965173@qq.com, 急用…… 有的谢谢了……

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,