#include "stdafx.h"
#include "iostream.h"
#include "string.h"
#include "stdio.h"
#include <stdlib.h>typedef struct LNode
{
char Data;
struct LNode *next;
}LNode,*Linklist;
//创建链表
void CreateList(Linklist &L)
{
char datas;
Linklist cur,newp;
L=(Linklist) malloc (sizeof(LNode));
// L= new LNode;
L->next = NULL;
cur=L;
while(1)
{
cin>>datas;
if (datas == '8')
{
break;
}
else
{
//newp = new LNode;
newp=(Linklist) malloc (sizeof(LNode));
newp->Data = datas;
cur->next = newp ;
cur = newp;
}
}
}//显示链表
void Display(Linklist L)
{
Linklist P;
P = L->next ;
while (P != NULL)
{
cout<<P->Data ;
P=P->next;
}
}void main()
{
Linklist LL;
LL = NULL;
CreateList(LL);
Display(LL);
system("pause");
}运行时,
我在控制台输入
1
2
8
当数入 8 后,
程序在 显示链表的
cout<<P->Data ;
出现这样的错误:
Unhandled exception in mypro.exe:0xC0000005 : Access Violation
各位大哥,我这个程序是错误在哪里呢?谢谢!
#include "iostream.h"
#include "string.h"
#include "stdio.h"
#include <stdlib.h>typedef struct LNode
{
char Data;
struct LNode *next;
}LNode,*Linklist;
//创建链表
void CreateList(Linklist &L)
{
char datas;
Linklist cur,newp;
L=(Linklist) malloc (sizeof(LNode));
// L= new LNode;
L->next = NULL;
cur=L;
while(1)
{
cin>>datas;
if (datas == '8')
{
break;
}
else
{
//newp = new LNode;
newp=(Linklist) malloc (sizeof(LNode));
newp->Data = datas;
cur->next = newp ;
cur = newp;
}
}
}//显示链表
void Display(Linklist L)
{
Linklist P;
P = L->next ;
while (P != NULL)
{
cout<<P->Data ;
P=P->next;
}
}void main()
{
Linklist LL;
LL = NULL;
CreateList(LL);
Display(LL);
system("pause");
}运行时,
我在控制台输入
1
2
8
当数入 8 后,
程序在 显示链表的
cout<<P->Data ;
出现这样的错误:
Unhandled exception in mypro.exe:0xC0000005 : Access Violation
各位大哥,我这个程序是错误在哪里呢?谢谢!
if (datas == '8')
{
cur->next = NULL; //加入这条语句
break;
}