问题遇到的现象和发生背景
C,请问这段程序的段错误(41行)是怎么回事
用代码块功能插入代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int a[6]={0};
typedef struct node
{
int data;
struct node *next;
}NODE;
/*用个数组保存输入的各个数字*/
void input()
{
for(int i=0;i<6;i++)
{
printf("Enter:");
scanf("%d",&a[i]);
}
}
/*尾插法建立链表*/
NODE * create()
{
int i=0;
NODE *header,*process=NULL,*rear=NULL;
while(1)
{
process=(NODE *)malloc(sizeof(NODE));
process->data=a[i];
if(process->data==0)
break;
process->next=NULL;
if(header==NULL)
{
header=process;
rear=process;
}
else
{
rear->next=process;//这里它提示我Segmentation fault
rear=process;
}
i++;
}
return header;
}
/*找对应位置上的值*/
int reNth(NODE *h, int n)
{
int i=0;
while(h!=NULL)
{
i++;
if(i==n)
{
return h->data;
}
h=(h->next);
}
}
int main()
{
NODE *s;
int number;
input();
printf("order\n");//输入要找的位置
scanf("%d",&number);
s=create();
printf("%d",reNth(s,number));
return 0;
}
运行结果及报错内容
没有结果:(
我想要达到的结果
我用的尾插法建立链表;
题目要求用遍历的方式找对应位置上的数字;
_该函数用于从键盘读入若干个整数(以0表示结束),按照正序创建链表,返回头指针。
编写一个函数,判断一个给定链表是否为有序的。假设函数原型为:
int isOrder(Node *h);
如果链表内容按升序排列,函数返回1;否则,函数返回0。
请编写一个函数,从给定的整数链表中找出指定整数。假设函数原型为:
Int reNth(NODE *h, int n);
假设h为链表表头指针,要求从链表中找出倒数第n个整数,返回该整数;链表元素个数小于n,则返回-1。_