#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
int times;
struct node* next;
};
typedef struct node Ele;
Ele* Creatlink(int a[],int N){
Ele *head = NULL, *tail=NULL, *p;
for (int i = 0; i < N; i++){
for (p = head; p && p->data!=a[i];p=p->next);
if (p) {
p->times++;
} else {
p = (Ele*)malloc(sizeof(Ele));
p->data = a[i];
p->times = 1;
p->next = NULL;
//建节点
// 插入
if(head){
//挂尾链,移尾指针
tail = tail->next = p;
}else{
head = tail = p;
}
}
}
return head;
}
void print(Ele*head){
Ele *p = head;
while(p){
printf("%3d %3d\n", p->data,p->times);
p = p->next;
}
}
int main(void){
int a[] = {3,3,5,9,4,6,5,4,2,2};
Ele *head;
int len = sizeof(a)/sizeof(a[0]);
// printf("%d",len);
head = Creatlink(a, len);
print(head);
}
统计数组中的每个数字的个数(链表)
最新推荐文章于 2022-11-23 17:04:54 发布