DFS in an adjacency list to find topological ordering - c

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;
}

Related

The following program below works but a dialog box appears saying that the filename.exe has stopped working. Why so?

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");
}

Passing a function with parameters also a function [duplicate]

This question already has answers here:
Wrong output while running C code [duplicate]
(8 answers)
Closed 5 years ago.
I came across some problems while doing my assignment. Part of this assignment involves creating a linked-list with the data given and printing them out. The data stored in the list is of datatype 'Atom', which can be an integer, a string, a "pair"(self-defined datatype), or a list made of Nodes.
If the input is (the first integer indicates the number of data)
4
1 2 3 4
it prints
1 2 3 4
What makes me feel strange is that, if I change the function read_list_helper(int n) like this
Node* read_list_helper(int n)
{
Atom *atom;
if (n == 0) return NULL;
return combine_atom_and_list(get_atom(), read_list_helper(n - 1));
}
the output becomes
4 3 2 1
Why would this happen? I have tried several keywords to search for the answer, but none of them works.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define STRING 0
#define NUM 1
#define PAIR 2
#define LIST 3
struct Atom;
typedef struct Pair
{
struct Atom *left;
struct Atom *right;
} Pair;
typedef struct Node
{
struct Atom *data;
struct Node *next;
} Node;
typedef struct Atom
{
int dtype;
int val = 0;
char str[21] = {'\0'};
Pair *pair = NULL;
Node *Node = NULL;
} Atom;
Node* read_list();
Node* read_list_helper(int);
Atom* get_atom();
Node* combine_atom_and_list(Atom*, Node*);
void print_atom(Atom*);
void print_pair(Pair*);
void print_list(Node*);
int main()
{
Node *head1;
head1 = read_list();
print_list(head1);
system("pause");
return 0;
}
Node* read_list()
{
int n;
scanf("%d", &n);
return read_list_helper(n);
}
Node* read_list_helper(int n)
{
Atom *atom;
if (n == 0) return NULL;
atom = get_atom();
return combine_atom_and_list(atom, read_list_helper(n - 1));
}
Atom* get_atom()
{
int num;
char str[21];
int i;
Atom* res = (Atom*)malloc(sizeof(Atom));
if (scanf("%d", &num) == 1)
{
res->dtype = NUM;
res->val = num;
}
else
{
scanf(" %20s", str);
res->dtype = STRING;
strncpy(res->str, str, 20);
}
return res;
}
Node* combine_atom_and_list(Atom* atom, Node* node)
{
Node *res = (Node*)malloc(sizeof(Node));
res->data = atom;
res->next = node;
return res;
}
void print_atom(Atom* atom)
{
if (atom == NULL) return;
switch (atom->dtype)
{
case NUM:
printf("%d", atom->val);
break;
case STRING:
printf("%s", atom->str);
break;
case PAIR:
print_pair(atom->pair);
break;
case LIST:
print_list(atom->Node);
break;
default:
break;
}
}
void print_pair(Pair* pair)
{
print_atom(pair->left);
print_atom(pair->right);
}
void print_list(Node* node)
{
print_atom(node->data);
if (node->next != NULL)
{
printf(" ");
print_list(node->next);
}
}
Function arguments are not evaluated in any particular order. Since one of your arguments does I/O, the order of evaluation affects the order of the inputs.

find function in linked list in c programming

I wanna write a program about linked list that gets a number and if the number was equal to the one of the nodes' data , gives the number of that node .
like the datas in 3 nodes are
123
56
78
and it gets a number like 56 and its equal to the second node's data so the output should be 2.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
node *next;
};
node* cnode();
void find(node *first,int i,int d);
int main()
{
node *first,*last;
int n,i,x,d;
printf(" How many nodes ?\t");
scanf("%d",&x);
for(i=1;i<=x;i++){
last=cnode();
last->next=first;
first=last;
}
printf("\n enter a particular data:\t");
scanf("%d",&d);
printf("\n number of the particular node:\t");
find(first,i,d);
}
void find(node *first,int i,int d){
int count=0;
while (first != NULL)
{
if (first->data == d)
count++;
first = first->next;
}
if(count != 0){
printf("%d",count);
}
if(count == 0){
printf("\n NOT FOUND ! ");
}
}
According to the C Standard the function main without parameters shall be declared like
int main( void )
Secondly this declaration of the structure
struct node{
int data;
node *next;
};
is not a valid C declaration.
You should write
struct node{
int data;
struct node *next;
};
typedef struct node node;
or
typedef struct node{
int data;
struct node *next;
} node;
You have to initialize at least node first with NULL. Otherwise the program will have undefined behavior.
node *first = NULL,*last;
The parameter i is not used within the function find. So it may be removed.
void find(node *first, int d);
The function definition can look at least like
void find( node *first, int d )
{
int count = 0;
while ( first != NULL && first->data != d )
{
first = first->next;
++count;
}
if ( first != NULL )
{
printf("%d",count);
}
else
{
printf("\n NOT FOUND ! ");
}
}

Random graph with structures

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
...

How to know avl node status in C?

This exercise is not really new to me, but it still is quite advance for me. Being a student, I would like to kindly ask for your help regarding this matter.
I need to understand, not just know, how AVL node deletion works. I've been given this C code that I have to complete to demonstrate how AVL insertion and deletion happens. I've done the basics. It's just the balancing and the rotation that got me dizzy.
Here's the main C file:
#include<stdio.h>
#include<stdlib.h>
#include "avl.h"
#define N 10
#define BALANCED 0
#define LEFT_LEANING 1
#define RIGHT_LEANING 2
int main(){
AVL *root=NULL;
int choice=0,x;
while((choice=menu())!=3){
printf("Value: ");
scanf("%i",&x);
switch(choice){
case 1: insert_value(&root,x);
view(root,2);
break;
case 2: delete_value(&root,x);
view(root,2);
}
}
}
here's the avl header file:
#define N 10
#define BALANCED 0
#define LEFT_LEANING 1
#define RIGHT_LEANING 2
//structure for an avl_node
typedef struct node_tag{
int x, height;
struct node_tag *parent;
struct node_tag *left;
struct node_tag *right;
}avl_node;
//print menu and get user choice
int menu(){
int choice;
printf("\n MENU ");
printf("\n[1] - Insert");
printf("\n[2] - Delete");
printf("\n[3] - Exit");
printf("\nYour choice: ");
scanf("%i",&choice);
return choice;
}
//find maximum of 2 numbers
int max(int a,int b){
return(a>b?a:b);
}
//finds the minimum value of the BST
avl_node* minimum(avl_node *rootptr){
if(rootptr!=NULL){
while(rootptr->left!=NULL) rootptr=rootptr->left;
}
return (rootptr);
}
//update height of a given node
void updateheight(avl_node *temp){
if(temp!=NULL)
temp->height = max(temp->left==NULL?-1:temp->left->height,temp->right==NULL?-1:temp->right->height)+1;
}
//left rotate the subtree
void left_rotate(avl_node **rootptr){
}
//right rotate the subtree
void right_rotate(avl_node **rootptr){
}
//determine rotation of node/s
void insert_fixup(avl_node **rootptr, avl_node *temp){
}
//insert value and create node
void insert_value(avl_node **rootptr, int x){
avl_node *temp;
temp = (avl_node *)malloc(sizeof(avl_node));
temp->x = x;
temp->height = 0;
temp->parent = temp->left = temp->right = NULL;
insert_node(rootptr,temp);
insert_fixup(rootptr,temp);
}
void view(avl_node *root, int tabs){
int i;
if(root != NULL){
view(root->right,tabs + 1);
for(i=0;i<tabs;i++) printf("\t");
printf("%2i\n",root->x);
view(root->left,tabs+1);
}
}//view the AVL
void swap(int *a, int *b){
int temp;
temp = *a; *a = *b; *b = temp;
}//swap values
//look for the successor of a node
avl_node* successor(avl_node *rootptr){
if(rootptr==NULL) return (rootptr);
else if(rootptr->right!=NULL) return (minimum(rootptr->right));
while(rootptr->parent!=NULL){
if(rootptr==rootptr->parent->right) rootptr=rootptr->parent;
else break;
}
return (rootptr->parent);
}
//function for searching the location of the node
avl_node* search(avl_node *root,int x){
if(root==NULL || root->x==x) return root;
else{
if(root->x>x) return(search(root->left,x));
else return(search(root->right,x));
}
}
//function for searching the value of the node
int search_node(avl_node *root, int x){
if(root==NULL) return 0;
if(root->x==x) return 1;
else{
if(root->x>x) return(search_node(root->left,x));
else return(search_node(root->right,x));
}
}
//function for deleting a node
void delete_value(avl_node** root,int x){
}
To understand, You should go through the Wiki Page and also try to understand pseudo code to understand node insertion and deletion in AVL tree. Also you can go through Presentation PPT to understand AVL tree. And to find status of your node from tree you can use search() function.

Resources