Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created November 14, 2019 17:22
Show Gist options
  • Save surinoel/90335eaf84dd31e738acad9a33534b09 to your computer and use it in GitHub Desktop.
Save surinoel/90335eaf84dd31e738acad9a33534b09 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 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, NULL};
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