#include
#include
struct linkedList
{
struct node* head;
struct node* tail;
int size;
};
struct node
{
int content;
struct node* next;
};
void enqueue(int n,struct linkedList* q)
{
struct node* new = malloc(sizeof(struct node));
new->content = n;
new->next = 0;
if(!q->size)
{
q->head = new;
q->tail = new;
}
else
{
q->tail->next = new;
q->tail = new;
}
q->size++;
}
void push(int n,struct linkedList* s)
{
struct node* new = malloc(sizeof(struct node));
new->content = n;
new->next = s->head;
s->head = new;
s->size++;
}
int pop(struct linkedList* s)
{
struct node* t = s->head;
int temp = t->content;
s->head = s->head->next;
s->size--;
free(t);
if(!s->size)
s->tail = 0;
return temp;
}
struct linkedList* newLinkedList()
{
struct linkedList* r = malloc(sizeof(struct linkedList));
r->size = 0;
return r;
}
char isEmpty(struct linkedList* s)
{
return !s->size;
}
void print(struct lminkedList l)
{
struct node* t = l.head;
while(t)
{
printf("%d ",t->content);
t = t->next;
}
}
void reGround(struct linkedList*l)
{
l->head = 0;
l->tail = 0;
l->size = 0;
}
//Depth first search using stack and adjacency list
void dfsStackAdList(struct linkedList** adlist,int first,int n)
{
int i;
char* v = (char*)malloc(n);
struct node* t;
for(i=0;ihead;
while(t)
{
if(!v[t->content])
push(t->content,stack);
t = t->next;
}
}
}
//Breadth first search using stack and adjacency list
void bfsAdList(struct linkedList** adlist,int first,int n)
{
int i;
char* v = (char*)malloc(n);
struct node* t;
for(i=0;ihead;
while(t)
{
if(!v[t->content])
enqueue(t->content,queue);
t = t->next;
}
}
}
//DFS for adjacency list using recursion
void dfsRecAdList(struct linkedList** adlist,int target,int n,char* v)
{
struct node* t;
v[target] = 1;
printf("%d ",target);
t = adlist[target]->head;
while(t)
{
if(!v[t->content])
dfsRecAdList(adlist,t->content,n,v);
t = t->next;
}
}
void dfsAdMatrixRec(char** map,int cur,char* v,int n)
{
int i;
v[cur] = 1;
printf("%d ",cur);
for(i=0;i