这个程序怎么修改哈~请高手指教!
M1_13.rar
(959 Bytes)
#include <stdio.h> #include <stdlib.h> struct node {int d; struct node *next;}; struct node *create(void) /*开辟一个8结点的单向链表*/ { struct node *head=NULL,*p,*q=NULL; int i; for(i=3;i<=10;i++) {p=(struct node *)malloc(sizeof(struct node)); p->d=i;p->next=NULL; if(head==NULL) head=p; else q->next=p; q=p; } return head; } void print(struct node *head) /*打印链表*/ {if(head==NULL) return; while(head->next!=NULL) {printf("%d",head->d);head=head->next;} printf("%d\n",head->d); } struct node *delst(struct node *head,int *n) {int count=0;struct node *p,*q,*r; p=r=head; while(p!=NULL) /*p指向的结点后移*/ {q=p->next; while(q!=NULL) /*若q指向的结点值是p指向的结点值的整 */ {if((q->d)%(p->d)==0) /* 数倍,去掉q指向的结点,否则q后移一个结点*/ {r->next=q->next; free(q);count++;q=r->next;} else{r=q;q=q->next; } p=p->next; } *n=count;return head; } void main() {int y;struct node *head; head=create(); print(head); head=delst(head,&y); print(head); printf("%d",y); }把源程序红色部分改成 if((q->d)%(p->d)==0)
#include <stdio.h> #include <stdlib.h> struct node {int d; struct node *next;}; struct node *create(void) /*开辟一个8结点的单向链表*/ { struct node *head=NULL,*p,*q=NULL; int i; for(i=3;i<=10;i++) {p=(struct node *)malloc(sizeof(struct node)); p->d=i;p->next=NULL; if(head==NULL) head=p; else q->next=p; q=p; } return head; } void print(struct node *head) /*打印链表*/ {if(head==NULL) return; while(head->next!=NULL) {printf("%d",head->d);head=head->next;} printf("%d\n",head->d); } struct node *delst(struct node *head,int *n) {int count=0;struct node *p,*q,*r; p=r=head; while(p!=NULL) /*p指向的结点后移*/ {q=p->next; while(q!=NULL) /*若q指向的结点值是p指向的结点值的整 */ if((q->d)%(p->d)==0) /* 数倍,去掉q指向的结点,否则q后移一个结点*//*这里去掉了一个{*/ {r->next=q->next; free(q);count++;q=r->next;} else{r=q;q=q->next; } p=p->next; } *n=count;return head; } void main() {int y;struct node *head; head=create(); print(head); head=delst(head,&y); print(head); printf("%d",y); }