user-0 提问: 有没有大神看看为什么最后只删除了一个7

回复:
user: user-1
content: #include<stdio.h> #include<stdlib.h> #define MAX 20 typedef struct{ 	int n; 	int data[MAX]; }SEQTABLE;  void delitem(SEQTABLE * st,int e) { 	int i,j; 	for(i=j=0;i<st->n;i++) 		if(st->data[i]!=e) 			st->data[j++]=st->data[i]; 	st->n=j; } int  main() { 	int i; 	SEQTABLE st = {10, {0,5,7,9,8,7,2,6,7,9}}; 	delitem(&st, 7); 	for(i =0; i < st.n; i++) 		printf("%4d", st.data[i]); 	printf("\n"); 	return 0; }

user: user-2
content: #include<stdio.h> #include<stdlib.h>  #define MAX 20  typedef struct{     int n;     int data[MAX+1]; } SEQTABLE;  void delitem(SEQTABLE * st,int e) {     int i, j;     for(i=1;i<=st->n;i++)     {         if(st->data[i]==e)         {             //for(i;i<st->n;i++)             for(j = i;j < st->n; j++)                 st->data[j] = st->data[j+1];             --st->n;             --i;         }     } } int  main() {     int i;      SEQTABLE st = {9, {0,5,7,9,8,7,2,6,7,9}};     delitem(&st, 7);     for(i = 1; i <= st.n; i++)     {         printf("%4d", st.data[i]);     }     printf("\n");     return 0; } 供参考～

user: user-0
content: 引用 4 楼 focuslight的回复:void print(Polylist p) { p = p->next; while(p)   --------------  看看p是否为null P的null空间已经被释放掉了，我改了一下还是输出不出来

user: user-3
content: void print(Polylist p) { p = p->next; while(p)   --------------  看看p是否为null

user: user-0
content: 引用 1 楼 focuslight的回复:要这么写， 你里面的for循环把i走到st->n了， 外围的循环就只走到 第一个7那里就停了  #include<stdio.h> #include<stdlib.h> #define MAX 20 typedef struct{ int n; int data[MAX+1]; } SEQTABLE;  void delitem(SEQTABLE * st,int e) { int i, j; for(i=0;i<=st->n;i++) {     j=i; if(st->data[i]==e) {      for(j;j<st->n;j++) st->data[j]=st->data[j+1]; --st->n; --i; } } } int  main() { int i;  SEQTABLE st = {9, {0,5,7,9,8,7,2,6,7,9}}; delitem(&st, 7); for(i = 0; i <= st.n; i++) { printf("%4d", st.data[i]); } printf("\n"); return 0; }  谢谢谢谢，懂了

user: user-0
content: 引用 1 楼 focuslight的回复:要这么写， 你里面的for循环把i走到st->n了， 外围的循环就只走到 第一个7那里就停了  #include<stdio.h> #include<stdlib.h> #define MAX 20 typedef struct{ int n; int data[MAX+1]; } SEQTABLE;  void delitem(SEQTABLE * st,int e) { int i, j; for(i=0;i<=st->n;i++) {     j=i; if(st->data[i]==e) {      for(j;j<st->n;j++) st->data[j]=st->data[j+1]; --st->n; --i; } } } int  main() { int i;  SEQTABLE st = {9, {0,5,7,9,8,7,2,6,7,9}}; delitem(&st, 7); for(i = 0; i <= st.n; i++) { printf("%4d", st.data[i]); } printf("\n"); return 0; }  可以再问你一个问题吗，我这个程序最后怎么输出不出来啊 #include<stdio.h> #include<stdlib.h> #include<malloc.h> #define NULL 0 typedef struct Polynode { int coef;  //因子 int exp;   //幂 struct Polynode *next; }Polynode , * Polylist;  Polylist polycreate( )  { Polynode *rear, *head, *s;  int c, e;  rear=head=(Polynode *)malloc(sizeof(Polynode));   scanf("%d%d",&c,&e);  while(c!=0)  {  s=(Polynode*)malloc(sizeof(Polynode)); s->coef=c;  s->exp=e;  rear->next=s;  rear=s;   scanf("%d%d",&c,&e);  } rear->next=NULL;  return(head); } Polylist multyploy(Polylist pa, Polylist pb) { Polynode *t1,*t2,*t3,*t,*rear,*p; int c, e; t1=pa->next; t2=pb->next; p=(Polynode*)malloc(sizeof(Polynode)); p->next=NULL; rear=p; while(t2) { t3=(Polynode*)malloc(sizeof(Polynode)); t3->coef=t1->coef*t2->coef; t3->exp=t1->exp+t2->exp; t3->next=NULL; rear->next=t3; rear=t3; t2=t2->next; } t1=t1->next; while(t1) { t2=pb->next; rear=p; while(t2) { e=t1->exp+t2->exp; c=t1->coef*t2->coef; while(rear->next&&rear->next->exp<e) rear=rear->next; if(rear->next&&rear->next->exp==e) { if(rear->next->coef+c) rear->next->coef+=c; else { t=rear->next; rear->next=t->next; free(t);  } } else { t=(Polynode*)malloc(sizeof(Polynode)); t->coef=c; t->exp=e; t->next=rear->next; rear->next=t; rear=rear->next; } } t1=t1->next; } t2=p; p=p->next; free(t2); return p; } void print(Polylist p) { p = p->next; while(p) { printf("%d", p->coef); printf("*x^%d",p->exp); if(p->next!=NULL) printf(" + "); p = p->next; } printf("\n"); }  void main(int argc, char* argv[]) { Polylist pa, pb,pc;   //定义三个表示多项式的链表  pa = polycreate();  //创建pa链表 pb = polycreate();  //创建pb链表 pc = multyploy(pa, pb);   //做乘法法，结果保存在pc链表上 print(pc);      //将加法的结果用一个多项式格式显示在输出设备上 }

user: user-3
content: 要这么写， 你里面的for循环把i走到st->n了， 外围的循环就只走到 第一个7那里就停了  #include<stdio.h> #include<stdlib.h> #define MAX 20 typedef struct{ int n; int data[MAX+1]; } SEQTABLE;  void delitem(SEQTABLE * st,int e) { int i, j; for(i=0;i<=st->n;i++) {     j=i; if(st->data[i]==e) {      for(j;j<st->n;j++) st->data[j]=st->data[j+1]; --st->n; --i; } } } int  main() { int i;  SEQTABLE st = {9, {0,5,7,9,8,7,2,6,7,9}}; delitem(&st, 7); for(i = 0; i <= st.n; i++) { printf("%4d", st.data[i]); } printf("\n"); return 0; }