链表 计算与指定数字相同的数的个数

    科技2024-07-24  14

    #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node {int data; struct node *next; }Node; int count(Node* head,int m); void destroy(Node*p); int main(void) {int n,m,x; Node *head=NULL,*s,*tail; scanf("%d",&n); for(int i=1;i<=n;i++) {scanf("%d",&x); s=(Node*)malloc(sizeof(Node)); s->data=x; if(i==1) head=s; else tail->next=s; tail=s; } tail->next=NULL; scanf("%d",&m); printf("%d\n",count(head,m)); destroy(head); system("PAUSE"); return 0; } int count(Node* head,int m) { int k=0; Node *p=head; while(p) {if (p->data==m)k++; p=p->next; } return k; } void destroy(Node* head) {Node *p=head,*q; while(p) { q=p; p=p->next; free(q); } }

    程序注解 P 348页

    Processed: 0.010, SQL: 8