-
[ACES] 3rd week - Graph전기전자공학/프로젝트 2017. 7. 19. 20:47
사용되는 자료구조
- Graph
- Linked List
- Queue
파일:
ADT_llist.c ADT_llist.h
ADT_graph.c ADT_graph.h
ADT_queue.c ADT_queue.h
문제 - bfs를 풀기 위한 파일들
ADT_graph_bfs.c main.c
모든 파일들을 손쉽게 엮어줄 Makefile
ADT_llist.h
12345678910111213141516171819202122232425262728293031323334353637383940#ifndef ADT_LLIST#define ADT_LLIST#include <stdio.h>#include <stdlib.h>//typedef int bool;#define true 1#define false 0// List Nodetypedef struct node {void* data_ptr;struct node* next;} NODE;// Listtypedef struct {int count;NODE* front;NODE* rear;NODE* pos;int (*cmp_func)(void* x, void* y);void (*print_func)(void* x);} LLIST;//OperationsLLIST* create_list( int (*cmp_func)(void* x, void* y),void (*print_func)(void* x));bool add_node_at(LLIST* list, unsigned int index, void* data);bool del_node_at(LLIST* list, unsigned int index);void* get_data_at(LLIST* list, unsigned int index);int find_data(LLIST* list, void* search_data);LLIST* copy_list(LLIST* list);void print_all(LLIST* list, NODE* from_front);void destroy_list(LLIST* list);#endifcs ADT_llist.c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178#include "ADT_llist.h"LLIST* create_list(int (*cmp_func)(void* x, void* y),void (*print_func)(void* x)) {LLIST* list;list = (LLIST*)malloc(sizeof(LLIST));if(list) {list->front = NULL;list->rear = NULL;list->pos = NULL;list->cmp_func = cmp_func;list->print_func = print_func;list->count = 0;}return list;}void destroy_list(LLIST* list) {while(list->count != 0) {del_node_at(list, 0);//printf("deleting node... %d rest...\n", list->count);}free(list);//printf("deleted list!\n");}bool add_node_at(LLIST* list, unsigned int index,void* data) {if((list == NULL) || (index > list->count))return false;NODE* new_p;new_p = (NODE*)malloc(sizeof(NODE));if(!new_p) return false;new_p->data_ptr = data;new_p->next = NULL;if(list->count == 0) {list->front = new_p;list->rear = new_p;(list->count)++;int iter_i = 0;if(index == 0) {new_p->next = list->front;list->front = new_p;(list->count)++;return true;}iter_i++;list->pos = list->front;while(iter_i != index) {list->pos = list->pos->next;iter_i++;}if(iter_i == list->count) {list->rear->next = new_p;list->rear = new_p;(list->count)++;return true;}else {new_p->next = list->pos->next;list->pos->next = new_p;(list->count)++;return true;}}bool del_node_at(LLIST* list, unsigned int index) {if((list == NULL) || (index >= list->count))return false;if(list->count == 1) {free(list->front);list->front = NULL;list->rear = NULL;list->count = 0;return true;}int iter_i = 0;list->pos = list->front;NODE* pre = NULL;while(iter_i != index) {pre = list->pos;list->pos = list->pos->next;iter_i++;}if(index == 0) {list->front = list->pos->next;free(list->pos);list->pos = NULL;(list->count)--;return true;}if(index == (list->count - 1)) {list->rear = pre;pre->next = NULL;free(list->pos);list->pos = NULL;(list->count)--;return true;}else {pre->next = list->pos->next;free(list->pos);list->pos = NULL;(list->count)--;return true;}return false;}void* get_data_at(LLIST* list, unsigned int index) {if((list == NULL) || (index >= list->count))return NULL;list->pos = list->front;int iter_i=0;while(list->pos !=NULL) {if(iter_i == index)return list->pos->data_ptr;list->pos = list->pos->next;iter_i++;}return NULL;}int find_data(LLIST* list, void* search_data) {list->pos = list->front;int cmp_result;int iter_i=0;while(list->pos != NULL) {cmp_result = (*(list->cmp_func))(list->pos->data_ptr, search_data);if(cmp_result == 0)return iter_i;list->pos = list->pos->next;iter_i++;}return -1;}LLIST* copy_list(LLIST* list) {LLIST* copied_list;copied_list = create_list(list->cmp_func, list->print_func);if(copied_list) {int iter_i = 0;while(iter_i != list->count) {add_node_at(copied_list, iter_i,get_data_at(list, iter_i));iter_i++;}}return copied_list;}void print_all(LLIST* list, NODE* from_front) {NODE* pos = from_front;while(pos != NULL) {(*(list->print_func))(pos->data_ptr);pos = pos->next;}}cs ADT_graph.h
ADT_graph.c
ADT_queue.h
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647#ifndef ADT_QUEUE#define ADT_QUEUE#include <stdio.h>#include <stdlib.h>/*typedef int bool;#define true 1#define false 0*//* ==== Queue Node ==== */#ifndef ADT_LLISTtypedef struct node {void *data_ptr;struct node *next;} QUEUE_NODE;#endif#ifdef ADT_LLISTtypedef struct qnode {void *data_ptr;struct node *next;} QUEUE_NODE;#endif/* ==== Queue ==== */typedef struct queue{int count;QUEUE_NODE *front;QUEUE_NODE *rear;} QUEUE;/* ==== define function ==== */QUEUE* create_queue();bool enqueue (QUEUE *queue, void *in);void *dequeue (QUEUE *queue);void *queue_hook_front (QUEUE *queue);void *queue_hook_rear (QUEUE *queue);void destroy_queue (QUEUE *queue);#endifcs ADT_queue.c
ADT_graph_bfx
'전기전자공학 > 프로젝트' 카테고리의 다른 글
[컴퓨터망] UDP ping 프로젝트 (3) 2018.03.25 [ACES] 메모 (assert, (0) 2017.07.11 [ACES - C ] 데이터 형과 변수의 종류(지역변수, 전역변수, 정적변수) (0) 2017.07.11 #5-6,7 포인터 배열 (0) 2017.06.29 [Kernighan - C] #5 - 5 (0) 2017.06.29