问题遇到的现象和发生背景
请教一下各位 为什么我的单链表在使用头插法创建后,打印出来是乱码
代码
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode {
struct LNode* next;
int data;
}LNode, * LinkList;
LinkList HeadInsert(LinkList& L)
{
LNode* p; int x;
L = (LinkList)malloc(sizeof(LNode)); //创建头结点
L->next = NULL;
scanf_s("%d", &x);
while (x !=9999) //输入9999时则结束插入
{
p = (LinkList)malloc(sizeof(LNode));
p->data = x;
p->next = L->next;
L->next = p;
scanf_s("%d", &x);
}
return L;
}
int main()
{
LinkList L;
HeadInsert(L);
while (L->next!=NULL)
{
printf("%d\n",&L->data);
L = L->next;
}
}
#include<stdlib.h>
typedef struct LNode {
struct LNode* next;
int data;
}LNode, * LinkList;
LinkList HeadInsert(LinkList& L)
{
LNode* p; int x;
L = (LinkList)malloc(sizeof(LNode)); //创建头结点
L->next = NULL;
scanf_s("%d", &x);
while (x !=9999) //输入9999时则结束插入
{
p = (LinkList)malloc(sizeof(LNode));
p->data = x;
p->next = L->next;
L->next = p;
scanf_s("%d", &x);
}
return L;
}
int main()
{
LinkList L;
HeadInsert(L);
while (L->next!=NULL)
{
printf("%d\n",&L->data);
L = L->next;
}
}