0

When I learn the queue, I found some problem, here is my code and the result.I hope someone can help me solve the problem, thank you! import java.util.Scanner;

public class Test{
    public static void main(String[] args){
        SQType sq = new SQType();
        Data data1;
        Scanner in = new Scanner(System.in);
        SQType s = sq.SQInit();                         //初始化队列

        System.out.println("入队列操作:");
        System.out.println("输入姓名 年龄进行操作:");
        do{
            Data data = new Data();
            data.name = in.next();
            data.age = in.nextInt();
            if(data.name.equals("0")){
                break;
            }
            else{
                sq.InSQType(s,data);
            }
        }while(true);

        String temp;
        System.out.println("出队列操作,按任意非0键继续:");
        temp = in.next();
        while(!temp.equals("0")){
            data1 = sq.OutSQType(s);
            System.out.printf("出队列的数据为(%s %d)",data1.name, data1.age);
            temp = in.next();
        }

        System.out.println("测试结束!");
        sq.SQTypeFree(s);
    }
}

class Data{
    String name;
    int age;
}

class SQType{
    private static final int QUEUELEN = 15;
    private Data[] data = new Data[QUEUELEN];
    private int head;
    private int tail;

    SQType SQInit(){
        SQType s;
        if((s = new SQType()) != null){                 //申请内存
            s.head = 0;                                 //设置队头
            s.tail = 0;                                 //设置队尾
            return s;
        }
        else{
            return null;
        }
    }

    int SQTypeIsEmpty(SQType s){                        //判断是否为空队列
        int temp = 0;
        if(s.head == s.tail){
            temp = 1;
        }
        return temp;
    }

    int SQTypeIsFull(SQType s){                         //判断是否为满队列
        int temp = 0;
        if(s.tail == QUEUELEN){
            temp = 1;
        }
        return temp;
    }

    void SQTypeClear(SQType s){                         //清空队列
        s.head = 0;
        s.tail = 0;
    }

    void SQTypeFree(SQType s){
        if(s != null){
            s = null;
        }
    }

    int InSQType(SQType s, Data data){                  //数据入队列
        if(s.tail == QUEUELEN){
            System.out.println("队列已满!操作失败!");
            return 0;
        }
        else{
            s.data[s.tail++] = data;
            return 1;
        }
    }

    Data OutSQType(SQType s){                           //数据出队列
        if(s.head == 0){
            System.out.println("队列为空!操作失败!");
            return null;
        }
        else {
            return s.data[s.head++];
        }
    }

    Data PeekSQType(SQType s){                          //读取队列数据
        if(SQTypeIsEmpty(s) == 1){
            System.out.println("空队列!");
            return null;
        }
        else{
            return s.data[s.head];
        }
    }

    int SQTypeLen(SQType s){                            //计算队列长度
        int temp;
        temp = s.tail - s.head;
        return temp;
    }
}

Here is the result, the queue can read in the data, but can't output the data enter image description here

Lee
  • 27
  • 4
  • 3
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Paolo Forgia Apr 11 '17 at 08:43
  • 1
    Can you put your code as text in the question instead of these images? – Robin Topper Apr 11 '17 at 08:44

1 Answers1

0

In the OutSQType, the code in the if-statement should be changed to "s.head == s.tail", or it will always return null

Lee
  • 27
  • 4