65,211
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
typedef struct _TNode{
int val;
int count;
struct _TNode* next;
}TNode;
TNode* get_result(int (*a)[2],int row)
{
TNode *link = NULL;
TNode *plink = link;
TNode *prevlink = link;
int i=0;
int j=1;
int tag=0;
int first = 1;
while(i<row)
{
j=1;
while(j<2)
{
link = plink;
while(link!=NULL)
{
if (a[i][j] == link->val)
{
link->count += 1;
tag = 1;
break;
}
tag = 0;
prevlink = link;
link = link->next;
}
if(tag == 0)
{
TNode* pnode = (struct _TNode*)malloc(sizeof(struct _TNode));
if(pnode == NULL)
{exit(-1);}
pnode->val=a[i][j];
pnode->count=1;
pnode->next = NULL;
if(first++ == 1)
{
link = pnode;
plink = link;
}
else
{
link = plink;
prevlink->next=pnode;
}
}
j++;
}/*cols..*/
i++;
}/*rows...*/
return plink;
}
void display(TNode* link)
{
TNode* plink = link;
while(plink!=NULL)
{
printf("Student NO :%d\tcount :%d\n",plink->val,plink->count);
plink=plink->next;
}
}
void freelink(TNode*link)
{
TNode* pt= link;
while(link!=NULL)
{
pt = link;
link = link->next;
free(pt);
}
}
int main()
{
int arr[10][2]={ \
1,10001,\
2,10002,\
3,10003,\
4,10005,\
5,10002,\
6,10007,\
7,10003,\
8,10009,\
9,10002,\
10,10005\
};
int i=0,j;
for(;i<10;i++)
{
for(j=0;j<2;j++)
{
printf("%-2d,",arr[i][j]);
}
printf("\n");
}
TNode* link = get_result(arr,10);
display(link);
freelink(link);
return 0;
}
/*
1 ,10001,
2 ,10002,
3 ,10003,
4 ,10005,
5 ,10002,
6 ,10007,
7 ,10003,
8 ,10009,
9 ,10002,
10,10005,
Student NO :10001 count :1
Student NO :10002 count :3
Student NO :10003 count :2
Student NO :10005 count :2
Student NO :10007 count :1
Student NO :10009 count :1
Press any key to continue
*/