I m trying to unterstand the code below but there´s something not clear in DFS function
enter code here
#include<stdio.h>
typedef struct node
{
struct node *next;
int vertex;
}node;
node *G[20];
int visited[20];
int n;
void read_graph();
void insert(int,int);
void DFS(int);
void main()
{
int i;
read_graph();
//initialised visited to 0
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
}
}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
//initialise G[] with a null
for(i=0;i<n;i++)
{
G[i]=NULL;
//read edges and insert them in G[]
printf("Enter number of edges:");
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
}
}
void insert(int vi,int vj)
{
node *p,*q;
//acquire memory for the new node
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
//insert the node in the linked list number vi
if(G[vi]==NULL)
G[vi]=q;
else
{
//go to end of the linked list
p=G[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
how does the backtracking happen after terminating the while loop in the function DFS() ? i don´t get it
Thx
Well, this is not a DFS (Depth First Search), as nothing is searched for. All that your DFS function does is traverse all edges, marking their nodes as visited. Once done, you only know if this is a connected graph - if there are any edges that have not been visited, then they have not been marked, and so cannot be reached from G[0].
the original code is not mine .. i just found it on this website : http://www.thecrazyprogrammer.com/2014/03/depth-first-search-dfs-traversal-of-a-graph.html
Related
I'm trying to implement shell sort on singly-linked lists. What I have tried to do is get the respective elements based on the inc value and sort them. However, I'm only familiar with shell sort implementation on arrays and not linked lists. Any help is appreciated.
struct node
{ int data;
struct node *next;
};
typedef struct node n;
void add(int a);
void shellsort(int size);
void display();
void moveptr(n*a, int distance);
n* head=NULL;
void main()
{ int i,x,size;
printf("How many elements do you want to enter: ");
scanf("%d",&size);
printf("Enter the data: ");
for(i=0;i<size;i++)
{ scanf("%d",&x);
add(x);
}
printf("List currently is: ");
display();
shellsort(size);
printf("\nafter sorting list is: ");
display();
}
void add(int a)
{ n *temp = (n*)malloc(sizeof(n));
temp->data=a;
temp->next=head;
head=temp;
}
void display()
{ n* temp=head;
while(temp!=NULL)
{ printf("%d-->",temp->data);
temp=temp->next;
}
}
void moveptr(n *ptr, int distance) // this moves a temp pointer to required location
{ int i=0;
while(i<distance)
{
ptr=ptr->next;
i=i+1;
}
}
void shellsort(int size)
{ int i,j,temp,inc=size/2;
n *a=head,*b=head;
do{
for(i=0;i<size;i++)
{
for(j=i+inc;j<size;j+=inc)
{ b=head;
moveptr(b,j);
if(a->data > b->data)
{ temp=b->data;
b->data=a->data;
a->data=temp;
}
}
a=a->next;
}
inc=inc/2;
}while(inc>=1);
}
If you insist on using shell sort:
allocate an array of pointers to list items,
initialize this array to point to the individual items in the list
sort the array using shell sort
relink the list in the order of the array elements
free the list.
#include<stdio.h>
#include<stdlib.h>
struct node{
int id;
char name[50];
float point;
char grade;
struct node *next;
};
struct node *head;
void DataEntry(struct node *);
void GetData(struct node *);
void DataDisp();
void Insert()
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
DataEntry(p);
if(head==NULL)
{
head=p;
}
else
{
struct node *q;
q=head;
while(q!=NULL)
{
q=q->next;
}
q->next=p;
p->next=NULL;
}
}
void DataEntry(struct node *p)
{
printf("Enter value of Id employee\n");
scanf("%d",&p->id);
printf("Enter name of the person\n");
scanf("%s",p->name);
printf("Enter points got by Employee\n");
scanf("%f",&p->point);
printf("Enter grade of the person\n");
fflush(stdin);
scanf("%c",&p->grade);
}
void GetData(struct node *q)
{
printf("The data entered by you is as follows\n");
printf("Id is %d\n",q->id);
printf("Name is %s\n",q->name);
printf("Point is %f\n",q->point);
printf("Grade is %c\n",q->grade);
}
void DataDisp()
{
struct node *z;
z=head;
if(z==NULL)
{
printf("List is Empty\n");
return;
}
while(z!=NULL)
{
GetData(z);
z=z->next;
}
}
int main()
{
Insert();
DataDisp();
}
I have implemented a singly linked list, I insert data in a list using Insert() function and displaying the data using DataDisp() function in DataDisp() function I have used GetData() function which is accessing data from linked list nodes. The problem here is I can do the first insertion and it displays data also using DataDisp() but after that, there is error file.exe stop running. I think there is a segmentation fault; I tried my best to solve the problem but could not run it successfully. Please help.
ONE MORE THING IS TRY TO PREVENT USE OF "FFLUSH" KEYWORD.THIS IS NOT A PORTABLE WAY OF CLEANING THE INPUT BUFFER, AND MAY NOT WORK WITH ALL COMPILER.
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.
Sometime back I asked a question about linked list and got nice replies...Now I've written a new code using the suggestions but I've run into an error. The code is:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *mknode()
{
return malloc(sizeof(node));
}
void create(node* h, int num)
{
int i;
node *temp=h;
for(i=0;i<num;i++)
{
temp->data=i;
if(i==(num-1))
temp->next=NULL;
else
temp->next=mknode();
temp=temp->next;
}
}
node* add_end(node *h,int num)
{
node *temp;
if(h==NULL)
{
h=mknode();
temp=h;
create(h,num);
}
else
{
temp=h;
while(h!=NULL){
h=h->next;}
h=mknode();
create(h,num);
}
return temp;
}
void display(node *h)
{
node *temp=h;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
int main()
{
node *head=NULL;
int num;
scanf("%d",&num);
head=add_end(head,num);
head=add_end(head,num);
display(head);
//printf("%d",list_len(head));
free(head);
return 0;
}
Now since I've called add_end twice for an input of 3 the output should be
0->1->2->0->1->2->
But instead I'm getting
0->1->2->
I've checked this much that the FOR loop inside create function is running 2n times for an input of n.
So the problem is that display function encounters a NULL but I can't figure out where in the code is it happening.
All help appreciated.
Thanking in advance
-user1614886
In add_end() you do not link the nodes correctly.
[blah blah]
else
{
temp=h;
while(h!=NULL){
h=h->next;}
h=mknode();
create(h,num);
}
You advance h until it is NULL but you should only move until h->next == NULL and append your next list there.
Here is my program which creates a link list and also reverses it.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *list=NULL;
struct node *root=NULL;
static int count=0;
struct node *create_node(int);//function to create node
void travel_list(void);
void create_list(int);
void reverse_list(void);
int main()
{
int i, j, choice;
printf("Enter a number this will be root of tree\n");
scanf("%d", &i);
create_list(i);
printf("Enter 1 to enter more numbers \n 0 to quit\n");
scanf("%d", &choice);
while (choice!=0){
printf("Enter a no for link list\n");
scanf("%d",&i);
// printf("going to create list in while\n");
create_list(i);
travel_list();
printf("Enter 1 to enter more numbers \n 0 to quit\n");
scanf("%d", &choice);
}
printf("reversing list\n");
reverse_list();
travel_list();
}
// end of function main
void create_list (int data)
{
struct node *t1,*t2;
//printf("in function create_list\n");
t1=create_node(data);
t2=list;
if( count!=0)
{
while(t2->next!=NULL)
{
t2=t2->next;
}
t2->next=t1;
count++;
}
else
{
root=t1;
list=t1;
count++;
}
}
struct node *create_node(int data)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
// printf("create node temp->data=%d\n",temp->data);
// printf("the adress of node created %p\n",temp);
return temp;
}
void travel_list(void )
{
struct node *t1;
t1=list;
printf("in travel list\n");
while(t1!=NULL)
{
printf("%d-->",t1->data);
t1=t1->next;
}
printf("\n");
}
void reverse_list(void)
{
struct node *t1,*t2,*t3;
t1=list;
t2=list->next;
t3=list->next->next;
int reverse=0;
if(reverse==0)
{
t1->next=NULL;
t2->next=t1;
t1=t2;
t2=t3;
t3=t3->next;
reverse++;
}
while(t3!=NULL)
{
t2->next=t1;
t1=t2;
t2=t3;
list=t1;
travel_list();
t3=t3->next;
}
t2->next=t1;
list=t2;
}
Above is a fully working code.
I want to know if there can be further enhancement to the above code?
Make your indentation and whitespace usage consistent
Use meaningful identifiers rather than t1, t2 and t3
Make the data member a generic type, for example void * rather than int.
Don't use global variables, pass struct node * pointers to your functions.