读书人

快疯了 链表有关问题 百思不得其解

发布时间: 2012-10-25 10:58:58 作者: rapoo

快疯了 链表问题 百思不得其解
#include<iostream>
#include<string>
using namespace std;
class Student
{
int enroll;
char sex;
string name;
public:
Student();
Student(int,char,string);
void print();
void add();
};
Student::Student()
{
enroll =12345;
sex = 'm';
name = "yuchang";
}
Student::Student(int a,char b, string c)
{
enroll = a;
sex = b;
name = c;
}
void Student::print()
{
cout<<"姓名:"<<name<<" "<<"性别:"<<sex<<" "<<"学号:"<<enroll<<endl;
}
void Student::add()
{
cout<<"请输入姓名:"<<endl;
cin>>name;
cout<<"请输入性别 "<<endl;
cin>>sex;
cout<<"请输入学号 "<<endl;
cin>>enroll;
}
struct node
{
Student s;
node* next;
};
class Student_Node
{

node* head;//头结点
public:
Student_Node();
void add();//添加
void del(Student& s);//删除
void insert(Student&,Student&);//插入
void display();//显示
};
Student_Node::Student_Node()
{

node* p ;
p = new node[20];
p->next=p;
head = p;
return;
}
void Student_Node::add()
{
node* p;
p = new node[50];
p=head->next;
while(p!=head)//不是头结点就往后走 直到最后一个节点
{
p=p->next;
}
p= p->next;
p->s.add();
p->next=head;

}
void Student_Node::display()
{
node* p;
p=head->next;
if(p == head)
{
cout<<"空循环链表"<<endl;
}
while(p!=head)
{
p->s.print();
p=p->next;

}
return;
}
int main()
{
Student_Node sn;
sn.add();
sn.add();
sn.display();
}

[解决办法]
while(p!=head)//不是头结点就往后走 直到最后一个节点
{
p=p->next;
} 指飞了
[解决办法]
看错 。。
[解决办法]
C++没学过 看的真心累人 建议添加的结点时候不要还叫p 叫q吧
还有while(p->next!=head)//不是头结点就往后走 直到最后一个节点
{
p=p->next;
}

读书人网 >C++

热点推荐