Skip to main content

DEPTH FIRST SEARCH IN JAVA

DEPTH FIRST SEARCH IN JAVA



public void dfs()
 {
  
  vertex[0].visited=true;
  displyVertex(0);
  stackobj.push(0);
  
  while(!stackobj.isEmpty())
  {
   int v=adjVertex((Integer) stackobj.peek());
   if(v==-1)
   {
    stackobj.pop();
   }
    else{
     vertex[v].visited=true;
     displyVertex(v);
     stackobj.push(v);
     
    }
   }
  }
  

Comments

.

Popular posts from this blog

Deleting a Node by passing data in LInkedList

Deleting a Node by passing data in LInkedList:-

The node can be deleted by passing the data value in a function.Here we have three pointers one to traverse the list other to point current node and third one to point previous of current node.



//deleting a given keypublic Node deleteKeyNode(Node head,int key){//traverse pointer Node temp=head;//previous pointer Node prevtemp=temp;while(temp!=null){//when key is at head positionif(key==head.data){ head=head.next; System.out.println("head is deleted");return head;}//when key is at any position other than headif(temp.data==key){ prevtemp.next=temp.next; temp.next=null; temp=prevtemp;} prevtemp=temp; temp=temp.next;}return head;}