순환 대기열front==rear
순환 대열이 가득 차거나 비어 있는 조건은 모두front==rear인데 이 두 사람 사이의 차이를 어떻게 구분합니까?
시나리오 1:
분석: 이 순환 수열은 데이터 그룹, 헤더 포인터front, 팀의 요소 개수count 필드를 포함합니다.처음에는 front와count를 모두 0으로 설정합니다.대열이 비어 있는 조건은count0입니다.팀이 꽉 찬 조건은countmaxsize입니다.코드는 다음과 같습니다.
class SqQueueClass2
{
const int MaxSize=5;
private string[] data; //
private int front; //
private int count; //
public SqQueueClass2() // ,
{
data=new string [MaxSize];
front=0;
count=0;
}
}
//----------------- -----------------
// -
public bool QueueEmpty()
{
return (count==0);
}
//
public bool enQueue(string e)
{
int rear;
rear=(front+count)%MaxSize;
if(count==MaxSize) //
Return false;
rear=(rear+1)%MaxSize;
data[rear]=e;
count++;
return true;
}
//
public bool deQueue()
{
if (count==0)
return false;
front=(front+1)%MaxSize;
e=data[front];
count--;
return true;
}
//
public int GetCount()
{
return count;
}
시나리오 2:
front와rear를 각각 대열의 머리와 대열의 끝 지침으로 하고, 또 하나의 표지tag로 대열이 비어 있을 수도 있고, 가득 찰 수도 있으며, frontrear는 대열이 비어 있거나 꽉 차는 조건으로 사용할 수 있다.Tag 초기값은 0으로 설정하고 입대에 성공할 때마다 Tag=1, 출대에 성공할 때마다 Tag=0입니다.frontrear & &tag1은 입대 작업 후front=rear를 나타낸다. 이때 팀이 가득 차면 같은 이치로frontrear & &tag=0이면 팀이 비어 있음을 나타낸다.코드는 다음과 같습니다.
namespace ConsoleApplication
{
class sqQueueClass
{
const int MaxSize = 100;
public string[] data;
public int front, rear;
public int tag = 0;
public sqQueueClass()
{
data = new string[MaxSize];
front = rear = -1;
}
//
public bool QueueEmpty()
{
return (front == rear && tag == 0);
}
//
public bool enQueue(string e)
{
if (front == rear && tag == 1)
return false;
rear = (rear + 1) % MaxSize;
data[rear] = e;
tag = 1;
return true;
}
//
public bool deQueue(ref string e)
{
if (front == rear && tag == 0)
return false;
front = (front + 1) % MaxSize;
e = data[front];
tag = 0;
return true;
}
}
}
비교:
방안1의 판단 조건은 비교적 간단하지만 저장 공간을 낭비하고count 변수로 대기열에 있는 요소의 개수를 직접 알 수 있다.방안2는 저장 공간을 충분히 이용할 수 있지만 출대 입대 작업을 할 때 매번 tag에 대해 재정의를 해야 한다. 이외에 방안2 판단 조건도 비교적 복잡하다.
요약:
비순환 대기열의 가짜 넘침 현상과 수조의 저장 공간을 충분히 사용할 수 있도록 수조의 전단과 후단을 연결하여 순환 대기열을 형성한다.순환 대열은 입대 출대 조작을 실현할 때 알고리즘의 실현 순서에 주의해야 한다. 입대할 때 먼저 대열이 꽉 찼는지 판단하고 출대할 때 대열이 비어 있는지 판단한 다음에 상응하는 입대 출대 조작을 해야 한다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.