You are on page 1of 3

#include <stdio.

h>
#include <stdlib.h>

typedef struct node {


int data;
struct node *next;
} nod;

typedef struct {
nod *top;
}stack;

typedef struct {
nod *head, *tail;
}queue;

// VARIABILE GLOBALE
nod **N;
int n;

// creare nod si returnare adresa nod


nod *nod_new(int destination, nod *next){
nod *newNode;
newNode=(nod *)malloc(sizeof(nod));
newNode->destination=destination;
newNode->next=next;
return newNode;
}
// functie creare lista adiacenta de noduri
void create_nod(int source, int destination){
nod *newnode;
nod **p;
newnode=nod_new(destination, NULL);
p = N+(source-1);
if(*p == NULL ){
*p=newnode;
}
else {
while (*p->next != NULL){
*p= *p ->next;
}
*p->next= newnode;
}
}

// initializare stiva
void stack_init(stack *s){
s->top=NULL;
}

int stack_empty(stack *s){


return s->top==NULL;
}

void stack_push(stack *s, int data){


nod *newNode;
newNode=nod_new(data,s->top);
s->top=newNode;
}

int stack_top(stack *s){


if(stack_empty(s)){
return 0;
}
return s->top->data;
}

int stack_pop(stack *s){


nod *top;
int topData;
if(stack_empty(s)) {
return 0;

}
top=s->top;
topData=top->data;
s->top=s->top->next;
free(top);
return topData;
}

void DFS (int position){

stack s;
int data;
int *visited = create_visited_array(n);
nod *p;

stack_init(&s);
stack_push(&s,position);
while (!stack_empty(&s)){
data=stack_top(&s);

if (visited[data-1]==NULL){
printf("%d\n",data );
visited[data-1]=1;

p= *(N+(data-1));
while (p && visited[p->data-1]){
p=p->next;

if(p){
stack_push(&s,p->data)
}
else {
stack_pop(&s);
}
// eliberam tabloul
free (visited);
}
}

// FUNCTII PRELUCRARE CU COADA


void queue_init(queue *q){
q->head= q->tail = NULL;
}

int queue_empty(queue *q){


return q->head==NULL;
}

void queue_enqueue(queue *q, int data){


nod *newNode;
newNode=nod_new(data,NULL);
if (queue_empty(q)){
q->head=q->tail=newNode;
}
else {
q->tail->next=newNode;
q->tail=newNode;
}
}

int queue_dequeue (queue *q){


nod *head;
int headData;
if(queue_empty(q)) {
return 0;
}
head=q->head;
headData = head->data;
if(head->next){

free(visited);

int main (){

return 0;
}

You might also like