标题:数据结构知识,画红色的求解释。。
取消只看楼主
l2480035288
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-3-15
结帖率:73.33%
已结贴  问题点数:4 回复次数:0 
数据结构知识,画红色的求解释。。
public class LinkList {
        
        protected int count;   //识别链表长度
        protected LinkListElement head;   //表示链表头
        public LinkList(){
            head=null;
            count=0;
            
        }
        public boolean isEmpty(){  //判断链表是否为空;
            return count==0;
        }
        
        public void addToTail(Object value){
            LinkListElement temp=new LinkListElement(value,null);
            if(head!=null){
                LinkListElement finger=head;
                while(finger.next()!=null){            //遍历链表,找尾节点
                    finger=finger.next();                    
                }
                finger.setNext(temp);
            }
            else head=temp;                         //如果为空,
            count++;
        }
        
        public Object remove(Object value){
            LinkListElement finger=head;
            LinkListElement previous=null;              //指向删除位置的前一个节点;
            while(finger!=null&&!finger.value().equals(value)){         // 节点的值为要删除的值,
                previous=finger;              
                finger=finger.next();                 //遍历链表
            }
            if(finger!=null){                        //查找成功
                if(previous==null){
                    head=finger.next();                   //要删除的为头节点
                }else{
                    previous.setNext(finger.next());
                }
                count--;
                return finger.value();
            }
            return null;
        }
        
    }

import java.util.Scanner;
public class Student extends LinkList
{
    int Room;
    int Id;
    protected String Name;
    protected int Age;
    protected String Sex;
    protected Student(String name,String sex,int age,int id,int room)
    {
        Room=room;
        Name=name;
        Id=id;
        Age=age;
        Sex=sex;
    }
    public int getage() {
        return Age;
    }
    public void setAge(int age) {
        Age = age;
    }
    public String getname() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public int getId() {
        return Id;
    }
    public void setId(int id) {
        Id = id;
    }
    public String getsex() {
        return Sex;
    }
    public void setSex(String sex) {
        Sex = sex;
    }
    public int getroom() {
        return Room;
    }
    public void setRoom(int room) {
        Room = room;
    }
    public Student()throws Exception{
        
        Scanner cin=new Scanner(System.in);
        System.out.println("输入宿舍号");
        Room=cin.nextInt();
        System.out.println("输入学生学号");
        Id=cin.nextInt();
        System.out.println("输入学生姓名");
        Name=cin.next();
        System.out.println("输入学生年龄");
        Age=cin.nextInt();
        System.out.println("输入学生性别");
        Sex=cin.next();
        
    }
}
public class D extends LinkList  implements Manger{

    public static void main(String[] args) throws Exception {
        // TODO 自动生成的方法存根
    D n=new D();
    Scanner cin=new Scanner(System.in);
    while(true){
        System.out.println("--你要执行的功能:--");
        System.out.println("1:添加一个宿舍人员");
        System.out.println("2:删除宿舍人员");
        System.out.println("3:查找一个宿舍人员");
        System.out.println("4:显示信息");
        System.out.println("99:退出系统");
        int number=cin.nextInt();
         
        if(number==1){
            Student a=new Student();
            n.add(a);        
        }else if(number==2){
            System.out.println("请输要删除学生的名字");
            String name;
            name=cin.next();
            n.remove(name);
            
        }
        else if(number==3)
        {
            System.out.println("请输入要查找学生的名字");
            String name2;
            name2=cin.next();
            n.find(name2);
            }
        else if(number==4)
        {
            n.list();
        }
        else if(number==99)
        {
                System.out.println("程序结束");    //正常退出程序;
                System.exit(0);
        }
        n.list();
    }
    }

    @Override
    public void add(Student s) throws Exception {
        
        addToTail(s);
        System.out.println("成功添加: 学号:"+s.getId()+" 姓名:"+s.getname()
                +" 年龄:"+s.getsex()+" 性别:"+s.getage()+"  宿舍号:"+s.getroom());

        // TODO 自动生成的方法存根
        
    }

    @Override
    public void remove(String name) {
        LinkListElement finger=head;
        while(finger!=null){
            Student s=(Student)finger.value();
            if(s.getname().equals(name)){
                remove(s);
                System.out.println("成功删除: 学号:"+s.getId()+" 姓名:"+s.getname()
                        +" 年龄:"+s.getsex()+" 性别:"+s.getage()+"  宿舍号:"+s.getroom());
                break;
            }else{
                finger=finger.next();
            }
            }
        
        }
搜索更多相关主题的帖子: Object public return count null 
2013-12-30 21:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-425917-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.252951 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved