Created
November 14, 2019 17:22
-
-
Save surinoel/90335eaf84dd31e738acad9a33534b09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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