关注公众号程序猿从入门到入土查询更方便哦
【8564】变成,采用链式存储实现队列的初始化,入队,出队操作。要求输入整形数据,输入0表示结束(0不存储在栈中)
输入
11 22 33 44 55 66 77 88 99 0
11 22 33 44 55 66 77 88 99
#include<bits/stdc++.h>
using namespace std;
typedef struct node {
int data;
node* prior;
node* next;
}QNode;
typedef struct queue {
QNode* head;
QNode* rear;
}Queue;
void InitQueue(Queue& Q) {
Q.head = Q.rear = new QNode;
}
void InQueue(Queue& Q,int data) {
QNode* temp = new QNode;
temp->data = data;
temp->prior = Q.rear;
Q.rear->next = temp;
Q.rear = temp;
}
int OutQueue(Queue& Q) {
QNode* temp = new QNode;
temp = Q.head->next;
Q.head->next = Q.head->next->next;
return temp->data;
}
int main() {
Queue Q;
InitQueue(Q);
int n;
cin >> n;
while (n!=0) {
InQueue(Q,n);
cin >> n;
}
while (Q.head->next!=Q.rear) {
cout << OutQueue(Q) << " ";
}
cout << OutQueue(Q);
return 0;
}