Graf losowy pomoc z kodem

0

Ktoś wie czemu przestaje działac po podaniu wartości?

#include<iostream>
#include<ctime>
#include<fstream>
using namespace std;
struct nodes;
struct adj
{
int ndnm;
int nmed;
nodes *next;
};
struct nodes
{
int ndnm;
int edln;
nodes *next;
};
int edc=0;
int randomed(int a,int b);
void checknd(nodes *t,adj *list,int u, int v, int edlength);
void disp(adj *list,int n);
int main()
{

cout<<"\nPodaj liczbę wezlow ... :";
int n;
cin>>n;
adj *list=new adj[n+1];
int i=0;
for(i=0;i<=n;i++)
{
list[i].ndnm=i;
list[i].nmed=0;
list[i].next=NULL;
}
cout<<"Stworzono formację sasiedztwa.\n";

int n1,n2;
cout<<"\nWprowadz limit dlugosci krawedzi :\n";
cout<<"\nPodaj dolna granice :";
cin>>n1;
cout<<"\nPodgaj gorna granice:";
cin>>n2;

int count=0,edlength;
int u=0,v=0;
nodes *temp,*temp2;
while(edc!=n)
{
u=rand()%n+1;
v=rand()%n+1;
if(u!=v)
{
if(list[u].nmed<7 && list[v].nmed<7)
{
edlength=randomed(n1,n2);
if(list[u].nmed==0)
{
list[u].next=new nodes;
temp=list[u].next;
temp->ndnm=v;
temp->edln=edlength;
temp->next=NULL;
list[u].nmed++;
}
else
{
checknd(list[u].next,list,u,v,edlength);
}
if(list[v].nmed==0)
{
list[v].next=new nodes;
temp2=list[v].next;
temp2->ndnm=u;
temp2->edln=edlength;
temp2->next=NULL;
list[v].nmed++;
}
else
{
checknd(list[v].next,list,v,u,edlength);
}

}
}
}
disp(list,n);
cin.ignore();

return 0;
}

int randomed(int a, int b)
{
return (a+rand()%(b-a)+1);
}

void checknd(nodes *t,adj *list,int u,int v,int edlength)
{
int flag=0;
nodes *t2;
do
{
if(t->ndnm==v)
{
flag=1;
}
if(t->next==NULL)
t2=t;
t=t->next;
}
while(t!=NULL);
t=t2;
if(flag==0)
{
t->next=new nodes;
t=t->next;
t->next=NULL;
t->ndnm=v;
list[u].nmed++;
if(list[u].nmed==3)
{
edc++;
}
t->edln=edlength;
}
}
void disp(adj *list, int n)
{

int i=0;
nodes *temp;
for(i=1;i<=n;i++)
{
temp=list[i].next;
do
{

temp=temp->next;
}
while(temp);
}
}

0

Nie wiem i się nie dowiem dopóki kodu nie sformatujesz i nie wrzucisz go tu jak należy.

0
#include<iostream>
#include<ctime>
#include<fstream>

using namespace std;
struct nodes;
struct adj
{
  int ndnm;
  int nmed; 
  nodes *next;
};
  struct nodes
{
  int ndnm;
  int edln;
  nodes *next;
};

  int edc=0;
  int randomed(int a,int b);
  void checknd(nodes *t,adj *list,int u, int v, int edlength);
  void disp(adj *list,int n);

int main()
{
  cout<<"\nPodaj liczbę wezlow ... :";
  int n;
  cin>>n;
  adj *list=new adj[n+1];
  int i=0;
  for(i=0;i<=n;i++)
  {
    list[i].ndnm=i;
    list[i].nmed=0;
    list[i].next=NULL;
  }
  cout<<"Stworzono formację sasiedztwa.\n";

  int n1,n2;
  cout<<"\nWprowadz limit dlugosci krawedzi:\n";
  cout<<"\nPodaj dolna granice :";
  cin>>n1;
  cout<<"\nPodgaj gorna granice:";
  cin>>n2;

  int count=0,edlength;
  int u=0,v=0;
  nodes *temp,*temp2;
  while(edc!=n)
  {
    u=rand()%n+1;
    v=rand()%n+1;
    if(u!=v)
    {
      if(list[u].nmed<7 && list[v].nmed<7)
    {
      edlength=randomed(n1,n2);
      if(list[u].nmed==0)
      {
        list[u].next=new nodes;
        temp=list[u].next;
        temp->ndnm=v;
        temp->edln=edlength;
        temp->next=NULL;
        list[u].nmed++;
      } else
      {
        checknd(list[u].next,list,u,v,edlength);
      }
      if(list[v].nmed==0)
      {
        list[v].next=new nodes;
        temp2=list[v].next;
        temp2->ndnm=u;
        temp2->edln=edlength;
        temp2->next=NULL;
        list[v].nmed++;
      } else
      {
        checknd(list[v].next,list,v,u,edlength);
      }
    }
  }
}
  disp(list,n);
  cin.ignore();

  return 0;
}


int randomed(int a, int b)
{
  return (a+rand()%(b-a)+1);
}


void checknd(nodes *t,adj *list,int u,int v,int edlength)
{
  int flag=0;
  nodes *t2;
  do
  {
    if(t->ndnm==v)
    {
      flag=1;
    }
    if(t->next==NULL)
      t2=t;
    t=t->next;
  }
  while(t!=NULL);
  t=t2;
  if(flag==0)
  {
    t->next=new nodes;
    t=t->next;
    t->next=NULL;
    t->ndnm=v;
    list[u].nmed++;
    if(list[u].nmed==3)
    {
      edc++;
    }
      t->edln=edlength;
    }
}

void disp(adj *list, int n)
{
  int i=0;
  nodes *temp;
  for(i=1;i<=n;i++)
  {
    temp=list[i].next;
    do
    {
      temp=temp->next;
    }
    while(temp);
  }
}

Sformatowałem cały kod, lecz nie analizowałem go.

0

Dzięki za sformatowanie

0

Co ci przestaje działać? Opisz twój problem. Mnie się twój kod kompiluje i działa (czy poprawnie to nie wiem) natomiast coś tam robi. Pierwszego bubla jakiego widać zaraz to poza tą zmienną globalną, którą trzeba szukać w kodzie gdzie jest inkrementowana (nieczytelne to jest, lepiej do tej funkcji przekazać przez referencję i oczywiście pozbyć się tej globalnej w tedy widać, że coś tam z nią robisz) to masz po prostu wyciek.

0

i po co ci ten cin.ignore()?

0

Problem jest gdy podam wartosci wszystkie w okienku, program zamyka sie po prostu i tyle, ja też przy kompilacji nie mialem zadnych problemów.

0

ten program ci się nie zamyka tylko kończy działanie (i zwraca 0 czyli działa dobrze). Chcesz zatrzymać jego działanie przy wyjściu? Wywal cin.ignore() i daj cin.sync(); cin.get();
Masz tam funkcję disp ale nic w niej nie wyświetlasz tylko przechodzisz sobie po grafie czy co to zaimplementowałeś.

1 użytkowników online, w tym zalogowanych: 0, gości: 1