问题描述 :
输入若干(不超过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

最低0.47元/天 解锁文章
5057

被折叠的 条评论
为什么被折叠?



