suchen

Heim  >  Fragen und Antworten  >  Hauptteil

C++的队列问题

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

<code>C</code><code>#include<iostream>

using namespace std;

struct node

{

    node *next;

    char data;

    node *front;

    node *rear;

};

node InitQueue();

int main()

{

    InitQueue();

    return 0;

}

node InitQueue()

{

    char temp;

    int i=0;

    node *Q;

    Q->front=Q->rear=new node;

    if(!Q->front)

    {

        cout<<"出错"<<endl;

    }

    Q->front->next=NULL;

    do

    {

        cin>>temp;

        if(temp=='$')

        {

            char e;

            if(Q->front==Q->rear)

            {

                cout<<"³ö´í!"<<endl;

            }

            node *Z;

            Z=Q->front->next;

            e=Z->data;

            Q->front->next=Z->next;

            cout<<e<<':'<<i<<'s'<<endl;

        }

        else if(temp!='#')

        {

            node *P=new node;

            P->data=temp;

            P->next=NULL;

            Q->rear->next=P;

            Q->rear=P;

            i++;

        }

    }while(temp!='#');

}

</code>

这是用队列来解决的食堂问题
这个代码的正确写法我知道了
但就是想问问这个为什么错,也不能说错吧,用codeblock运行之后就崩溃了,编译能通过
求指教!谢谢。。

阿神阿神2895 Tage vor674

Antworte allen(2)Ich werde antworten

  • PHPz

    PHPz2017-04-17 11:55:47

    1

    2

    <code>node *Q; 指针没初始化直接使用 不崩溃才奇怪

    </code>

    Antwort
    0
  • 天蓬老师

    天蓬老师2017-04-17 11:55:47

    1

    2

    3

    <code> node *Q = (node*)malloc(sizeof(node));

     Q->front=Q->rear=Q;

    </code>

    Antwort
    0
  • StornierenAntwort