simple adjacency list

자료구조 2015. 5. 22. 15:27

#include <stdio.h>

#include <stdlib.h>


struct node

{

int end;

int w;

struct node* next;

};


struct node** adjlist;

int node_cnt=5;


struct node* adjlist2[5];




struct node* newnode(int e)

{

struct node* temp= (struct node*) malloc(sizeof(struct node));

temp->end=e;

temp->next=NULL;

temp->w=1;


return temp;

}


void insert(int s,int e)

{

if (adjlist[s] == NULL)

{

adjlist[s] = newnode(e);

}

else

{

struct node* temp = adjlist[s];


while(temp->next != NULL)

{

temp=temp->next;

}


temp->next= newnode(e);

}

}

void print()

{

int i;

struct node* temp;


for (i=0;i<node_cnt;i++)

{

temp = adjlist[i];

printf ("%d : ",i);

while ( temp!=NULL )

{

printf ("%d ",temp->end);

temp=temp->next;

}

printf ("\n");

}


}


int main(void)

{

int i;


adjlist = (struct node**)malloc(sizeof(struct node*) * node_cnt );


for (i=0;i<node_cnt; i++)

{

adjlist[i] = NULL;

}


insert(0,1);

insert(1,0);


insert(0,2);

insert(2,0);


insert(1,2);

insert(2,1);


insert(3,1);

insert(1,3);


insert(4,3);

insert(3,4);


print();


}


'자료구조' 카테고리의 다른 글

hash simple linear  (0) 2015.05.22
binary search tree ( insert, kth smallest, inorder, delete by key)  (0) 2015.05.15
link list(single)  (0) 2015.05.01
boolean combination  (0) 2015.05.01
Posted by rjh
,