C/C++キューのデキューエンキュー単鎖表実装(完全コード)


キューのC/C++実装ロジックは比較的簡単で、単一チェーンテーブルでキューの作成、エンキュー、アウトキュー、キューが空の操作であるかどうかを判断し、ここで直接コードを貼り付けた.
/*        */

#include 

using namespace std;

typedef struct queue {
    int val;
    struct queue* next;
}Queue;

void Init(Queue* ptr, int n) {  //     
    while (n --) {
        Queue* temp = (Queue *)malloc(sizeof(Queue));
        cin >> temp->val;
        temp->next = NULL;

        ptr->next = temp;
        ptr = temp;
    }
}

bool QueueEmpty(Queue* ptr) {   //        
    if (ptr->next == NULL)
        return true;
    else  
        return false;
}

void Push(Queue* ptr, int item) { //  
    Queue* temp = (Queue *)malloc(sizeof(Queue));
    temp->val = item;
    temp->next = NULL;

    while (ptr->next) {
        ptr = ptr->next;
    }

    ptr->next = temp;
}

void Pop(Queue* ptr) {  //  
    if (QueueEmpty(ptr)) {
        cout << "    ,    !" << endl;
        return;
    }else {
        Queue* temp = ptr->next;
        ptr->next = ptr->next->next;
        free(temp);
    }
}


void Show(Queue* ptr) { //    
    cout << "    : " ;
    while (ptr->next) {
        cout << ptr->next->val << " ";
        ptr = ptr->next;
    }
    cout << endl;
}


int main() {
    int n;
    cout << "           : ";
    cin >> n;

    Queue* front = (Queue *)malloc(sizeof(Queue));  //     
    Init(front, n);

    Show(front);

    Push(front, 23);

    Show(front);

    Pop(front);

    Show(front);



    return 0;
}

    :

           : 2
13 18
    : 13 18 
    : 13 18 23 
    : 18 23 

ご注意:転載は作者に連絡してください.問題があれば、直接伝言を残してもいいです.