//插入结点 #include<stdio.h> #include<stdlib.h> #include<stdbool.h> typedef struct LNode{ int data; //定义链表数据域 struct LNode *next; //定义后继节点 }LNode,*Linklist; //尾插法 Linklist Creat_list(int count){ Linklist head = (Linklist)malloc(sizeof(LNode)); //为头指针开辟内存 Linklist node = NULL; //定义工作指针 Linklist end = NULL; //定义尾指针 head->next = NULL; //初始化头指针的下一个为空 end = head; int data; for(int i=0;i<count;i++){ scanf("%d",&data); node = (LNode*)malloc(sizeof(LNode)); //为新结点开辟内存 node->data=data; //新结点数据域赋值 end->next=node; //新结点赋值给尾结点的下一个 end=node; //重新定义尾结点 } end->next=NULL; return head; } //插入结点 bool add(LNode *L,int addn){ LNode *p=L,*q=L->next; LNode *node; int j=0; while(p && j<addn){ p=q; q=q->next;++j; } if(!p && j>addn) return false; node = (LNode*)malloc(sizeof(LNode)); node->data = addn; node->next = p->next; p->next = node; return true; } void output(Linklist head){ Linklist a = head->next; while(a){ printf("%d",a->data); printf(" "); a=a->next; } } int main(){ int n,m; scanf("%d",&n); scanf("%d",&m); LNode* head = Creat_list(n); add(head,m); output(head); }