2016-04-03 95 views
-1

此代码用于使用邻接列表创建图。
node是垂直链表的每个元素的水平链表的结构,
graphnode是垂直链表的结构。为什么我在代码中出现分段错误(核心转储)错误?

代码编译正确,但是当给出第一个输入时,它显示后面的分段错误。

#include<iostream> 
using namespace std; 
struct node  //struct 
{ 
    struct node *next; 
    char value; 
}; 
struct graphnode 
{ 
    struct node *start; 
    struct graphnode *down; 
    char value; 
    int count; 
}; 
graphnode * creategraphnode(char val) 
{ 
    struct graphnode *newnode=new graphnode; 
    newnode->start=NULL; 
    newnode->down=NULL; 
    newnode->value=val; 
    newnode->count=0; 
    return newnode; 
} 
node *createnode(char val) 
{ 
    struct node *newnode=new node; 
    newnode->next=NULL; 
    newnode->value=val; 
} 
void insertgraphnode(char value,graphnode *graph) 
{ 
    struct graphnode *newnode=creategraphnode(value); 
    if(graph==NULL) 
    { 
     graph=newnode; 
    } 
    else 
    { 
     graphnode *temp=graph; 
     while(temp->down) 
      temp=temp->down; 
     temp->down=newnode; 
    } 
} 
void insertnode(graphnode *graph) 
{ 
    char val; 
    struct node *temp=graph->start; 
    cout<<"What is the outdegree of "<<graph->value; 
    cin>>graph->count; 
    cout<<"\nEnter"<<graph->count<<" nodes separated by space:"; 
    for(int i=1;i<=graph->count;i++) 
    { 
     cin>>val; 
     node* newnode=createnode(val); 
     temp=newnode; 
     temp=temp->next; 
    } 
} 
void display(struct graphnode *graph) 
{ 
    if(graph==NULL) 
    { 
     cout<<"\nNo data to display"; 
     return; 
    } 
    struct graphnode *temp=graph; 
    while(temp) 
    { 
     struct node *temp1=temp->start; 
     cout<<temp->value<<"=>> "; 
     if(temp1==NULL) 
     { 
      continue; 
     } 
     else 
     { 
      while(temp1) 
      { 
       cout<<temp1->value<<"-> "; 
       temp1=temp1->next; 
      } 
     } 
     cout<<"/\n"; 
    } 
} 
int main() 
{ 
    struct graphnode *graph=NULL; 
    int totalnodes; 
    char val; 
    cout<<"\nHow many nodes does the graph contain? : "; 
    cin>>totalnodes; 
    cout<<"\nEnter "<<totalnodes<<" space separated nodes : "; 
    for(int i=1;i<=totalnodes;i++) 
    { 
     cin>>val; 
     insertgraphnode(val,graph); 
    } 
    struct graphnode *temp=graph; 
    while(temp->down) 
    { 
     insertnode(temp); 
     temp=temp->down; 
    } 
    display(graph); 
    return 0; 
} 
+0

是否有一个插件会自动创建问题SO当ap节目压碎? – Slava

+0

'struct node * temp = graph-> start;'是第一个节点插入时的问题。那时'graph'是'NULL'。 –

+0

我不会调用函数insertnode(),除非我在它之前使用了函数insertgraphnode() –

回答

0

如果我没有错,你的代码至少有三个问题。

1)您的createnode()不返回创建的节点;添加return newnode;

2)您的insertgraphnode()不会更改调用函数中的第二个参数;所以,当你在main()

insertgraphnode(val,graph);

NULL;里面insertgraphnode()你改变的价值,但功能外仍然NULL;永远。这导致分段错误。

你可以解决这个变化insertgraphnode()所以第二个参数是一个指针指针,并通过&graph

另一种解决方案是修改insertgraphnode()返回值

graphnode * insertgraphnode(char value,graphnode *graph) 
{ 
    struct graphnode *newnode=creategraphnode(value); 
    if(graph==NULL) 
    { 
     graph=newnode; 
    } 
    else 
    { 
     graphnode *temp=graph; 
     while(temp->down) 
      temp=temp->down; 
     temp->down=newnode; 
    } 
    return graph; 
} 

,把它以这种方式

graph = insertgraphnode(val,graph); 

3)display()while进去循环,因为你不改变值temp