繁难的二逼邻接表法 为了不拉低智商请勿模仿
#include <stdio.h>
#include <malloc.h>
#include <queue>
using namespace std;
const int MAXSIZE = 1000;
typedef int ElementType;
struct ListNode
{
ElementType Data;
ListNode *Next;
};
ListNode* CreateList(int data = -1)
{
ListNode* p = (ListNode*) malloc(sizeof(ListNode));
p->Next = NULL;
p->Data = data;
return p;
}
int Length(ListNode* const ptrL)
{
ListNode* p = ptrL;
int i = -1;
while(p)
{
p = p->Next;
i++;
}
return i;
}
ListNode* FindKth(int k, ListNode* ptrL)
{
ListNode* p = ptrL;
int i = 0;
while (p != NULL && i < k)
{
p = p->Next;
i++;
}
return p;
}
ListNode* Find(ElementType X, ListNode* ptrL)
{
ListNode* p = ptrL;
while (p != NULL && p->Data != X)
{
p = p->Next;
}
if (p == NULL)
{
//printf("%d : NOT FOUND !\n", X);
}
return p;
}
ListNode* InsertListNode(ElementType X, int i, ListNode* const ptrL)
{
if (i == 1)
{
ListNode* p = (ListNode*)malloc(sizeof(ListNode));
p->Data = X;
p->Next = ptrL->Next;
ptrL->Next = p;
return ptrL;
}
ListNode* p = FindKth(i - 1, ptrL);
if (p == NULL)
{
printf("Wrong parameter %d", i);
return p;
}
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->Data = X;
newNode->Next = p->Next;
p->Next = newNode;
return ptrL;
}
ListNode** CreateListGraph(int size)
{
ListNode** graph = (ListNode**) malloc(sizeof(ListNode*) * size);
for (int i = 0; i < size; i++)
{
graph[i] = (ListNode*) malloc(sizeof(ListNode));
graph[i]->Data = i;
graph[i]->Next = NULL;
}
return graph;
}
int FindAscendPos(int item, ListNode* list)
{
if (list == NULL)
{
printf("ERROR : NULL LIST\n");
return 0;
}
ListNode* currentNode = list->Next;
int i = 1;
while (currentNode != NULL && currentNode->Data < item)
{
i++;
currentNode = currentNode->Next;
}
return i;
}
void LinkConnect(const int& a, const int& b, ListNode** const graph)
{
InsertListNode(b, FindAscendPos(b, graph[a]), graph[a]);
InsertListNode(a, FindAscendPos(a, graph[b]), graph[b]);
}
bool IsLinkConnected(int a, int b, ListNode** graph)
{
if (graph[a] == NULL || graph[b] == NULL)
{
return false;
}
if (Find(a, graph[b]) == NULL)
{
return false;
}
return true;
}
void DFS(ListNode** graph, ListNode* graphList, bool* isVisited)
{
ListNode* currentNode = graphList;
while (currentNode != NULL)
{
// 如果该节点未被访问过
if (!isVisited[currentNode->Data])
{
isVisited[currentNode->Data] = true;
printf("%d ", currentNode->Data);
// 节点的邻接点:graph[currentNode->Data]
DFS(graph, graph[currentNode->Data], isVisited);
}
currentNode = currentNode->Next;
}
}
void BFS(ListNode** graph, ListNode* graphList, bool* isVisited)
{
queue<ListNode*> t;
t.push(graph[graphList->Data]);
isVisited[graphList->Data] = true;
while (!t.empty())
{
ListNode* currentNode = t.front();
t.pop();
printf("%d ", currentNode->Data);
while (currentNode->Next != NULL)
{
currentNode = currentNode->Next;
if (!isVisited[currentNode->Data])
{
t.push(graph[currentNode->Data]);
isVisited[currentNode->Data] = true;
}
}
}
}
void ComponentsSearch(ListNode** graph, bool* isVisited, int N, int function)
{
for (int i = 0; i < N; i++)
{
if (!isVisited[graph[i]->Data])
{
if (function == 1)
{
printf("{ ");
DFS(graph, graph[i], isVisited);
printf("}\n");
}
else
{
printf("{ ");
BFS(graph, graph[i], isVisited);
printf("}\n");
}
}
}
}
int main(void)
{
int N;
int E;
scanf("%d %d", &N, &E);
ListNode** graph = CreateListGraph(MAXSIZE);
for (int i = 0; i < E; i++)
{
int vertical;
int horizontal;
scanf("%d %d", &vertical, &horizontal);
LinkConnect(vertical, horizontal, graph);
}
bool isVisited[12];
for (int i = 0; i < 12; i++)
{
isVisited[i] = false;
}
int firstList = 0;
while (graph[firstList] == NULL)
{
firstList++;
}
ComponentsSearch(graph, isVisited, N, 1);
for (int i = 0; i < 12; i++)
{
isVisited[i] = false;
}
ComponentsSearch(graph, isVisited, N, 2);
return 0;
}