Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct node{
- int element;
- struct node* next;
- };
- struct Queue{
- struct node* front;
- struct node* rear;
- };
- bool isEmpty(Queue* q){
- if(q->front==NULL)
- return true;
- else
- return false;
- }
- void enqueue(int x, Queue* q){
- struct node* t = (struct node*) malloc(sizeof(struct node));
- t->element = x;
- t->next = NULL;
- if(isEmpty(q)){
- q->front = q->rear = t;
- //cout << "Enqueue done" << endl;
- }
- else{
- q->rear->next = t;
- q->rear = t;
- //cout << "Enqueue done" << endl;
- }
- }
- void dequeue(Queue* q){
- if(isEmpty(q)){
- cout << "Queue is already empty" << endl;
- return;
- }
- else if(q->front == q->rear){
- struct node* t = q->front;
- q->front = q->rear = NULL;
- free(t);
- }
- else{
- struct node* t = q->front;
- q->front = q->front->next;
- free(t);
- }
- }
- int peek(Queue *q){
- if(isEmpty(q)){
- //cout << "Queue is already empty" << endl;
- return -9999;
- }
- return q->front->element;
- }
- void traversal(Queue *q){
- struct node* cur = q->front;
- while(cur!=NULL){
- cout << cur->element << endl;
- cur = cur->next;
- }
- }
- struct Queue* Q[100];
- struct Queue* MQ;
- int main(){
- //freopen("input.txt","r",stdin);
- int N;
- int s, d, root;
- cin >> N;
- cin >> root;
- for(int i=1; i<=N; i++)
- Q[i] = (struct Queue*)malloc(sizeof(struct Queue));
- while(cin>>s>>d){
- enqueue(d,Q[s]);
- enqueue(s,Q[d]);
- }
- for(int i=1; i<=N; i++){
- cout << "Traversing Lists for vertex: " << i << endl;
- traversal(Q[i]);
- }
- /*
- MQ = (struct Queue*) malloc(sizeof(struct Queue));
- cout << "Executing BFS from root vertex " << root << ".... " << endl;
- BFS(root);
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment