求助:c-free问题修改一下
题目:编一程序,输入职员工号和完成产品数量,程序允许同一职工有多次输入,
由程序对其完成数量实现累计。程序按完成数量对它们排序,并确定名次。
按完成产品数量由多到少顺序,输出名次、同一名次的职员人数及他们的工号(工号由大到小)。
要求程序用有序链表储存数据信息.
我编的这个不能实现累加,高手帮我改一下,按题目要求
#include<stdio.h>
#include <stdlib.h>
//创建链表,并定义
struct node;
typedef struct node *ptr;
struct node
{
int num;
int amount;
ptr Next;
};
ptr CreatNode(int num,int amount);
ptr FindNode(ptr List,ptr NewNode);
void Insert( ptr List,ptr NewNode);
int compare(ptr L,ptr R);
void ShowRank(ptr List);
//定义工号产品数量
int main()
{
int num;
int amount;
ptr NodeFound,NewNode,List;
NodeFound = NewNode = List = NULL;
printf("Guide:print O,the result will present.\nType in data like this:123456 100\nPut in negative num to stop insert.\nPut in zero to show ranks.\n"); //提示用户如何输入的出结果
List = CreatNode(0,0);
scanf( "%d",&num);
while(num>=0)
{
while(!num){
ShowRank(List);
printf("Now please input data.\n");
scanf( "%d",&num);
if(num<0){ //输入一个小于0的数显示结果
printf("Thank you.\n");
getchar();
return 0;
}
}
scanf( "%d",&amount);
NewNode = CreatNode(num,amount); //创建一个新的链表
if( NodeFound = FindNode(List,NewNode) )
NewNode = NodeFound;
Insert(List,NewNode);
scanf( "%d",&num);
}
ShowRank(List);
printf("Thank you.\n");
getchar();
return 0;
}
ptr CreatNode(int num,int amount)
{
ptr NewNode;
NewNode = (ptr)malloc(sizeof(struct node));
NewNode->amount = amount;
NewNode->num = num;
NewNode->Next = NULL;
return NewNode;
}
ptr FindNode(ptr List,ptr NewNode)
{
ptr Pre;
Pre = List;
while(List->Next){
if(List->num == NewNode->num){
Pre->Next = List->Next;
List->amount += NewNode->amount;
List->Next = NULL;
return List;
}
Pre = List;
List = List->Next;
}
return List->Next;
}
void Insert( ptr List,ptr NewNode)
{
ptr tmp;
while( compare(List->Next,NewNode) )
List = List->Next;
NewNode->Next = List->Next;
List->Next = NewNode;
return;
}
int compare(ptr L,ptr R)
{
if(!L)
return 0;
if(L->amount > R->amount) //比较产品数量多少,返回
return 1;
else if(L->amount < R->amount)
return 0;
else if(L->num <R->num) //比较工号大小,返回
return 1;
else if(L->num < R->num)
return 0;
}
void ShowRank(ptr List)
{
List = List->Next;
if(!List){
printf("No Data!\n");
return;
}
printf("Rank of data:\n");
while(List)
{
printf("%8d %8d\n",List->num,List->amount);
List = List->Next;
}
printf("All Ranked!\n"); //最后输出最后的排列
return;
}