Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // This is Implementation of Queue
  5.  
  6. struct Node {
  7. int data;
  8. struct Node *next;
  9. }*front=NULL,*rear=NULL;
  10.  
  11. void enqueue(int ele){
  12. struct Node *temp;
  13. temp = new Node;
  14. temp->data = ele;
  15. temp->next = NULL;
  16. if(front==NULL){
  17. front=rear=temp;
  18. }else{
  19. rear->next = temp;
  20. rear=temp;
  21. }
  22. }
  23.  
  24. int dequeue(){
  25. if(front==NULL){
  26. return 0;
  27. }
  28. struct Node *p=front;
  29. int x;
  30. x=front->data;
  31. front = front->next;
  32. delete p;
  33. return x;
  34. }
  35.  
  36. int isempty(){
  37. return front==NULL;
  38. }
  39.  
  40. // Queue implementation ends here
  41.  
  42. void BFS(int A[][7],int start, int n){
  43. int visited[7]={0};
  44. int i=start,j;
  45. cout << i << " ";
  46. visited[i] = 1;
  47. enqueue(i);
  48. while(!isempty()){
  49. j = dequeue();
  50. for(int x=1;x<n;x++){
  51. if(A[j][x]==1 && visited[x]!=1){
  52. cout << x << " ";
  53. visited[x] = 1;
  54. enqueue(x);
  55. }
  56. }
  57. }
  58. }
  59.  
  60. int main(){
  61. int start=1;
  62. int size=7;
  63.  
  64. int A[7][7]={{0,0,0,0,0,0,0},
  65. {0,0,1,1,0,0,0},
  66. {0,1,0,0,1,0,0},
  67. {0,1,0,0,1,0,0},
  68. {0,0,1,1,0,1,1},
  69. {0,0,0,0,1,0,0},
  70. {0,0,0,0,1,0,0}};
  71.  
  72. BFS(A,start,size);
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement