Professional Documents
Culture Documents
struct node
{
int val;
struct node * next;
};
typedef struct node * Node;
struct graph
{
int num;
Node *arr;
int *visited;
};
typedef struct graph Graph;
typedef int Vertex;
void printll(Node n)
{ while(n!= NULL){ printf("%d -> ",n->val); n = n->next; } printf("\n"); }
void print(Graph g)
{ int i=0; for (i=0; i< g.num; i++) printll(g.arr[i]); }
Graph createGraph(int numV)
{
Graph g; g.num = numV; g.arr = (Node *)malloc(sizeof(struct node)* numV);
g.visited = (int *)malloc(sizeof(int)*numV); int i;
for (i=0; i<numV; i++){
g.arr[i] = (Node)malloc(sizeof(struct node));
g.arr[i]->val = i;
g.arr[i]->next = NULL;
g.visited[i] = 0;
}
return g; }
free(temp);
return x;
}
void BFS(Graph g, Vertex v)
{
int u,level = 0;
queue q = createQueue();
enqueue(q,v);
g.visited[v] = 1;
Node temp;
while ( q->head != NULL )
{
u = q->head->val;
printf("\n%d ",u );
// g.arr[u]
temp = g.arr[u]->next;
while(temp!= NULL) {
if (g.visited[temp->val] == 0) {
g.visited[temp->val] = 1;
enqueue(q,temp->val);
}
temp = temp->next;
}
dequeue(q);
} return; }
LAB 11
typedef struct stack * Stack;
struct stack{
Vertex v;
Node nxt;
Stack next;
};
void main(){
FILE * fin = fopen("graph0.txt","r");
if(fin == NULL)
printf("error");
int a1, a2, max = -1, temp, i = 0,j, *en;
Node N = NULL, L;
while(!feof(fin)){
fscanf(fin,"%d %d\n",&a1,&a2);
temp = a1 > a2 ? a1:a2;
if(temp > max)
max = temp;
}
fseek(fin, 0, SEEK_SET);
Graph g = createGraph(++max);
while(!feof(fin)){
fscanf(fin,"%d %d\n",&a1,&a2);
g = addEdge(g,a1,a2);
}
for(i = 0; i < max; ++i){
en = getAdjacent(g,i);
if(en == NULL)
continue;
printf("%d :- ",i);
for(j = 1;j<=en[0];++j)
printf("%d ",en[j]);
printf("\n");
free(en);
}
Edge st = g->edg_list;
printf("\nEdge List : \n");
while(st != NULL){
printf("%d %d\n",st->from, st->to);
st = st->next;
}
// g = addEdge(g,0,5);
printf("\nStarting TopoSort1 : \n");
L = topoSort1(g);
while(L != NULL){
printf("%d ",L->v);
N = L;
L = L->next;
free(N);
}
L = topoSort2(g);
printf("\nStarting TopoSort2 : \n");
while(L != NULL){
printf("%d ",L->v);
N = L;
L = L->next;
free(N);
}
LAB 7
printf("\n");
i++;
}
}
char *randstring(int length)
{
static int mySeed = 25011984;
char *string = "abcdefghijklmnopqrstuvwxyz";
size_t stringLen = strlen(string);
char *randomString = NULL;
srand(time(NULL) * (length+1) + ++mySeed);
randomString = malloc(sizeof(char) * (length +1));
//randomString = myalloc(sizeof(char) * (length +1));
if (randomString)
{
short key = 0;
int n = 0;
for (n = 0;n < length;n++)
{
key = rand() % stringLen;
randomString[n] = string[key];
}
randomString[length] = '\0';
return randomString;
}
}
struct hasht
{
int elementcount;
Node *arr;
// hasht.arr = (Node)malloc(sizeof(struct node)* tsize);
// above cmd for allocation of tsize array
};
typedef struct hasht hashtable;
l = strlen(buf);
char c;
for(i=0; i<l; i++)
{
c = buf[i];
// printf(" %d ",c );
if ( c<65 || c > 122 || (c<97 && c>90) )
{
//printf("Checking %s \n",buf);
return 0;
}
}
return 1;
}
void printll(Node n)
{
printf( YEL );
while(n!= NULL)
{
printf("%d -> ",n->val);
n = n->next;
}
printf("\n");
printf(RESET);
}
}
return h;
}
if ( h.arr[x]->val == -1 )
{
h.arr[x]->val = index;
return;
}
if(strcmp(book[temp_node->val],s) == 0 && temp_node->next == NULL )
{
temp_node->val = index;
return;
}
if ( strcmp(book[temp_node->val],s) == 0 )
{
h.arr[x] = temp_node->next;
while(temp_node->next != NULL)
temp_node = temp_node->next;
temp_node->next = new_node;
return;
}
while(temp_node->next != NULL)
{
if ( strcmp(book[temp_node->val],s) != 0 )
{
if (flag)
prev_node = temp_node;
}
else
flag = 0;
temp_node = temp_node->next;
}
if (strcmp(book[temp_node->val],s) == 0)
{
temp_node->val = index;
return;
}
if (!flag)
{
Node node_to_del = prev_node->next;
prev_node->next = node_to_del->next;
node_to_del->next = NULL ;
free(node_to_del);
}
else
{
temp_node->next = new_node;
return;
}
}
if (temp == NULL)
return querying_cost;
LAB 8 trees
struct t_node
{
int val;
struct t_node * left_tree;
struct t_node * right_tree;
int height;
};
int rand_num()
{
int height(tree_node t)
{
void update_height(tree_node t)
{
if (t==NULL)
return;
update_height(t->left_tree);
t->height = height(t);
update_height(t->right_tree);
}
void printtree(tree_node t)
{
if(t==NULL)
return;
update_height(t);
printtree(t->left_tree);
printf("%d of height %d\n", t->val, t->height);
printtree(t->right_tree);
return;
}
if ( t == NULL )
{
t = temp;
// update_height(t1);
return t;
}
if (t->val == num)
return t;
else if ( t->val < num )
t->right_tree = add(t1, t->right_tree, num);
else if ( t->val > num )
t->left_tree = add(t1, t->left_tree, num);
}
}
else if ((z->val < y->val && z->val < x->val))
{
a[0] = z;
if (y->val < x->val)
{
a[1] = y;
a[2] = x;
}
else
{
a[1] = x;
a[2] = y;
}
// printf("in rotate: %d %d %d\n",a[0]->val, a[1]->val, a[2]->val );
}
return a;
}
identify_other_child(x,y,z,t);
//balancing root
// root->right and z are the same thing but different pointer
// so to change at z change root->right rather than z
if (z_parent == NULL)
{
z = b;
flag = 1;
}
else{
if ( z_parent -> val < z->val)
z_parent->right_tree = b;
else
z_parent->left_tree = b;
}
b->left_tree = a;
b->right_tree = c;
a->left_tree = t[0];
a->right_tree = t[1];
c->left_tree = t[2];
c->right_tree = t[3];
if (flag)
return z;
return root;
}
#define MaxHeight 3
#define N 7
#define size 111111
typedef struct treeNode * Element;
typedef struct iterator * Iterator;
typedef struct treeNode * TreeNode;
typedef int Boolean;
int uarr[size];
int index;
void initialize(){
int i, temp, j;
for(i = 0; i < size; ++i)
uarr[i] = i;
for(i = 0; i < size; ++i){
j = rand()%size;
temp = uarr[j];
uarr[j] = uarr[i];
uarr[i] = temp;
}
index = 0;
}
int getUniqueNum(){
if(index == size)
index = 0;
return uarr[index++];
}
struct treeNode{
int key;
Iterator it;
};
struct iterator{
Element * arr;
int pos;
int max;
};
TreeNode icreateTree(){
int depth = 0;
int n = rand()%N, i, m;
TreeNode root = (TreeNode)malloc(sizeof(struct treeNode));
root->key = getUniqueNum();
TreeNode nd = root;
if(n == 0 || depth == MaxHeight){
root->it = NULL;
return root;
}
SetN s = addN(NULL, nd, depth, n), end = s;
while(s != NULL){
nd = s->nd;
depth = s->depth;
n = s->n;
nd->it = (Iterator)malloc(sizeof(struct iterator));
nd->it->arr = (Element *)malloc(sizeof(Element)*n);
nd->it->pos = 0;
nd->it->max = n;
return root;
}
TreeNode icreateTree2(){
int depth = 0;
int m = rand()%N, i;
TreeNode root = (TreeNode)malloc(sizeof(struct treeNode));
root->key = getUniqueNum();
TreeNode nd = root;
if(m == 0 || depth == MaxHeight){
root->it = NULL;
return root;
}
nd->it = (Iterator)malloc(sizeof(struct iterator));
nd->it->arr = (Element *)malloc(sizeof(Element)*m);
nd->it->pos = 0;
nd->it->max = m;
Stack s = pushN(NULL, nd, 0, depth);
while(s != NULL){
nd = s->nd;
i = s->pos;
depth = s->depth;
s = popN_st(s);
if(i < (nd->it->max)-1)
s = pushN(s, nd, i+1, depth);
return root;
}
}
resetIt(root,0);
}