- ·上一篇文章:链表的c语言实现(三)
- ·下一篇文章:链表的c语言实现(五)
链表的c语言实现(四)
2、插入(后插)假设在一个单链表中存在2个连续结点p、q(其中p为q的直接前驱),若我们需要在p、q之间插入一个新结点s,那么我们必须先为s分配空间并赋值,然后使p的链域存储s的地址,s的链域存储q的地址即可。(p->link=s;s->link=q),这样就完成了插入操作。下例是应用插入算法的一个例子:#include <stdio.h>#include <malloc.h>#include <string.h>#define N 10typedef struct node{char name[20];struct node *link;}stud;stud * creat(int n) /*建立单链表的函数*/{stud *p,*h,*s;int i;if((h=(stud *)malloc(sizeof(stud)))==NULL){printf("不能分配内存空间!");exit(0);}h->name[0]='\0';h->link=NULL;p=h;for(i=0;

