Finding path between nodes using BFS in C language - c

I'm new to C language and it's been harder for me to work with pointers after working in Java😥
I was trying to write a code of finding a path (not necessary minimum) between two nodes in a graph using breadth-first-search.
Here is my code :
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 200
void push(int a);
int pop(void);
void bfs(int a,int b,int len);
int nextnode(int a);
typedef struct node{
int data;
struct node* next;
}node;
int res[MAXSIZE];
int visited[MAXSIZE];
int rear,front;
node* graph[MAXSIZE];
int len;
int path[MAXSIZE];
int nextnode(int a)
{
if(graph[a]==NULL)
return -1;
else
{
struct node* c=graph[a];
while(visited[c->data]!=1 && c!=NULL)
{
c=c->next;
}
if(c==NULL)
return -1;
else
return c->data;
}
}
void push(int a)
{
path[rear]=a;
rear++;
}
int pop()
{
if(front==rear)
return -1;
int num=path[front];
front++;
return num;
}
int main()
{
rear=0;
len=0;
front=0;
int n,e;
int i,a,b;
printf("%s\n%s", "Inputting Graph... ","Enter number of nodes and edges: ");
scanf("%d %d",&n,&e);
printf("%s %d %s\n", "Graph Created with",n,"nodes without any edge.");
printf("%s\n","Enter the edges in 1 2 format if an edge exist from Node 1 to Node 2" );
for(i=1;i<=n;i++)
{
graph[i]=NULL;
visited[i]=0;
}
struct node* new = (struct node*)malloc(sizeof(struct node));
for(i=0;i<e;i++)
{
scanf("%d %d",&a,&b);
new->data=b;
new->next=NULL;
struct node* curr=graph[a];
if(curr==NULL)
{
graph[a]=new;
}
else
{
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=new;
}
}
printf("%s\n", "Graph Created Successfully.");
printf("%s", "Enter the node numbers between which the path is to be found between: ");
scanf("%d %d",&a,&b);
bfs(a,b,0);
printf("Length is %d\n",len);
for(i=1;i<=len;i++)
{
printf("%d\n",res[len]);
}
}
void bfs(int a,int b,int len)
{
int c;
visited[a]=1;
int flag=0;
while(a!=-1)
{
c=nextnode(a);
while(c!=-1)
{
c=nextnode(a);
if(c==b)
{
flag=1;
break;
}
push(c);
visited[c]=1;
}
len++;
res[len]=a;
if(flag==1)
{
res[len]=b;
break;
}
a=pop();
}
}
I know it's huge, but please mind going through it once. The problem I'm getting is Segmentation Fault after I input all the values, and before dfs() function call! Please Help.
For understanding: I have used array of Lists. Each array index denotes a node and the list denotes all the edges it is connected to. eg: if my Graph has 1->2, 1->3, 2-3 edges;
graph[1] will have a list 2->3->NULL. And graph[2] will have 3->NULL.
Thank you.
EDIT
As pointed out by Aditi, the error was in the line where nextnode function ran the while loop. After changing the code to
while(c != NULL && visited[c->data] == 1 )
the program ran flawlessly.
Thanks!

I think what you are trying to do is not graph[i] = NULL but graph[i]->next = NULL

Related

C language:Binary search tree with strings

I have to create a program for a binary search tree for strings, which calculates how many times each word repeats, and it seems like I can't get the program to do as intended, because when printing it out, what should be printed first, are the words/characters smaller than the origin node, followed by the words/characters bigger than the origin node. But what my program does, it just prints it out in the order that I introduced the words/characters, I am still thinking about, how to realise the part of the program that will calculate the number of appearances of each word.
#include<stdio.h>
#include<stdlib.h>
#define MAX 15
typedef struct BST
{
char data[MAX];
struct BST *left;
struct BST *right;
} node;
node *create();
void insert(node *,node *);
void preorder(node *);
int main()
{
char ch;
node *root=NULL,*temp;
do
{
temp=create();
if(root==NULL)
{
root=temp;
}
else
{
insert(root,temp);
}
printf("\nDo you want to enter more(y/n)?");
ch=getch();
}
while(ch=='y'||ch=='Y');
printf("\nPreorder Traversal: ");
preorder(root);
return 0;
}
node *create()
{
node *temp;
printf("\nEnter data:");
temp=(node*)malloc(sizeof(node));
fgets(&temp->data,MAX,stdin);
temp->left=temp->right=NULL;
return temp;
}
void insert(node *root,node *temp)
{
if(temp->data<root->data)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}
if(temp->data>root->data)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
}
void preorder(node *root)
{
if(root!=NULL)
{
printf("%s ",root->data);
preorder(root->left);
preorder(root->right);
}
}
Here's what I came up with, with the help of somebody in the comments, and it works as intended
void insert(node *root,node *temp)
{
int cmp_rezult=strcmp(temp->data,root->data);
printf("\nCompare:%d ", cmp_rezult);
if(cmp_rezult<0)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}
if(cmp_rezult>0)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
}
In insert you do:
temp->data<root->data
but that's not how you compare strings in C.
To compare strings use strcmp
Further it seems that insert miss a code block for handling "equal".
So something like:
int cmp_result = strcmp(temp, data<root->data);
if (cmp_result < 0)
{
// Handle less than
}
else if (cmp_result > 0)
{
// Handle greater than
}
else // i.e. cmp_result == 0
{
// Handle equal
}

Storing input from user into linked list using array

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(&current);
}
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

Lattices of Partial Order Set Segmentation Fault(core dumped)

Given a Set S = {x1,x2,x3,x4,x5,….} whose size is n (< 20), I need to list all the lattices (subsets of S) on S with size k(should be taken as input, but in my question I fixed it for debugging purposes). The relation over which the partial order is defined is divisibility.
#include <stdio.h>
#include <stdbool.h>
typedef struct{
int data;
struct node* next;
}node;
void addEdge(node* graph[],int a,int b){
node *ptr=(node *)malloc(sizeof(node));
ptr->next=graph[a];
ptr->data=b;
graph[a]=ptr;
}
void buildGraph(node* graph[],int n){
int i,j;
for(i=2;i<=n;i++){
for(j=1;j<=i/2;j++){
if(i%j==0){
if(j!=1) addEdge(graph,i/j,i);
else addEdge(graph,j,i);
}
}
}
}
void printGraph(node* graph[],int n){
int i=0;
node *ptr;
for(i=0;i<=n;i++){
ptr=graph[i];
printf("Row-%d ",i);
while(ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
}
void print(int array[],int n){
int i;
for(i=0;i<n;i++) printf("%d ",array[i]);
printf("\n");
}
bool checkDuplicate(int array[],int n,int a){
int i;
for(i=0;i<n;i++) if(array[i]==a) return false;
return true;
}
void formLattice(node* graph[],int lattice[],int k,int vertex,int level){
//printf("Level-%d \n",level);
if(level>k) return;
if(level==k) {
lattice[level-1]=vertex;
print(lattice,k);
return;
}
lattice[level-1]=vertex;
node* ptr=graph[vertex];
while(ptr!=NULL){
// if(checkDuplicate(lattice,level,ptr->data))
formLattice(graph,lattice,k,ptr->data,level+1);
ptr=ptr->next;
}
}
void printLattice(node* graph[],int n,int k){
int lattice[k],i;
node* ptr;
for(i=1;i<=n;i++){
ptr=graph[i];
while(ptr!=NULL){
lattice[0]=i;
formLattice(graph,lattice,k,ptr->data,2);
ptr=ptr->next;
}
}
}
int main(void) {
int n=10,k=4;
node* graph[n+1];
int i;
for(i=0;i<=n;i++) graph[i]=NULL;
buildGraph(graph,n);
// printGraph(graph,n); // Comment to print out the Hasse Diagram
printLattice(graph,n,k);
return 0;
}
Showing Segementation Fault(core dumped)
Any idea why is this happening so?
Thanks in Advance
The code of formLattice() has evidences that only those vertexes are probed for lattice[level] that are connected to lattice[level-1]. Not every lattice, of course, has a total order on it (namely {1,2,3,6} doesnt). How can the present program choose the vertex 3 for lattice[2] while the vertex 2 is stored into lattice[1]?
In other words, there must be another loop, going for all already chosen vertexes in lattice from 0 to level-1, around the present loop for ptr.

Sorting the numbers in a list / C

I need to sort the numbers that are entered in a list, but I am doing something wrong and it's sorting all of them except the first one.Any ideas how to fix this?
Here's my code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node* List;
void Add (struct node* p, int d)
{
struct node* q;
q=malloc(sizeof(struct node));
if (q==NULL)
printf("Not enaugh memory!");
else
{
q->data=d;
if(List==NULL)
{
q->next=NULL;
List=q;
}
else
{
struct node *ptr=List;
while((ptr->next!=NULL)&&(ptr->next->data>d))
{
ptr=ptr->next;
}
q->next=ptr->next;
ptr->next=q;
}
}
}
int main()
{
int n,i,a;
printf("How much numbers are you going to enter? ");
scanf("%d",&n);
for (i=1; i<=n; i++)
{
printf("\nEnter a number: ");
scanf("%d",&a);
Add(List,a);
}
printf("\nThe sorted numbers are: ");
struct node *ptr=List;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
printf("\n\n");
system("PAUSE");
return 0;
}
Thanks for the help in advance :-)
in add() function,
if(List==p)
this statement is true for all elements you insert to list since the call to add is,
Add(List,a);
so p=List. therefore the sorting code written in else part is not executed.
Also add statements to check for empty initial list.
You can use code similar to this,
void Add (int d)
{
struct node* q;
q=malloc(sizeof(struct node));
if (q==NULL)
printf("Not enaugh memory!");
else
{
q->data=d;
if(List==NULL)
{
q->next=NULL;
List=q;
}
else
{
struct node *ptr=List;
while((ptr->next!=NULL)&&(ptr->next->data>d))
{
ptr=ptr->next;
}
q->next=ptr->next;
ptr->next=q;
}
}
}
Since list is a global variable you dont need to pass it to Add() function. change the function call to
Add(a);
void Add (struct node* p, int d){
struct node* q;
q=malloc(sizeof(struct node));
if (q==NULL)
printf("Not enaugh memory!");
else{
q->data=d;
if(List==NULL || List->data < d){//modify this line
q->next= List;//and change NULL to List
List=q;
} else {
struct node *ptr=List;
while((ptr->next!=NULL)&&(ptr->next->data>d)){
ptr=ptr->next;
}
q->next=ptr->next;
ptr->next=q;
}
}
}
You always call Add with List as the first parameter, so it's alway true inside Add that (List==p). Consequently each new item is just inserted at the front of the list and there is no sorting at all.
EDIT 1
A good practice would be sending a list to the Add routine as a parameter. Or, if you want to keep it external, just don't give it to Add at all and test if(List == NULL)
void Add( int d)
{
// ... alloc 'q' and fill q->d here, then:
if(List == NULL)
{
q->next = NULL;
List = q;
}
else
{
struct node *b; // put q after b
for( b = List; b->next != NULL; b = b->next)
if( b->next->data >= d)
break;
q->next = b->next;
b->next = q;
}
}
EDIT 2
Example of transferring the list to a function by parameter
void Add( struct node **n, int d)
{
// ... alloc 'q' and fill q->d here, then:
while( *n != NULL && (*n)->data < d)
n = & (*n)->next;
q->next = *n;
*n = q;
}
int main()
{
// ...
Add( &List, a);
// ...
}

Do the rules of writing a structure in C vary with Turbo C++ compiler and GCC?

I have a singly linked list program in C. When i compile it on TC++ it has only 2 errors regarding some declaration(its fine). But when i compile it in Ubuntu using GCC, it has way too many errors. I have created a custom datatype called NODE for the members of the structure, but GCC wont accept it. And as i have used typedef, there is an error which says -
expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’
Any rules i am missing out? Please help me!
This is the code:
#include<stdio.h>
typdef struct node
{
int data;
NODE *next;
}NODE;
//Creation Of the Nodes with NULL pointers
NODE* createnewnode()
{
NODE* nn;
nn=(NODE*)malloc(sizeof(NODE));
if(nn==NULL)
{
printf("Insufficient Memory");
exit(0);
}
printf("Enter data");
scanf("%d",&nn->data);
nn->next=NULL;
return(nn);
}
// Creation Of the Links
NODE* createlinkedlist(NODE *hn, int n)
{
NODE *cn, *nn;
for(i=0;i<n;i++);
{
nn=createnewnode();
if(hn==NULL)
{
hn=nn;
}
else
{
cn->next==nn;
}
cn=nn;
return(hn);
}
//Display of The Data
void display(NODE *hn)
{
NODE *cn;
for(cn=hn;cn!=NULL;cn=cn->next)
{
printf("\t %d, "cn->data);
}
}
//Linear Searching
void search(NODE *hn, int n)
{
NODE *cn;
int i, x;
printf("Enter the data to be found");
scanf("%d",&x);
i=0;
while(i<n)
{
if(x==cn->data)
{
printf("Data found at %d",i+1);
break;
}
cn=cn->next;
i=i++;
}
}
void main()
{
int n;
NODE* hn=NULL;
printf("Enter the number of nodes to be created");
scanf("%d",&n);
createlinkedlist(hn,n);
display(hn);
}
Even with corrected typedef you can't use NODE inside its own declaration. Do a forward declaration of NODE first, or even better call it just node:
typedef struct node node;
struct node {
...
node* next;
};
Edit: and other nitpicks:
don't cast the return of malloc. If you feel the need for it, you are wrong somewhere else. (Here that you don't include stdlib.h)
since long C has no default types for variables anymore. Your i in createlinkedlist is unknown and no modern compiler will let this through
you have at least one place where you use a == operator instead of an assignment =. Always compile with all warnings enabled, any decent compiler should have found that.
// correction of above asked code, but some logics of programmer are not /correct.
#include <cstdlib>
#include <iostream>
#include<stdio.h>
using namespace std;
typedef struct node // typedef spell
{
int data;
node *next; // NODE was in upper case it should be in lower case
node() //Constructor : i defined it in traditional way as extra
{
next = NULL;
data = 0;
}
}NODE;
//Creation Of the Nodes with NULL pointers
NODE* createnewnode()
{
NODE* nn;
nn=(NODE*)malloc(sizeof(NODE));
if(nn==NULL)
{
printf("Insufficient Memory");
exit(0);
}
printf("Enter data");
scanf("%d",&nn->data);
nn->next=NULL;
return(nn);
}
// Creation Of the Links
NODE* createlinkedlist(NODE *hn, int n)
{
NODE *cn, *nn;
int i; // int i, was not defined, more correction fixed
for(i=0;i<n;i++);
{
nn=createnewnode();
if(hn==NULL)
{
hn=nn;
}
else
{
cn->next==nn;
}
cn=nn;
return(hn);
}
}
//Display of The Data
void display(NODE *hn)
{
NODE *cn;
for(cn=hn;cn!=NULL;cn=cn->next)
{
printf("\t %d", cn->data);
}
}
//Linear Searching
void search(NODE *hn, int n)
{
NODE *cn;
int i, x;
printf("Enter the data to be found");
scanf("%d",&x);
i=0;
while(i<n)
{
if(x==cn->data)
{
printf("Data found at %d",i+1);
break;
}
cn=cn->next;
i=i++;
}
}
int main()
{
int n;
NODE* hn=NULL;
printf("Enter the number of nodes to be created");
scanf("%d",&n);
createlinkedlist(hn,n);
display(hn);
system("pause");
return 0;
}

Resources