链表的插入操作
作者:互联网
//链表的插入操作
#include<stdio.h>
#include<stdlib.h>
typedef struct slist
{
int num;
struct slist *next;
}list;
void creatlast(list *l,int a[],int n)//链表的建立
{
list *s,*r;
int i;
l = (list *)malloc(sizeof(list));
l->next = NULL;
r = s;
for(i = 0; i < n; i++)
{
s = (list *)malloc(sizeof(list));
s->num = a[i];
r->next = s;
r = s;
}
r->next = NULL;
}
void insertelement(list *l,int x,int i)//先找到前驱结点
{
list *p;//定义为前驱结点
int k = 0;
list newl;//定义新结点
while(p != NULL && k != i - 1 )//找前驱结点
{
p = p->next ;
k++;
}
//开始插入
if(p == NULL)
{
printf("貌似找不到该结点 :\n");
}
else
{
newl = (list *)malloc(sizeof(list));//先分配空间
newl->next = p->next ;//先将要插入位置的结点地址赋给新结点指针域,也就是两个相连接
p->next = newl; //再将新结点的地址赋给前驱结点的指针域
}
}
标签:结点,newl,int,list,next,链表,插入,操作,NULL 来源: https://blog.csdn.net/qq_44653420/article/details/100901065