//链表的练习
import java.lang.*;
class Student{
int no;
String name;
double score;
Student next; //对next对象的引用
void show(){ //输出
System.out.println(no+" "+name+" "+score);
}
void put(int i,String s,double d){ //输出
no=i;
name=s;
score=d;
}
}class StudentList{
Student head;
StudentList(int len){ //constructor
Student p;
head=p=new Student();
for(int i=1;i<len;i++){
p.next=new Student();
p=p.next;
}
}
void showAll(){
Student p;
for(p=head.next;p.next!=null;p=p.next){
p.show();
}
}
void putAll(){
Student p=head.next;
p.put(1001,"n1",70.6);
p=p.next;
p.put(1002,"n2",80.5);
p=p.next;
p.put(1003,"n3",90.5);
}
void insert(int a,Student b){ //插入操作
Student p,q;
if(head.next==null) //空表
head.next=b;
else if(head.next.no==a){ //a作为第一个结点
b.next=head.next;
head.next=b;
}
else{ //search a
p=q=head;
while(p.no!=a&&p.next!=null){
q=p;
p=p.next;
}
if(p.no==a){ //找到a了
b.next=p;
q.next=b;
}
else
p.next=b; //a不在链表中,把b插在表尾
b.next=null;
}
}
void earse(int a){
Student p,q;
if(head.next==null)
System.out.println("null list");
else if(head.next.no==a){ //删除第一个结点
head.next=head.next.next;
}
else{ //查找a
p=q=head;
while(p.no!=a&&p.next!=null){
q=p;
p=p.next;
}
if(p.no==a){
q.next=p.next;
}
else
System.out.println("no this node");
}
}
}
class LB{
public static void main(String args[]){
StudentList s=new StudentList(3);
s.putAll();
s.showAll();
System.out.println();
Student b=new Student();
b.put(1005,"n5",87.5);
s.insert(1002,b);
s.showAll();
System.out.println();
s.earse(1002);
s.showAll();
}
}
//编译通过了,运行时老报错误
import java.lang.*;
class Student{
int no;
String name;
double score;
Student next; //对next对象的引用
void show(){ //输出
System.out.println(no+" "+name+" "+score);
}
void put(int i,String s,double d){ //输出
no=i;
name=s;
score=d;
}
}class StudentList{
Student head;
StudentList(int len){ //constructor
Student p;
head=p=new Student();
for(int i=1;i<len;i++){
p.next=new Student();
p=p.next;
}
}
void showAll(){
Student p;
for(p=head.next;p.next!=null;p=p.next){
p.show();
}
}
void putAll(){
Student p=head.next;
p.put(1001,"n1",70.6);
p=p.next;
p.put(1002,"n2",80.5);
p=p.next;
p.put(1003,"n3",90.5);
}
void insert(int a,Student b){ //插入操作
Student p,q;
if(head.next==null) //空表
head.next=b;
else if(head.next.no==a){ //a作为第一个结点
b.next=head.next;
head.next=b;
}
else{ //search a
p=q=head;
while(p.no!=a&&p.next!=null){
q=p;
p=p.next;
}
if(p.no==a){ //找到a了
b.next=p;
q.next=b;
}
else
p.next=b; //a不在链表中,把b插在表尾
b.next=null;
}
}
void earse(int a){
Student p,q;
if(head.next==null)
System.out.println("null list");
else if(head.next.no==a){ //删除第一个结点
head.next=head.next.next;
}
else{ //查找a
p=q=head;
while(p.no!=a&&p.next!=null){
q=p;
p=p.next;
}
if(p.no==a){
q.next=p.next;
}
else
System.out.println("no this node");
}
}
}
class LB{
public static void main(String args[]){
StudentList s=new StudentList(3);
s.putAll();
s.showAll();
System.out.println();
Student b=new Student();
b.put(1005,"n5",87.5);
s.insert(1002,b);
s.showAll();
System.out.println();
s.earse(1002);
s.showAll();
}
}
//编译通过了,运行时老报错误
// 更改!否则p多指了一条记录,而这记录还未new Student
Student p=head;
p.put(1001,"n1",70.6);
p=p.next;
p.put(1002,"n2",80.5);
p=p.next;
p.put(1003,"n3",90.5);
}
(1003,"n3",90.5); 然后再插入1005这个结点。 现在运行的结构是1002 1005 1002 和我设想的不一样,请问我哪里写错了? 感谢!
void showAll(){
Student p;
for(p=head.next;p.next!=null;p=p.next){
p.show();
}
}
有点问题,应把for(p=head.next;p.next!=null;p=p.next)
写成for(p=head;p!=null;p=p.next)
这样打印就不会出问题了,你再看看是不是你要的结果
按你程序的意识(我觉得)你是要设计一个带空头节点的链表
而”yuzl32(Hello!有酒醉)“的修改是不带头节点的,我楼上的是带头接点的
我猜想你是要设计带头接点的链表所以我对你的程序做了如下修改:
1。
StudentList(int len){ //constructor
Student p;
head=p=new Student();
for(int i=1;i<=len;i++){ //修改i<len
p.next=new Student();
p=p.next;
}
}
//len代表的是链表中实际的接点数,不包括头接点
2.
void showAll(){
Student p;
for(p=head.next;p!=null;p=p.next){//////////////p.next!=null
p.show();
}
}
//p指向当前打印的接点,只有当p=null时才不退出
3.
void insert(int a,Student b){ //插入操作
Student p,q;
if(head.next==null) //空表
head.next=b;
else
{
for(p=head.next,q=head;p!=null&&p.no!=a;q=p,p=p.next);
b.next=p;
q.next=b;
}
}
///我觉得这个算法没有必要那么复杂--------------不知道我的修改是不是你想要的,不过一定可以实现你想要的功能-----------