llist.cpp

来自「判断链表中是否有循环 有的话」· C++ 代码 · 共 44 行

CPP
44
字号
#include "LList.h"
#include <iostream>
using namespace std;

bool LList:: append(int elem){
	tail=tail->next=new Node(elem);
	return true;
}

void LList::print(){
	Node *temp;
	temp = fence;
	do
	{
		cout << temp->e <<" ";			
		temp = temp ->next;	
	}while (temp!=fence);
}
////////////判断是否循环/////////
bool LList::circleInList()   
  {   
	  if(head == NULL || head->next == NULL)  
	  {   
		return false;   
	  }   
	  if(head->next == head)
	  {   
		  fence = head;
		  return true;   
	  }   
	  Node *pTemp1 = head;       //step   1   
	  Node *pTemp = head->next;  //step   2   
	  while(pTemp != pTemp1 && pTemp != NULL && pTemp->next !=  NULL)   
	  {   
		  pTemp1 = pTemp1->next;   
		  pTemp = pTemp->next->next;   
	  }   
	  if(pTemp == pTemp1)   
	  {   
		  fence = pTemp;
		  return true;   
	  }   
	  return false;   
  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?