대기열의 체인 구현 (c 언어)

카탈로그
  • (1) 대기열의 체인 구조체
  • (둘) 빈 대기열 만들기
  • (3)입고
  • (4)판공
  • (5)대장
  • (6) 빈 대기열 만들기
  • (7)출전
  • (8) 출력
  • (9) 호출된 주함수
  • 모든 조합 코드
  • (1) 대열의 체인 구조체
    #define EleType int 
    #define Status int
    
    typedef struct QueueNode
    {
    	EleType e;
    	struct QueueNode* next;
    }QueueNode,*LinkQueuePoi;
    
    typedef struct LinkQueue
    {
    	LinkQueuePoi front;
    	LinkQueuePoi rear;
    }LinkQueue;
    

    (2) 빈 대기열 만들기
    Status InitLinkQueue(LinkQueue* queue)
    {
    
    	if (!queue)
    	{
    		return ERROR;
    	}
    	QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
    	node->next = NULL;
    	queue->front = queue->rear = node;
    	return OK;
    }
    

    (3) 입고
    Status CleaerLinkQueue(LinkQueue* queue)
    {
    
    	if (!queue)
    	{
    		return ERROR;
    	}
    	//    
    	if (queue->front == queue->rear)
    	{
    		return ERROR;
    	}
    	QueueNode* node = queue->front->next;
    	while (node)
    	{
    
    		queue->front->next = node->next;
    		if (queue->rear == node)
    		{
    			queue->rear = queue->front;
    		}
    		free(node);
    		node = queue->front->next;
    	}
    	return OK;
    }
    

    (4) 판정 공백
    Status EmptyLinkQueue(LinkQueue* queue)
    {
    	//   
    	if (!queue)
    	{
    		return ERROR;
    	}
    	//    
    	if (queue->front == queue->rear)
    	{
    		return TRUE;
    	}
    	return FALSE;
    }
    
    

    (5) 대장
    int LengthLinkQueue(LinkQueue* queue)
    {
    	//   
    	if (!queue)
    	{
    		return ERROR;
    	}
    	//    
    	if (queue->front == queue->rear)
    	{
    		return 0;
    	}
    	QueueNode* node = queue->front->next;
    	int num = 0;
    	while (node)
    	{
    		node = node->next;
    		num++;
    	}
    	return num;
    }
    

    (6) 빈 대기열 만들기
    Status AddQueue(LinkQueue* queue,EleType e)
    {
    	
    	if (!queue)
    	{
    		return ERROR;
    	}
    	QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
    	if (!node)
    	{
    		return ERROR;
    	}
    	node->next = NULL;
    	node->e = e;
    	queue->rear->next = node;
    	queue->rear = node;
    	return OK;
    }
    

    (7) 출대
    Status DelQueue(LinkQueue* queue, EleType *e)
    {
    
    	if (!queue)
    	{
    		return ERROR;
    	}
    
    	QueueNode* node = queue->front->next;
    	*e = node->e;
    	queue->front->next = node->next;
    	if (node = queue->rear)
    	{
    		queue->rear = queue->front;
    	}
    	return OK;
    }
    

    (8) 출력
    void PrintfLinkQueue(LinkQueue* queue)
    {
    	if (!queue)
    	{
    		return;
    	}
    	QueueNode* node = queue->front->next;
    	while (node)
    	{
    		printf("%d,", node->e);
    		node = node->next;
    	}
    	printf("
    "
    ); return; }

    (9) 호출된 주함수
    int main(int argc, char *argv[])
    {
    	LinkQueue queue;
    	InitLinkQueue(&queue);
    	AddQueue(&queue, 1);
    	AddQueue(&queue, 2);
    	AddQueue(&queue, 3);
    	AddQueue(&queue, 4);
    	printf("       :%d
    "
    ,LengthLinkQueue(&queue)); printf(" :
    "
    ); PrintfLinkQueue(&queue); int e1, e2; DelQueue(&queue, &e1); DelQueue(&queue, &e2); printf(" :%d,%d
    "
    , e1, e2); printf(" :
    "
    ); PrintfLinkQueue(&queue); printf(" :%d
    "
    , LengthLinkQueue(&queue)); CleaerLinkQueue(&queue); printf(" , %d,rear = %p,front=%p",LengthLinkQueue(&queue), queue.rear,queue.front); printf("
    "
    ); return 0; }

    모든 조합 코드
    #include 
    #include 
    #include 
    #include 
    #define QUEUESIZE 10
    #define ERROR 0 
    #define OK 1 
    #define TRUE 1 
    #define FALSE 0
    #define EleType int 
    #define Status int
    typedef struct QueueNode
    {
    	EleType e;
    	struct QueueNode* next;
    }QueueNode,*LinkQueuePoi;
    typedef struct LinkQueue
    {
    	LinkQueuePoi front;
    	LinkQueuePoi rear;
    }LinkQueue;
    
    Status InitLinkQueue(LinkQueue* queue)
    {
    
    	if (!queue)
    	{
    		return ERROR;
    	}
    	QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
    	node->next = NULL;
    	queue->front = queue->rear = node;
    	return OK;
    }
    
    Status CleaerLinkQueue(LinkQueue* queue)
    {
    
    	if (!queue)
    	{
    		return ERROR;
    	}
    	//    
    	if (queue->front == queue->rear)
    	{
    		return ERROR;
    	}
    	QueueNode* node = queue->front->next;
    	while (node)
    	{
    
    		queue->front->next = node->next;
    		if (queue->rear == node)
    		{
    			queue->rear = queue->front;
    		}
    		free(node);
    		node = queue->front->next;
    	}
    	return OK;
    }
    
    Status EmptyLinkQueue(LinkQueue* queue)
    {
    	//   
    	if (!queue)
    	{
    		return ERROR;
    	}
    	//    
    	if (queue->front == queue->rear)
    	{
    		return TRUE;
    	}
    	return FALSE;
    }
    
    int LengthLinkQueue(LinkQueue* queue)
    {
    	//   
    	if (!queue)
    	{
    		return ERROR;
    	}
    	//    
    	if (queue->front == queue->rear)
    	{
    		return 0;
    	}
    	QueueNode* node = queue->front->next;
    	int num = 0;
    	while (node)
    	{
    		node = node->next;
    		num++;
    	}
    	return num;
    }
    s AddQueue(LinkQueue* queue,EleType e)
    {
    	
    	if (!queue)
    	{
    		return ERROR;
    	}
    	QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
    	if (!node)
    	{
    		return ERROR;
    	}
    	node->next = NULL;
    	node->e = e;
    	queue->rear->next = node;
    	queue->rear = node;
    	return OK;
    }
    
    Status DelQueue(LinkQueue* queue, EleType *e)
    {
    
    	if (!queue)
    	{
    		return ERROR;
    	}
    
    	QueueNode* node = queue->front->next;
    	*e = node->e;
    	queue->front->next = node->next;
    	if (node = queue->rear)
    	{
    		queue->rear = queue->front;
    	}
    	return OK;
    }
    
    void PrintfLinkQueue(LinkQueue* queue)
    {
    	if (!queue)
    	{
    		return;
    	}
    	QueueNode* node = queue->front->next;
    	while (node)
    	{
    		printf("%d,", node->e);
    		node = node->next;
    	}
    	printf("
    "
    ); return; } int main(int argc, char *argv[]) { LinkQueue queue; InitLinkQueue(&queue); AddQueue(&queue, 1); AddQueue(&queue, 2); AddQueue(&queue, 3); AddQueue(&queue, 4); printf(" :%d
    "
    ,LengthLinkQueue(&queue)); printf(" :
    "
    ); PrintfLinkQueue(&queue); int e1, e2; DelQueue(&queue, &e1); DelQueue(&queue, &e2); printf(" :%d,%d
    "
    , e1, e2); printf(" :
    "
    ); PrintfLinkQueue(&queue); printf(" :%d
    "
    , LengthLinkQueue(&queue)); CleaerLinkQueue(&queue); printf(" , %d,rear = %p,front=%p",LengthLinkQueue(&queue), queue.rear,queue.front); printf("
    "
    ); return 0; }

    좋은 웹페이지 즐겨찾기