1.单链表任意位置删除
datetype pos;printf("please input pos");scanf("%d",&pos);head=delete_all(head,pos);Output(head);
Linklist delete_all(Linklist head,datetype pos)
{if(pos<1||pos>length(head)||head==NULL)return head;if(head->next==NULL){free(head);head=NULL;}else{if(pos==1){head=delete_head(head);return head;}else{Linklist p=head;for(int i=1;i<pos-1;i++){p=p->next;}Linklist q=p->next;p->next=q->next;free(q);p=NULL;return head;}}
}
2.单链表任意位置修改
datetype pos;printf("please input pos");scanf("%d",&pos);printf("please input element");scanf("%d",&element);mod_all(head,pos,element);Output(head);
void mod_all(Linklist head,datetype pos,datetype element)
{Linklist p=head;if(head==NULL||pos<1||pos>length(head))return;for(int i=1;i<pos;i++){p=p->next;}p->date=element;
}
3.单链表任意位置查找
datetype pos;printf("please input pos");scanf("%d",&pos);find_all(head,pos);Output(head);
void find_all(Linklist head,datetype pos)
{Linklist p=head;if(head==NULL||pos<1||pos>length(head))return;for(int i=1;i<pos;i++){p=p->next;}printf("%d\n",p->date);
}
4.单链表任意元素查找
datetype key;printf("please input key");scanf("%d",&key);datetype index=find_ele(head,key);if(index==-1)printf("不存在\n");else{printf("在第%d的位置\n",index);}
int find_ele(Linklist head,datetype key)
{if(head==NULL)return -1;datetype count=0;Linklist p=head;for(int i=1;i<=length(head);i++){count++;if(p->date==key)return count;p=p->next;}return -1;
}
5.单链表任意元素修改
datetype key;printf("please input key");scanf("%d",&key);printf("please input element");scanf("%d",&element);mod_ele(head,key,element);Output(head);
int mod_ele(Linklist head,datetype key,datetype element)
{if(head==NULL)return -1;datetype pos=find_ele(head,key);Linklist p=head;for(int i=1;i<pos;i++){p=p->next;}p->date=element;
}
6.单链表任意元素删除
datetype key;printf("please input key");scanf("%d",&key);delete_ele(head,key);Output(head);
int delete_ele(Linklist head,datetype key)
{if(head==NULL)return -1;Linklist p=head;datetype pos=find_ele(head,key);for(int i=1;i<pos-1;i++){p=p->next;}Linklist q=p->next;p->next=q->next;free(q);q=NULL;return 0;
}
7.单链表逆置(面试)
head=nizhi(head);Output(head);
Linklist nizhi(Linklist head)
{if(head==NULL)return head;Linklist p=head->next;head->next=NULL;while(p){Linklist t=p;p=p->next;t->next=head;head=t;}return head;
}
8.单链表查找倒数第n个节点(面试)
printf("please input n:");scanf("%d",&n);back_node(head,n);
datetype back_node(Linklist head, datetype n)
{if(head==NULL||n<1||n>length(head))return -1;datetype pos=length(head)-n+1;find_all(head,pos);return 0;}
9.单链表排序(面试)
datetype flag;printf("printf input flag:(0|1)");scanf("%d",&flag);if(flag==1|flag==0){sort_list(head,flag);Output(head);}else{printf("input error\n");}
datetype sort_list(Linklist head,datetype flag)
{if(head==NULL)return -1;Linklist p=head;if(flag==1){for(int i=0;i<length(head)-1;i++){p=head;for(int j=0;j<length(head)-i-1;j++){if(p->date > p->next->date){p->date ^= p->next->date;p->next->date ^= p->date;p->date ^= p->next->date;}p=p->next;}}}else{for(int i=0;i<length(head)-1;i++){p=head;for(int j=0;j<length(head)-i-1;j++){if(p->date < p->next->date){p->date ^= p->next->date;p->next->date ^= p->date;p->date ^= p->next->date;}p=p->next;}}}return 0;
}
10.单链表释放内存
head=free_list(head);
Linklist free_list(Linklist head)
{if(head==NULL)return head;Linklist p=head;while(head){p=head;head=head->next;free(p);}p=NULL;return head;
}