新手求链表改错!!
程序代码:
#include<stdio.h> #include<stdlib.h> typedef struct Node{ int data; struct Node *next; } ElemSN; ElemSN *Creatlink(int a[],int n) //创建链表 { ElemSN *p,*h,*t; for(int i=0;i<n;i++) { p=(ElemSN *)malloc(sizeof(ElemSN)); p->data=a[i]; p->next=NULL; if(i==0) t=h=p; else t=t->next=p; } return h; } ElemSN *DelTailNode(ElemSN *h) { ElemSN *delp,*t,*q=h; for(delp=h->next;delp->next;delp->next) { if(delp->data==q->data) //删除重复值 { q->next=delp->next; //连动指针 free(delp); delp=delp->next; //delp指向下一个结点 } } return h; } void Printlink(ElemSN *h) { ElemSN *p; for(p=h;p;p=p->next) printf("%3d",p->data); } int main(void) { int a[10]={1,2,3,4,22,6,71,83,9,3}; ElemSN *head; head=Creatlink(a,10); head=DelTailNode(head); Printlink(head); return 0; }