Q2.5

 
 

编程之美里很tricky的解法明天去看一看

#include <iostream>
using namespace std;

typedef struct Node
{
	char data;
	struct Node* next;
}Node, *pNode;

pNode Init(char* a, int len)
{
	int i;
	pNode head, p, q;
	head = new Node();
	head->data = a[0];
	q = head;
	
	for (i = 1; i < len; ++i)
	{
		p = new Node();
		p->data = a[i];
		q->next = p;
		q = p;
	}
	q->next = NULL;
	
	return head;
}

void printList(pNode p)
{
	while(p)
	{
		cout<<p->data<<" ";
		if (p->next)
		{
			p = p->next;
		}
		else
		{
			cout<<endl;
			break;
		}
	}
}

pNode findCircle(pNode p , int bitmap)
{
	while(p)
	{
		if (bitmap & (1 << (p->data - 'A')))
		{
			return p;
		}
		bitmap |= (1 << (p->data - 'A')); 
		p = p->next;
	}
	return NULL;
}

int main(void)
{
	pNode head, re;
	int bitmap = 0;
	//char a[] = "ABCDEC";//
	char a[] = {'A', 'B', 'C', 'D', 'E', 'C'};
	head = Init(a, sizeof(a) / sizeof(char));
	//head = Init(a, strlen(a));
	printList(head);

	re = findCircle(head, bitmap);
	if (re)
	{
		cout<<re->data<<endl;
	}
	else
		cout<<"不存在circle"<<endl;

	return 0;
}


你可能感兴趣的:(Q2.5)