单链表的元素插入算法

单链表的元素插入算法图片有点模糊谢谢帮我作答

第1个回答  2018-05-18
#include <stdlib.h>#include<stdio.h>typedef struct LNode{
int data;
LNode *next;
}*List,LNode;void Creat(List &L,int n){//创建链表
List p;//用于循环创建的节点
L=(List)malloc(sizeof(struct LNode));
L->next=NULL;
for(int i=1;i<=n;i++){
p=(List)malloc(sizeof(struct LNode));
scanf("%d",&p->data);
p->next =L->next;
L->next=p;
}
}//创建成功void Print(List L3){
L3=L3->next;
while(L3){
printf("%d",L3->data);
L3=L3->next;
}
}void Insert(List &L,int i,int e){//在第i个位置之前插入e
List p,s;
p=L;
int j=0; while(p&&j<=i-1){
if(j==i-1){
s=(List)malloc(sizeof(struct LNode));
s->data=e;
s->next=p->next;
p->next=s;
}
j++;
p=p->next;
}
}

int main(){ List L;
int n,i,e;
scanf("%d,%d,%d",&n,&i,&e);
Creat(L,n);
Insert(L,i,e);
Print(L); return 0;
}1本回答被网友采纳