The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Wednesday, February 29, 2012

Implementing Graph in Linked Lists


#include<stdio.h>
#include<conio.h>
#include<iostream.h>

struct node
{
  char info;
  int status;
  struct node *next;
  struct edge *adj;
};

struct edge
{
 struct node *dest;
 struct edge *link;
}*nedg,*ptre;

struct node *start,*nptr,*ptr,*loc,*rear;

int choice;
char item;

 void find_node()
 {
  loc = start;
  while(loc->info != item && loc != NULL)
   loc = loc->next;
 }

void create_node()
 {
  while(1)
  {
   nptr = new node;
   nptr->next = NULL;
   nptr->adj = NULL;
   printf("\n Enter The New Node To be Inserted: ");
   cin>>item;
   nptr->info = item;
   if(item == '0')
    break;
   if(start == NULL)
    start = rear = nptr;
   else
    {
    find_node();
    if(loc == NULL)
     {
     printf("\n\n\t\tInsertion has been done \n\n");
     rear->next = nptr;
     rear = nptr;
     }
    else
     {
     printf("\n\n\t\tInsertion can't be done due to similiar vertices \n\n");
     delete nptr;
     }
    }
   }
  getch();
 }

void int_stats()
{
 ptr = start;
 while(ptr != NULL)
 {
  ptr->status = 1;
  ptr = ptr->next;
 }
}

void display_node()
 {
  ptr = start;
  printf("\n\n Now The List Is : \n\t");
  printf("\n Vertex List        Adjacent List \n");
  while(ptr != NULL)
  {
   printf("\n\t%c\t\t",ptr->info);
   ptre = ptr->adj;
   while(ptre != NULL)
   {
   printf(" %c",ptre->dest->info);
   ptre = ptre->link;
   }
   ptr = ptr->next;
  }
 }

void create_vertices()
{
 ptr = start;
 while(ptr != NULL)
 {
  choice = 0;
  while(1)
  {
  printf("\n Enter the element to be inserted as edge '%c': ",ptr->info);
  cin>>item;
  if(item == '0')
   break;
  find_node();
  if(loc != NULL)
  {
  nedg = new edge;
  nedg->dest = loc;
  nedg->link = NULL;
   if(ptr->adj == NULL)
    ptr->adj = nedg;
   else
   {
    nedg->link = ptr->adj;
    ptr->adj = nedg;
   }
   }
  }
   ptr = ptr->next;
 }
}

void main()
{
clrscr();
create_node();
create_vertices();
display_node();
getch();
}