2011-07-27 2 views
0

링크 된 목록을 작성한 다음 동적 스택으로 바꾼 다음이를 동적 대기열로 전환해야했습니다. 모든 것이 "dequeuing"을 제외하고 제대로 작동하는 것처럼 보입니다. 끝내려는 프로그램과 마찬가지로 "LinkedList_Stack_BNS11.exe [4972]에서 처리되지 않은 win32 예외가 발생했습니다."라는 오류 메시지가 나타납니다.LinkedList/Stack/Queue - 큐 꺼내기 도움말

프로그램을 단계별로 실행하거나 실행하면 그 부분까지 원활하게 실행되므로 포인터 중 하나를 잘못 입력했기 때문에 큐를 꺼내는 것만 가정합니다.

출력 :

0

1

5 개 항목 Enquing

.... // 큐에

값이었다 (대기열에서 제외)을 Finsihes

// 정확한 숫자는 ...

// 프로그램에서 오류가 발생했습니다. 그것이 끝나고 닫힐 때.

나는 너무 많은 코드가 나를 알고 내가 도움을 사전에

감사합니다 (아래의 모든 물건의 바로 중간에) 그냥 "큐 해제"로 자르게됩니다하자가 포함 된 경우! ! 나는 내가 잘못한 것을 보지 않고있다. 그것이 "머리"가 가리키는 곳과 관련이 있다고 생각합니까? Idk.

헤더 파일 :

class NumberList 
{ 
private: 
    // 
    struct ListNode 
    { 
     int value; // Value in this node 
     struct ListNode *next; // Pointer to the next node 
    }; 

    ListNode *head; // List head pointer 
    ListNode *rear; 

public: 
    //Constructor 
    NumberList() 
    { head = NULL; rear = NULL; } 

    //Destructor 
    ~NumberList(); 

    //Stack operations 
    bool isEmpty(); 

    //Queue operations 
    void enqueue(int); 
    void dequeue(int &); 
}; 
#endif 

List_Stack_Queue.cpp : 주요

bool NumberList::isEmpty() 
{ 
    bool status; 

    if(!head) 
     status = true; 
    else 
     status = false; 

    return status; 
} 

    void NumberList::enqueue(int num) 
    { 
     ListNode *newNode; // Point to a new node 

     // Allocate a new node and store num there. 
     newNode = new ListNode; 
     newNode->value = num; 

     //If there are no nodes in the list 
     // make newNode the first node. 
     if(isEmpty()) 
     { 
      head = newNode; 
      rear = head; 
      //newNode->next = NULL; 
     } 
     else 
     { 
      rear->next = newNode; 
      rear = rear->next; 
      //newNode->next = head; 
      //head = newNode; 
     } 
    } 

    void NumberList::dequeue(int &num) 
    { 
     ListNode *temp; 

     if(isEmpty()) 
      cout << "The queue is empty.\n"; 
     else 
     { 
      num = head->value; 
      temp = head; 
      head = head->next; 
      delete temp; 
     } 
    } 

:

const int MAX_VALUES = 3; 

// Create a DynIntQueue object. 
NumberList iQueue; 

// Enqueue a series of numbers. 
cout << "Enqueuing " << MAX_VALUES << " items...\n"; 
for (int x = 0; x < MAX_VALUES; x++) 
    iQueue.enqueue(x); 

cout << endl; 

//Dequeue and retrieve all numbers in the queue 
cout << "The values in the queue were (Dequeuing):\n"; 
while(!iQueue.isEmpty()) 
{ 
    int value; 
    iQueue.dequeue(value); 
    cout << value << endl; 
} 
return 0; 

답변

2

마지막 노드의 다음 요소는 연결된 목록에서 NULL으로 설정해야합니다. 그래서, 나에게

void NumberList::enqueue(int num) 
{ 
    // ... 
    if(isEmpty()) 
    { 
     head = newNode; 
     head->next = NULL; 
     rear = head; 
    } 
    else 
    { 
     rear->next = newNode; 
     rear = rear->next; 
     rear->next = NULL;  // Pointing the next node element to null. 
    } 
} 

는 어떤 일이 Numberlist::isEmpty(); 멤버 함수 잘못된 것 같다. 목록이 비어 있는지 여부를 어떻게 결정합니까? 그것의 정의를 보여주십시오.

+0

감사합니다. 당신의 코드는 오류없이 작동합니다. :) cpp 파일에서 NumberList :: isEmpty()를 게시했습니다 (맨 위). 다시 과제물에 몇 점을 저축했습니다 :). 질문 : 지금은 템퍼 레이트하려고하고 있으므로 새 질문을 게시하는 대신이 질문을 편집해야합니까? – Riotson