Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created November 14, 2019 17:28
Show Gist options
  • Save surinoel/c28cd2111cee4d12b278ea5b05185c78 to your computer and use it in GitHub Desktop.
Save surinoel/c28cd2111cee4d12b278ea5b05185c78 to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Queue {
struct Node *front;
struct Node *rear;
};
void InitQueue(struct Queue **q) {
*q = (struct Queue *)malloc(sizeof(struct Queue));
(*q)->front = (*q)->rear = NULL;
}
void EnQueue(struct Queue **q, int data) {
struct Node *tmp = (struct Node *)malloc(sizeof(struct Node));
tmp->data = data;
tmp->next = NULL;
if ((*q)->front == NULL) {
(*q)->front = (*q)->rear = tmp;
return;
}
(*q)->rear->next = tmp;
(*q)->rear = tmp;
}
void PrintQueue(struct Queue **q) {
struct Node *ptr = (*q)->front;
while (ptr != NULL) {
cout << ptr->data << ' ';
ptr = ptr->next;
}
cout << '\n';
}
int main(void) {
struct Queue *q = NULL;
InitQueue(&q);
EnQueue(&q, 10);
EnQueue(&q, 20);
EnQueue(&q, 30);
EnQueue(&q, 40);
EnQueue(&q, 50);
PrintQueue(&q);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment