链表问题
#include "stdio.h"
#include "stdlib.h"
typedef struct Lnode
{
int data;
struct Lnode *next;
}Londe,*LinkList;
void main()
{
int i = 0;
LinkList L,p,q;
L = (LinkList)malloc(sizeof(Lnode));
if(L == NULL)
{
printf("申请内存空间失败!\n");
exit(0);
}
L->next = NULL;
q = L;
while(i < 5)
{
p = (LinkList)malloc(sizeof(Lnode));
if(p == NULL)
{
printf("申请内存空间失败\n");
exit(0);
}
p->next = NULL;
printf("请输入数据:\n");
scanf("%d",&p->next);
p->next = q->next;
q->next = p;
q = p;
i++;
}
p = L->next;
while(p)
{
printf("%d",p->data);
}
}
我想输入五个数,如:1、2、3、4、5 ,然后用链表的形式输出。但是结果出现了乱码。哪位高手帮忙看看出了什么问题?
#include "stdlib.h"
typedef struct Lnode
{
int data;
struct Lnode *next;
}Londe,*LinkList;
void main()
{
int i = 0;
LinkList L,p,q;
L = (LinkList)malloc(sizeof(Lnode));
if(L == NULL)
{
printf("申请内存空间失败!\n");
exit(0);
}
L->next = NULL;
q = L;
while(i < 5)
{
p = (LinkList)malloc(sizeof(Lnode));
if(p == NULL)
{
printf("申请内存空间失败\n");
exit(0);
}
p->next = NULL;
printf("请输入数据:\n");
scanf("%d",&p->next);
p->next = q->next;
q->next = p;
q = p;
i++;
}
p = L->next;
while(p)
{
printf("%d",p->data);
}
}