[데이터 구조] 체인 팀 (큐 의 체인 저장)

대기 열의 체인 저장 소 는 포인터 필드 와 데이터 필드 를 포함 하여 먼저 노드 를 설계 해 야 합 니 다.그리고 한 대열 전체 에 대해 한 팀 의 첫 번 째 지침 과 한 팀 의 꼬리 지침 을 설계 했다.팀 의 첫 번 째 지침 은 대열 의 첫 번 째 요 소 를 가리킨다.메모리 형식 은 다음 과 같이 설명 할 수 있 습 니 다.
typedef struct LinkNode{
	ElemType data;
	struct LinkNode *next;
}LinkNode;
typedef struct{
	LinkNode *front;
	LinkNode *rear;
}LinkQueue;

전체적인 디자인 은 다음 과 같다.
#include 
#include 
#include 
using namespace std;

typedef int ElemType;
typedef struct LinkNode{
	ElemType data;
	struct LinkNode *next;
}LinkNode;
typedef struct{
	LinkNode *front;
	LinkNode *rear;
}LinkQueue;

void InitQueue(LinkQueue &Q);			//         
bool QueueEmpty(LinkQueue &Q);			//         
void EnQueue(LinkQueue &Q,ElemType &x);	//         
bool DeQueue(LinkQueue &Q,ElemType &x);	//         

int main()
{
	
	return 0;
}
void InitQueue(LinkQueue &Q){			 
	Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));
	Q.front->next=NULL;			//               
}

bool QueueEmpty(LinkQueue &Q){		//     
	if(Q.front==Q.rear)
		return true;
	else
		return false; 
}

void EnQueue(LinkQueue &Q,ElemType &x){
	LinkNode *p;
	p=new LinkNode;					//       p  ,     
	p->data=x;
	p->next=NULL;
	Q.rear->next=p;					//      
	Q.rear=p;
}

bool DeQueue(LinkQueue &Q,ElemType &x){
	if(Q.front==Q.rear)
		return false;
	LinkNode *p;
	p=Q.front->next;
	x=p->data;
	Q.front->next=p->next;
	if(Q.rear==p)					//              
		Q.rear=Q.rear;				//       
	free(p);
	return true;
}

좋은 웹페이지 즐겨찾기