Breadth first search using a double linked list

implmentation of a graph using double linked lists and BFS

Page 1 of 1

6 Replies - 7736 Views - Last Post: 04 December 2010 - 06:48 AM Rate Topic: -----

#1 abo el cabaten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 10

Breadth first search using a double linked list

Posted 04 December 2010 - 02:37 AM

I tried to implement a program to create a graph and breadth first search The program should display the nodes visited the distance between the nodes am stucked I have got errors .
# include <iostream>
using namespace std;


#include<conio.h>

#include<stdlib.h>


void create();  // For creating a graph
void dfs();  // For Deapth First Search(DFS) Traversal.
void bfs();  // For Breadth First Search(BFS) Traversal.

struct node  // Structure for elements in the graph
{
   int data;
   int status;
   struct node *next;
   struct link *adj;
};

struct link  // Structure for adjacency list
{
   struct node *next;
   struct link *adj;
};

struct node *start,*p,*q;
struct link *l,*k;

int main()
{
   int choice;
  // clrscr();
   create();
   while(1)
   {


cout<<"Enter your choice: "<<endl;
      cin>>choice;
      switch(choice)
      {

	 case 1:
	    bfs();
	    break;
	 case 2:
	    exit(0);
	    break;
	 default:
cout<<"wrong choice"<<endl;
	    
      }
   }
   return 0;
}

void create()    // Creating a graph
{
   int dat;
	int flag=0;
   int size=20 ;
   start=NULL;
   cout<<"Enter The number of nodes in the graph"<<endl;
      cin>>size ;
	  
  
cout<<"Enter the nodes in the graph(0 to end): "<<endl;
  // while(1)
   //{
	   for(int i=0;i<20;i++)
	   {
		   cin>>dat;
	   }
      //cin>>dat;
     // if(dat==size )
	// break;
      p=new node;
      p->data=dat;
      p->status=0;
      p->next=NULL;
      p->adj=NULL;
      if(flag==0)
      {
	 start=p;
	 q=p;
	 flag++;
      }
      else
      {
	 q->next=p;
	 q=p;
      }
  // }
   p=start;
   while(p!=NULL)
   {
      cout<<"Enter the links to "<<p->data<<" (0 to end) : "<<endl;
      flag=0;
      while(1)
      {
	 cin>>dat;
	 if(dat==0)
	    break;
	 k=new link;
	 k->adj=NULL;
	 if(flag==0)
	 {
	    p->adj=k;
	    l=k;
	    flag++;
	 }
	 else
	 {
	    l->adj=k;
	    l=k;
	 }
	 q=start;
	 while(q!=NULL)
	 {
	    if(q->data==dat)
	       k->next=q;
	    q=q->next;
	 }
      }
      p=p->next;
   }

   return;
}


// Deapth First Search(DFS) Traversal.
void bfs()
{
   int qu[20],i=1,j=0;
   p=start;
   while(p!=NULL)
   {
      p->status=0;
      p=p->next;
   }
   p=start;
   qu[0]=p->data;
   p->status=1;
   while(1)
   {
      if(qu[j]==0)
	 break;
      p=start;
      while(p!=NULL)
      {
	 if(p->data==qu[j])
	     break;
	 p=p->next;
      }
      k=p->adj;
      while(k!=NULL)
      {
	 q=k->next;
	 if(q->status==0)
	 {
	    qu[i]=q->data;
	    q->status=1;
	    qu[i+1]=0;
	    i++;
	 }
	 k=k->adj;
      }
      j++;
   }
   j=0;
   


   while(qu[j]!=0)
   {
      cout<<qu[j]<<"  ";
      j++;
   }
   
   return;
}



any help?

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 04 December 2010 - 05:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Breadth first search using a double linked list

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Breadth first search using a double linked list

Posted 04 December 2010 - 05:14 AM

Any help? Yeah, maybe when you copy and paste the errors EXACTLY as they appear. And when you're posting code, please obey the rules of the forum ad use code tags as shown below:

:code:
Was This Post Helpful? -1
  • +
  • -

#3 abo el cabaten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 10

Re: Breadth first search using a double linked list

Posted 04 December 2010 - 05:26 AM

I don't know where are the errors am not lazy by the way

still not working !

This post has been edited by JackOfAllTrades: 04 December 2010 - 05:27 AM
Reason for edit:: Removed repost of code

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Breadth first search using a double linked list

Posted 04 December 2010 - 05:28 AM

You may not be lazy, but you're not paying attention.

I didn't ask you repost the code, I asked you to post the ERRORS! If you're not getting actual errors, but the output is not what you expect, then post what your input, your expected output, and what you're actually receiving. Don't make us work harder than we have to to help YOU!

EDIT: Oh wait, you ARE lazy! And a liar! That code comes from here. Tsk, tsk.

This post has been edited by JackOfAllTrades: 04 December 2010 - 05:31 AM

Was This Post Helpful? -1
  • +
  • -

#5 abo el cabaten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 10

Re: Breadth first search using a double linked list

Posted 04 December 2010 - 05:34 AM

I got logical errors not syntax errors ! I have a graph fro countries I want to input the node number and the distances(vertices) in the switch when i choose the breadth first search the program stops ! abort
Was This Post Helpful? 0
  • +
  • -

#6 abo el cabaten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 10

Re: Breadth first search using a double linked list

Posted 04 December 2010 - 06:29 AM

I did not get it from this website ok I am stucked in it i changed in it it is not even working !
Was This Post Helpful? 0
  • +
  • -

#7 abo el cabaten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 10

Re: Breadth first search using a double linked list

Posted 04 December 2010 - 06:48 AM

AT leat you could tell me how to implement another one or fix this one I am a begginer i asked for a hekp thats all! AM NOT A LIAAAARRRRR

3 days am thinking about this problem :S
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1