#include<iostream> using namespace std; #define OK 1 #define ERROR 0 typedef int Status; typedef char QElemType; typedef struct QNode { QElemType data; struct QNode *next; } QNode, *QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; } LinkQueue; Status InitQueue(LinkQueue &Q) { Q.front = Q.rear = new QNode; Q.front->next = NULL; return OK; } Status EnQueue(LinkQueue &Q, QElemType e) { QueuePtr p; p = new QNode; p->data = e; p->next = NULL; ; ; return OK; } Status DeQueue(LinkQueue &Q, QElemType &e) { QueuePtr p; if (Q.front == Q.rear) return ERROR; p = Q.front->next; e = p->data; ; if (Q.rear == p) ; delete p; return OK; } int main() { LinkQueue Q; int n,m,i; char c; InitQueue(Q); cin >> n; for(i=0;i<n;i++){ cin >> c; EnQueue(Q,c); } for(i=0;i<n;i++){ DeQueue(Q,c); } cin >> m; for(i=0;i<m;i++){ cin >> c; EnQueue(Q,c); } for(i=0;i<m;i++){ DeQueue(Q,c); cout << c << " "; } return 0; }补全这段代码
时间: 2024-03-20 17:44:07 浏览: 68
#include_iostream.h_
4星 · 用户满意度95%
为了让代码能够正常运行,需要在代码中填充一些缺失的部分。下面是完整的代码:
```c++
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
typedef char QElemType;
typedef struct QNode {
QElemType data;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
} LinkQueue;
Status InitQueue(LinkQueue &Q) {
Q.front = Q.rear = new QNode;
Q.front->next = NULL;
return OK;
}
Status EnQueue(LinkQueue &Q, QElemType e) {
QueuePtr p;
p = new QNode;
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
Status DeQueue(LinkQueue &Q, QElemType &e) {
QueuePtr p;
if (Q.front == Q.rear)
return ERROR;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
delete p;
return OK;
}
int main() {
LinkQueue Q;
int n,m,i;
char c;
InitQueue(Q);
cin >> n;
for(i=0;i<n;i++){
cin >> c;
EnQueue(Q,c);
}
for(i=0;i<n;i++){
DeQueue(Q,c);
}
cin >> m;
for(i=0;i<m;i++){
cin >> c;
EnQueue(Q,c);
}
for(i=0;i<m;i++){
DeQueue(Q,c);
cout << c << " ";
}
return 0;
}
```
这段代码实现了一个基于链式存储的队列,并且能够读入一串字符,将其入队,并依次出队输出。
阅读全文