I am trying to implement code for a random graph, where all vertices are connected to each other. The edges should be randomly selected. I wrote this code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define VOL 100
struct node{
int info;
struct node *next;
};
struct node *read_list(void){
struct node *p, *first=NULL;
int i,V;
for(i=0;i<V;i++){
p=malloc(sizeof(struct node));
p->next=first;
p->info=rand()%V;
first=p;
}
return(first);
}
void print_list(struct node *p){
while(p!=NULL){
printf("%d-> ", p->info);
p=p->next;
}
printf("NULL\n");
return;
}
int read_graph(struct node *G[]){
int i, V;
printf("Select a number of vertices:\n");
scanf("%d", &V);
for(i=0;i<V;i++){
printf("Adjacency list of vertex %d:\n", i);
G[i]=read_list();
}
return(V);
}
void print_graph(struct node *G[], int V){
int i;
printf("Adjacency lists of the graph:\n");
for(i=0;i<V;i++){
printf("Adjacency vertices to %d: ",i);
print_list(G[i]);
}
return;
}
int adj(int i, int j, struct node *G[]){
int r;
struct node *p;
p=G[i];
while (p!=NULL && p->info !=j)
p=p->next;
if(p==NULL)
r=1;
else
r=0;
return (r);
}
int main(){
srand(time(NULL));
struct node *G[VOL], *L;
int V;
V=read_graph(G);
print_graph(G, V);
L=read_list();
return 0;
}
However, it doesn't work and I don't know why. Xcode tells me 'build succeeded" but the code prints nothing (currently only 'Select a number of vertices'): no adjacency list, no edges.. Could you please check it and tell me where the errors are?
In the for statement below, the variable V is not initialized.
...
struct node *read_list(void) {
struct node *p, *first = NULL;
int i, V; // <<<<<<<<<<<<<<<<<<< V not initialized
for (i = 0; i<V; i++) {
//^ trouble here
...
Related
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct Graph* GraphList();
struct Listnode
{
int vertex;
struct Listnode*next;
};
struct Graph
{
int V;
int E;
struct Listnode *Adj;
};
void main()
{
struct Graph *G=NULL;
int i;
struct Listnode *temp=NULL;
printf("Program to Implement graph using Adjacency List\n");
G=GraphList();
for(i=0;i<G->V;i++)
{
temp=G->Adj+i;
while((temp->next)!=(G->Adj+i))
{
printf("%d-->",temp->vertex);
temp=temp->next;
}
printf("%d",temp->vertex);
printf("\n");
}
}
struct Graph* GraphList()
{
int i,j,x,y;
struct Listnode *t,*temp;
struct Graph *G;
G=(struct Graph*)malloc(sizeof(struct Graph));
printf("Enter the no.of vertices and Edges respectively\n");
scanf("%d %d",&G->V,&G->E);
G->Adj=(struct Listnode*)malloc((sizeof(struct Listnode))*G->V); //Undirected Graph
for(i=0;i<G->V;i++)
{
(G->Adj+i)->vertex=i+1;
(G->Adj+i)->next= (G->Adj+i);
}
for(i=0;i<G->V;i++)
{
t= (G->Adj+i);
printf("Enter number of neigbouring nodes to node-%d ",i+1);
scanf("%d",&x);
for(j=0;j<x;j++)
{
printf("Enter the vertex number-%d that is neighbour to node -%d ",j+1,i+1);
scanf("%d",&y);
temp=(struct Listnode*)malloc(sizeof(struct Listnode));
temp->vertex=y;
temp->next= G->Adj+i;
while(t->next!= (G->Adj+i))
t=t->next;
t->next=temp;
}
}
return G;
}
The above code works fine, but when I replace "G->Adj+i" with "G->Adj[i]" it doesn't work. I have created array of type "struct Listnode" (let's say size-5) and and stored it in "G->Adj", and I feel it is fair to use "G->Adj[i]" but I don't understand why error message pops-up as:
"D:\DataStructures by SS\Graph Link (standard).c|60|error: incompatible types when assigning to type 'struct Listnode *' from type 'struct Listnode'| ".
Please explain.
G->Adj+i is equivalent to &G->Adj[i]. The leading ampersand is required to undo [i]'s dereference.
This program is supposed to be a demonstration for deleting a node from a linked list.
The program works and output is the same as desired but soon after a dialog box appears saying that the executable file has stopped working. I used code-blocks as the IDE and C-programming language. I would like to know why that happened and how to avoid it happening in the future. Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void create(struct node *first,int a[])
{
struct node *t,*last;
int i;
first->data=a[0];
first->next=NULL;
last=first;
for(i = 1; i < 5; i++)
{
t=(struct node *)malloc(sizeof(struct node));
t->data=a[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void display(struct node *f)
{
int i;
for(i=0;i<5;i++)
{
printf("%d ",f->data);
f=f->next;
}
}
int delete(struct node * f,int pos)
{
int i,x;
struct node *q=NULL;
if (pos < 1 || pos > 5) {
return -1;
}
if (pos == 1) {
q=f;
f=f->next;
x=q->data;
free(q);
return x;
}
for (i = 0; i < pos - 1; i++)
{
q=f;
f=f->next;
}
q->next=f->next;
x=f->data;
free(f);
return x;
}
int main()
{
int a[]={3,5,7,8,9};
struct node *first;
int pos=4,t;
first=(struct node *)malloc(sizeof(struct node));
create(first,a);
t=delete(first,pos);
display(first);
return 0;
}
The issue is on display function where f is null
A solution is to remove the for loop and use a while testing if f is null or not.
I have also added a final \n to flush stdout
void display(struct node *f)
{
while (f)
{
printf("%d ",f->data);
f=f->next;
}
printf("\n");
}
this is the wrong code,while i transfer it in my main code , it shows program receive a signal SIGSEGV,segmentation fault.whats wrong with it?
char *DeleteList(LinkList L,char* e,int i)
{
LinkList p;
p=(LinkList)malloc(sizeof(LNode));
p=GetNode(L,i-1);
*e=p->next->data;
p->next=p->next->next;
free(p);
return e;
}
the main function
int main()
{
char *e;
LinkList L,p,z,q;
L=CreatListH();
p=(LinkList)malloc(sizeof(LNode));
e=DeleteList(L,e,3);
p=L;
p=p->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
the head File statement
#define ERROR 0
#define OK 1
typedef struct LNode{
char data;
struct LNode *next;
}LNode,*LinkList;
LinkList CreatListH();
LinkList CreatListT();
LinkList GetNode(LinkList L,int i);
LinkList LocateNode(LinkList L,char e);
int InsertList(LinkList L,char e,int i);
char *DeleteList(LinkList L,char* e,int i);
int ListLength(LinkList L);
void DifferenceList(LinkList La,LinkList Lb);
So I'm trying to get input from user and store it in linked list, using array (every 5 chars a new linked list is created). After getting EOF I want to print the input (actually print the arrays in each linked list)
here is my code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
typedef struct charNode {
int arr[MAX];
struct charNode *next;
} charNode;
void addNode();
void printAll();
int main(){
int c,i;
charNode *head=malloc(sizeof(charNode));
charNode *current=head;
while((c=getchar())!=EOF){
while(i<MAX){
current->arr[i++]=c;
}
i=0;
addNode(current);
}
printAll(head);
return 0;
}
void addNode(charNode *current){
struct charNode *link = (struct charNode*) malloc(sizeof(struct charNode));
current->next =link;
link->next = NULL;
current=current->next;
}
void printAll(charNode *head){
int j=0;
while(head->next!=NULL){
while(j<MAX){
printf("\n %d \t",head->arr[j++]);
}
printAll(head->next);
}
return;
}
and I'm getting "Segmentation fault (core dumped)" error..
This is Undefined behavior:
int c,i;
charNode *head=malloc(sizeof(charNode));
charNode *current=head;
while((c=getchar())!=EOF){
while(i<MAX){
current->arr[i++]=c;
}
since you are using i uninitialized to access your array, which could produce the Segmentation fault.
Change this:
printf("\n %d \t",head->arr[j++]);
to this:
printf("\n %c \t",head->arr[j++]);
so that you print characters, instead of numbers.
Simply initialize i to 0 for a start, happy debugging! =)
int main(){
char c;
charNode *head=malloc(sizeof(charNode));
charNode *current=head;
int i = 0;
while((c=getchar())!=EOF){
getchar();
while(i<MAX){
current->arr[i++]=c;
}
i=0;
addNode(¤t);
}
printAll(head);
return 0;
}
void addNode(charNode **current){
struct charNode *link = malloc(sizeof(struct charNode));
link->next = NULL;
(*current)->next =link;
*current=(*current)->next;
}
void printAll(charNode *head){
int j=0;
if(head!=NULL){
while(j<MAX){
printf("%c\n",head->arr[j++]);
}
if(head->next != NULL)
printAll(head->next);
}
}
I changed your code a bit. Especially take a good look at addNode(). Now it works correctly
I have been trying to create a program that prints the order in which nodes in a graph are closed during a DFS search. So in the graph 1->2->3 it should print: 3 2 1, somehow the code below would print 3 1 2, in general
for simple graphs like the one above it will print n 1 2 ... n-2 n-1.
If you know what I'm doing wrong please share.
#include <stdio.h>
#include <stdlib.h>
#define UNVISITED 0
#define VISITING 1
#define CLOSED 2
typedef struct node{
int id;
struct node *next;
}Node;
typedef struct graph{
int V,E;
int *visited;
Node ** list;
Node* order;
}Graph;
Graph *makeGraph(){
int v,e,i;
scanf("%d %d",&v ,&e);
Graph* g = (Graph*)malloc(sizeof(Graph));
g->V=v;
g->E=e;
g->order =NULL;
g->list = (Node**)malloc(v*sizeof(Node*));
g->visited = (int*)malloc(v*sizeof(int));
for(i=0;i<v;++i){
g->list[i]=NULL;
g->visited[i]=UNVISITED;
}
return g;
}
void createArcs(Graph *g){
int from,to;
Node *n = (Node*)malloc(sizeof(Node));
for(int i=0;i<g->E;++i){
scanf("%d %d",&from ,&to);
n->id = to;
n->next = g->list[from-1];
g->list[from-1]=n;
}
return;
}
void visitNode(Graph *g, int i){
if(g->visited[i]==CLOSED)
return;
else if(g->list[i]==NULL){
printf("%d\t", i+1);
g->visited[i]=CLOSED;
return;
}
Node* n = g->list[i];
while(n!=NULL){
visitNode(g,n->id-1);
n=n->next;
}
printf("%d\t", i+1);
g->visited[i]=CLOSED;
return;
}
int main()
{
Graph *g = makeGraph();
createArcs(g);
for(int i=0;i<g->V;i++)
visitNode(g,i);
return 0;
}