#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct st { int num; struct st * next; }; struct st * create(); struct st * insert(struct st * head); struct st * zeng(struct st * head); struct st * shan(struct st * head); struct st * nixu(struct st * head); struct st * search(struct st *head); int size (struct st * head); int main() { struct st * head,*p; int i=3; head=create(); insert(head); p=head; p=p->next ; while(p){ printf("%d ",p->num); p=p->next ; } printf("/n"); zeng(head); shan(head); p=head; p=p->next ; while(p){ printf("%d ",p->num); p=p->next ; } printf("/n"); printf("逆序之后:/n"); nixu(head); p=head; p=p->next ; while(p){ printf("%d ",p->num); p=p->next ; } printf("/n"); printf("删除重复节点:/n"); search(head); p=head; p=p->next ; while(p){ printf("%d ",p->num); p=p->next ; } return 0; } struct st * nixu(struct st * head) { struct st * p,*s; p=head; p=p->next ; head->next =NULL; while(p) { s=p; p=p->next; s->next =head->next ; head->next =s; } return 0; } struct st * shan(struct st * head) { struct st * q,*p; int n; printf("请输入要删除的节点:/n"); scanf("%d",&n); int j=0; p=head; while(p&&j<n-1){ p=p->next ; j++; } q=p->next ; p->next =q->next ; free(q); return 0; } struct st * zeng(struct st * head) { struct st * p,* q; int j=0,n; printf("输入要插入的位置:/n"); scanf("%d",&n); int i; i=size(head); if(0<n&&n<i+1) { p=head; while(p&&j<n-1){ p=p->next ; j++; } printf("输入要插入的数:/n"); q=(struct st *)malloc(sizeof(st)); scanf("%d",&q->num); q->next =p->next ; p->next =q; p=p->next ; p=head; p=p->next ; while(p){ printf("%d ",p->num); p=p->next ; } printf("/n"); } else{ printf("输入有误/n"); } return 0; } struct st * create() { struct st * head=NULL; head=(struct st * )malloc(sizeof(st)); return head; } struct st * insert(struct st * head) { printf("请输入号码(以-1结束):/n"); struct st *p=NULL,*q=NULL; int i; q=head; p=(struct st *)malloc(sizeof(st)); while(scanf("%d",&p->num)!=EOF) { if(p->num==-1) break; q->next=p; q=p; p=(struct st *)malloc(sizeof(st)); }q->next=NULL; return 0; } struct st * search(struct st *head) { struct st *p,*q,*r; p=head->next; while(p){ q=p; while(q->next){ if(q->next->num==p->num) { r=q->next; q->next =r->next ; free(r); } else{ q=q->next ; } }p=p->next ; } return 0; } int size(struct st * head) { int i=0; struct st * p; p=head; p=p->next ; while(p) { i++; p=p->next ; } return i; }