#include"stdio.h" #include"stdlib.h" struct linknode { int date; struct linknode *next; }; struct linknode *creat()//建立链表 { struct linknode *head,*tail,*temp; int dates; head=temp=tail=NULL; while(scanf("%d",&dates)!=EOF) { temp=(struct linknode*)malloc(sizeof(struct linknode)); if(temp==NULL) printf("allocate error!\n"); else { temp->date=dates; temp->next=NULL; if(head==NULL) head=tail=temp; else { tail->next=temp; tail=temp; } } } return head; } void print(struct linknode *head)//打印链表 { struct linknode *p; p=head; while(p) { printf("%d ",p->date); p=p->next; } } struct linknode*find(struct linknode*head,int dates)//找特定值 { struct linknode *p; p=head; while(p->date!=dates&&p->next!=NULL) { p=p->next; } if(p->date==dates) return p; else return NULL; } struct linknode*findahead(struct linknode *head,int dates)//找特定值的前一个 { struct linknode *p,*q; p=head; q=NULL; while(p->date!=dates&&p->next!=NULL) { q=p; p=p->next; } if(p->date==dates) return q; else return NULL; } struct linknode *entertohead(struct linknode *head,int dates)//在头指针前加数据 { struct linknode *enter; enter=(struct linknode *)malloc(sizeof(struct linknode)); if(enter==NULL) printf("allocate error!\n"); enter->date=dates; enter->next=NULL; if(head==NULL) head=enter; else { enter->next=head; head=enter; } return head; } struct linknode *entertotail(struct linknode *head,int dates)//在链表尾加数据 { struct linknode *enter,*p; p=head; enter=(struct linknode*)malloc(sizeof(struct linknode)); if(enter==NULL) printf("allocate error!\n"); enter->date=dates; enter->next=NULL; if(head==NULL) head=enter; else { while(p->next) { p=p->next; } p->next=enter; } return head; } struct linknode *entertovalue(struct linknode*head,int value,int dates)//特定位置加节点 { struct linknode *enter,*fp,*p; p=findahead(head,value); fp=find(head,value); if(fp==NULL) printf("no number is value\n"); enter=(struct linknode*)malloc(sizeof(struct linknode)); enter->date=dates; enter->next=NULL; if(enter==NULL) printf("allocate error\n"); else { if(fp==head) { enter->next=head; head=enter; } else { p->next=enter; enter->next=fp; } } return head; } struct linknode *delet(struct linknode *head,int dates)//删除节点 { struct linknode *delet,*p; p=findahead(head,dates); delet=find(head,dates); if(!delet) return NULL; else { p->next=delet->next; free(delet); return head; } } void main()//测试链表 { struct linknode *head,*fd; head=creat(); print(head); fd=findahead(head,4); if(fd==NULL) printf("ahead of value is NULL.\n"); else printf("\n%d\n",fd->date); print(head=entertohead(head,88)); printf("\n"); print(entertotail(head,77)); printf("\n"); head=delet(head,6); if(head) print(head); else printf("have no this number\n"); printf("\n"); }