求助数据结构问题,请问下面这个代码为什么输出不了?
#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;
typedef struct node{
ElemType data;
struct node* next;
}linknode;
void Initlist(linknode *L, ElemType a[], int n)
{
linknode *s;
L = (linknode*)malloc(sizeof(linknode));
L->next = NULL;
for (int i = 1; i <= n; i++)
{
s = (linknode*)malloc(sizeof(linknode));
s->data = a[i];
s->next = L->next;//将阶段s插入到原来首结点之前,头结点之后
L->next = s;
}
}
ElemType Midnode(linknode *L,int k) {
int i;
linknode *p ;
for (p = L, i = 0; p&&i <= k - 1; i++)
{
p = p->next;
}
return (p->data);
}
int main()
{
linknode *L;
char ch;
ElemType a[20];
int n, k;
printf("请输入线性表个数:\n");
scanf("%d", &n);
printf("请输入%d个元素:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%c", &a[i]);
}
Initlist(L, a,n);
if (n % 2 == 1)
{
k = n / 2;
}
else
{
k = (n + 1) / 2;
}
ch = Midnode(L, k);
printf("线性表中间位置的值为%c", ch);
return 0;
}
#笔试题目##include<stdlib.h>
typedef char ElemType;
typedef struct node{
ElemType data;
struct node* next;
}linknode;
void Initlist(linknode *L, ElemType a[], int n)
{
linknode *s;
L = (linknode*)malloc(sizeof(linknode));
L->next = NULL;
for (int i = 1; i <= n; i++)
{
s = (linknode*)malloc(sizeof(linknode));
s->data = a[i];
s->next = L->next;//将阶段s插入到原来首结点之前,头结点之后
L->next = s;
}
}
ElemType Midnode(linknode *L,int k) {
int i;
linknode *p ;
for (p = L, i = 0; p&&i <= k - 1; i++)
{
p = p->next;
}
return (p->data);
}
int main()
{
linknode *L;
char ch;
ElemType a[20];
int n, k;
printf("请输入线性表个数:\n");
scanf("%d", &n);
printf("请输入%d个元素:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%c", &a[i]);
}
Initlist(L, a,n);
if (n % 2 == 1)
{
k = n / 2;
}
else
{
k = (n + 1) / 2;
}
ch = Midnode(L, k);
printf("线性表中间位置的值为%c", ch);
return 0;
}