#include<iostream.h>void main()
{
struct node //定义了一个拥有三个
{ //成员的结构体
long num;
float score;
struct node *next;
} *p,*student=0;
//int n;
long num1;
float score1;
//p=student;
//for(int i=1;i<4;i++);
int i=1;
while (i<4)
{
student=new node;
cout<<"please input num \n";
cin>>num1;
cout<<endl;
student->num=num1;
cout<<"please input score \n";
cin>>score1;
cout<<endl;
if (i==1) {p=student;} //p是头结点
student->score=score1;
student=student->next;
cout<<"is" <<'\t'<<i<<endl;
i++;
}
//student->next=0; //如果这句用的话,程序在这句出错
student=0;
cout<<"over"<<endl;
while (p!=0) //程序运行下面语句一遍后出错
{cout<<p->num<<'\t'<<p->score<<"OK"<<endl;
p=p->next;
cout<<"haha"<<endl;
}
}系统提示出错信息:
“0x00414e83”指令引用的“0xcdcdcdd1”内存。
该内存不能为“read”。
{
struct node //定义了一个拥有三个
{ //成员的结构体
long num;
float score;
struct node *next;
} *p,*student=0;
//int n;
long num1;
float score1;
//p=student;
//for(int i=1;i<4;i++);
int i=1;
while (i<4)
{
student=new node;
cout<<"please input num \n";
cin>>num1;
cout<<endl;
student->num=num1;
cout<<"please input score \n";
cin>>score1;
cout<<endl;
if (i==1) {p=student;} //p是头结点
student->score=score1;
student=student->next;
cout<<"is" <<'\t'<<i<<endl;
i++;
}
//student->next=0; //如果这句用的话,程序在这句出错
student=0;
cout<<"over"<<endl;
while (p!=0) //程序运行下面语句一遍后出错
{cout<<p->num<<'\t'<<p->score<<"OK"<<endl;
p=p->next;
cout<<"haha"<<endl;
}
}系统提示出错信息:
“0x00414e83”指令引用的“0xcdcdcdd1”内存。
该内存不能为“read”。
这是必然的,此时student指向的是oxcdcdcdcd,因为i = 4时循环已经跳出
student没有做new的操作
while (p!=0) //程序运行下面语句一遍后出错
{cout<<p->num<<'\t'<<p->score<<"OK"<<endl;
p=p->next;
cout<<"haha"<<endl;
}
这时的p指向的是你认为创建的链表的最后一个节点,因为
if (i==1) {p=student;} //p是头结点
这句话使得p和student指向同一块地址,而后面
student=student->next;
在改变student的同时也改变了p
struct Student
{
long number;
float score;
Student *next;
};Student *head;
Student *Creat()
{
Student *ps;
Student *pEnd;
ps=new Student;
cin>>ps->number>>ps->score;
head=NULL;
pEnd=ps;//避免野指针
while(ps->number!=0)
{
if (head==NULL)
{
head=ps;
}
else
{
pEnd->next=ps;
pEnd=ps;
ps=new Student;
cin>>ps->number>>ps->score;
// ps->next=pEnd;
}
}
pEnd->next=NULL;
delete ps;
return (head);//找到链表的头指针然后返回
}void Showlist (Student *head)
{
cout<<"now the items of list are \n";
while (head)
{
cout <<head->number<<" , "<<head->score<<endl;
head=head->next;
}
}
{
tt *next;
};tt *head,*p;
head = new tt;
p = head;
for( int i = 0;i < 3;i++ )
{
p->next = new tt;
p = p->next;
}
p->next = NULL;
while( head )
{
head = head->next;
}
{
struct node //定义了一个拥有三个
{ //成员的结构体
long num;
float score;
struct node *next;
} *p,*student=0;
//int n;
long num1;
float score1;
//p=student;
//for(int i=1;i<4;i++);
int i=1;
while (i<4)
{
student=new node;
cout<<"please input num \n";
cin>>num1;
cout<<endl;
student->num=num1;
cout<<"please input score \n";
cin>>score1;
cout<<endl;
if (i==1) {p=student;} //p是头结点
student->score=score1;
student=student->next;
cout<<"is" <<'\t'<<i<<endl;
i++;
}
//student->next=0; //如果这句用的话,程序在这句出错
student=0;
cout<<"over"<<endl;
while (p!=0) //程序运行下面语句一遍后出错
{cout<<p->num<<'\t'<<p->score<<"OK"<<endl;
p=p->next;
cout<<"haha"<<endl;
}
}系统提示出错信息:
“0x00414e83”指令引用的“0xcdcdcdd1”内存。
该内存不能为“read
在改变student的同时也改变了p看来对指针和链表不清楚
指针变量也是一个变量而已
student=student->next;
并不会改变了p 因为p和student是两个不同的变量 其内容是地址
p=student; 的意思是在p存储了和student相同的内容(即地址)楼主原来的程序
一 根本没形成链表
结果是这样的
p->第一个节点
第二个节点
第三个节点
根本没有链起来
二 就算形成了链表,以后的程序也不对