在链表指定位置插入结点

问题描述 :

输入若干(不超过100个)非负整数,创建一个不带头结点的单向链表。
再输入一个位置index以及一个数据data,程序中首先创建一个新结点s,s的数据成员为data,然后调用函数insertNode将s插入到链表的指定位置index处,最后输出结果链表。
请编写insertNode函数,完成插入操作。insertNode函数的原型如下:


struct student *insertNode(struct student *head, struct student *s, int index)
形参:
struct student *head:链表的头指针,需要插入到这个链表中
struct student *s:指向需要插入的结点
int index:插入位置。index从1开始编号。如果链表中已有n个结点,则index的有效范围为 1<= index <= n+1。
返回值:
函数返回结果链表的头指针。

 

部分程序代码如下:
#include <stdio.h>
#include <stdlib.h>
struct student
{
 int  num;
 struct student  *next;
};
//从键盘读入数据创建链表,新结点插入到尾部
struct student *createByTail()
{
 struct student *head;
 struct student *p1,*p2;
 int n;
 n=0;
 p1=p2=(struct student*)malloc(sizeof(struct student));
 scanf("%d",&p1->num);
 head=NULL;  //首先置链表为空链表
&n

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值