// Graph data structure typedefstructgraph { int numNodes; // adjacency list Node* adjLists[128]; int visited[128]; } Graph;
/** * Create a stack with no element */ Stack* create_stack() { returnmalloc(sizeof(Stack)); }
/** * Create a queue with no elment */ Queue* create_queue() { returnmalloc(sizeof(Queue)); }
/** * Create an node with a element */ Node* create_node(char value) { Node* node = malloc(sizeof(Node)); node -> data = value; node -> next = NULL; return node; }
/** * Create Graph with number of vertices * @param n number of vertices */ Graph* create_graph(int n) { Graph* graph = malloc(sizeof(Graph)); graph -> numNodes = n; /* It is not necessary graph -> adjLists = malloc(n * sizeof(Node)); graph -> visited = malloc(n * sizeof(Node)); int i; for (i = 0; i < n; i++) { // No Nodes, no visiting graph -> adjLists[i] = NULL; graph -> visited[i] = 0; } */ return graph; }
/** * Add edges to a graph * @param graph the specified graph to add * @param start start node in an edge * @param end end node in an edge */ voidadd_edge(Graph* graph, char start, char end) { // Add edge from start to end Node* node = create_node(end); node -> next = graph -> adjLists[start]; graph -> adjLists[start] = node;
// Add edge from end to start node = create_node(start); node -> next = graph -> adjLists[end]; graph -> adjLists[end] = node; }
/** * insert a value into a Stack * @param stack which stack you will insert into * @param value what you want to insert */ voidpush_s(Stack* stack, char value) { if (stack -> length != MAX_SIZE) { stack -> datas[stack -> length++] = value; } else { printf("stack overflow!!"); } }
/** * decide if a queue is full * @return {@code 1} is full <br> * {@code 0} is not full */ intis_full_q(Queue* queue) { if (queue -> rear == MAX_SIZE - 1) { return1; } else { return0; } }
/** * decide if a queue is empty * @return {@code 1} is empty * {@code 0} is not empty */ intis_empty_q(Queue* queue) { if (queue -> front == queue -> rear) { return1; } else { return0; } }
/** * decide if a stack is empty * @return {@code 1} is empty * {@code 0} is not empty */ intis_empty_s(Stack* stack) { if (stack -> length == 0) { return1; } else { return0; } }
/** * insert a value into a Queue * @param queue which queue you will insert into * @param value what you will insert */ voidpush_q(Queue* queue, char value) { if (!is_full_q(queue)) { queue -> datas[queue -> rear++] = value; } else { printf("Queue is full"); } }
/** * pop element from first * @param queue where you will pop * @return the first element */ charpop_q(Queue* queue) { if (queue -> front != queue -> rear) { returnqueue -> datas[queue -> front++]; } else { return-1; } }
/** * pop the end element of a Stack * @param stack you will get its last element * @return the last element of {@code stakc} */ charpop_s(Stack* stack) { if (stack -> length != 0) { returnstack -> datas[--stack -> length]; } else { return-1; } }