📄 有无循环.cpp
字号:
/*有一个单向链表,可能是以下两种:
<1>无循环链表
最后一个节点的next指针是NULL值
<2>有循环的链表
最后一个节点的next指针,指向链表中的某一个节点
*/
#include<iostream.h>
typedef struct SomeList
{
//int Some;
SomeList *next;
}SomeList;
bool ListType(SomeList * ListHead)
{
SomeList *Now;
SomeList *i;
Now = ListHead;
while(Now->next != NULL)
{
i = ListHead;
while(i != Now)
{
if(Now->next == i)
return 1; //ÓÐÑ­»·
i = i->next;
}
Now = Now->next;
}
return 0; //ÎÞÑ­»·
}
void main()
{
SomeList *Head = new SomeList;
Head->next = NULL;
if(ListType(Head))
cout<<"test 1: 有循环!\n";
else
cout<<"test 1: 无循环!\n";
SomeList *Tail = new SomeList;
Head->next = Tail;
Tail->next = Head;
if(ListType(Head))
cout<<"test 2: 有循环!\n";
else
cout<<"test 2: 无循环!\n";
SomeList *Middle = new SomeList;
Head->next = Middle;
Middle->next = Tail;
Tail->next = Middle;
if(ListType(Head))
cout<<"test 3: 有循环!\n";
else
cout<<"test 3: 无循环!\n";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -