데이터 구조 (체인 대기 열) (C 언어)

4050 단어
C 언어 를 이용 하여 체인 대기 열의 기본 기능 을 실현 합 니 다.
  • 헤더 파일:
  • #ifndef _LINKQUEUE_H_
    #define _LINKQUEUE_H_
    
    #include 
    #include 
    
    #define SUCCESS   100
    #define FAILURE   101
    #define TRUE      102
    #define FALSE     103
    
    typedef int DataType;
    
    typedef struct node
    {
    	DataType data;
    	struct node *next;
    }Node;
    
    typedef struct queue
    {
    	Node *front;
    	Node *rear;
    }Queue;
    
    int QueueInit(Queue **queue);
    int QueueInsert(Queue *queue, DataType e);
    int QueueDelete(Queue *queue);
    int GetTop(Queue *queue);
    int QueueEmpty(Queue *queue);
    int QueueClear(Queue *queue);
    int QueueDestory(Queue **queue);
    
    #endif
    
  • 기능 함수:
  • #include "LinkQueue.h"
    
    int QueueInit(Queue **queue)
    {
    	if(NULL == queue)
    	{
    		return FAILURE;
    	}
    	
    	*queue = (Queue *)malloc(sizeof(Queue));
    	if(NULL == *queue)
    	{
    		return FAILURE;
    	}
    	
    	Node *p = (Node *)malloc(sizeof(Node));
    	if(NULL == p)
    	{
    		return FAILURE;
    	}
    	p->next = NULL;
    	
    	(*queue)->front = p;
    	(*queue)->rear = p;
    	
    	return SUCCESS;
    }
    
    int QueueInsert(Queue *queue, DataType e)
    {
    	if(NULL == queue)
    	{
    		return FAILURE;
    	}
    	
    	Node *p = (Node *)malloc(sizeof(Node));
    	if(NULL == p)
    	{
    		return FAILURE;
    	}
    	
    	p->data = e;
    	p->next = NULL;
    	
    	queue->rear->next = p;
    	queue->rear = p;
    	
    	return SUCCESS;
    }
    
    int QueueDelete(Queue *queue)
    {
    	if(NULL == queue || queue->front->next == NULL)
    	{
    		return FAILURE;
    	}
    	
    	DataType e = queue->front->next->data;
    	Node *p = queue->front->next;
    	queue->front->next = p->next;
    	free(p);
    	
    	if(queue->front->next == NULL)
    	{
    		queue->rear = queue->front;
    	}
    	
    	return e;
    }
    
    int GetTop(Queue *queue)
    {
    	if(NULL == queue || queue->front->next == NULL)
    	{
    		return FAILURE;
    	}
    	
    	return queue->front->next->data;
    }
    
    int QueueEmpty(Queue *queue)
    {
    	if(NULL == queue)
    	{
    		return FAILURE;
    	}
    	
    	return (queue->rear == queue->front) ? TRUE : FALSE;
    }
    
    int QueueClear(Queue *queue)
    {
    	if(NULL == queue)
    	{
    		return FAILURE;
    	}
    	
    	Node *p = queue->front->next;
    	while(p)
    	{
    		queue->front->next = p->next;
    		free(p);
    		p = queue->front->next;
    	}
    	
    	queue->rear = queue->front;
    	
    	return SUCCESS;
    }
    
    int QueueDestory(Queue **queue)
    {
    	if(NULL == queue || *queue == NULL)
    	{
    		return FAILURE;
    	}
    	
    	Node *p = (*queue)->front->next;
    	while(p)
    	{
    		(*queue)->front->next = p->next;
    		free(p);
    		p = (*queue)->front->next;
    	}
    	
    	(*queue)->rear = (*queue)->front;
    	free((*queue)->rear);
    	free(*queue);
    	
    	return SUCCESS;
    }
    
  • 테스트 주 함수:
  • #include "LinkQueue.h"
    
    int main()
    {
    	Queue *queue = NULL;
    	int ret, i;
    	
    	ret = QueueInit(&queue);
    	if(SUCCESS == ret)
    	{
    		printf("Init Success.
    "); } else { printf("Init Failure.
    "); } for(i = 0; i < 9; i++) { ret = QueueInsert(queue, i); if(SUCCESS == ret) { printf("QueueInsert %d Success.
    ", i); } else { printf("QueueInsert %d Failure.
    ", i); } } for(i = 0; i < 4; i++) { ret = QueueDelete(queue); if(FAILURE == ret) { printf("QueueDelete Failure.
    "); } else { printf("QueueDelete %d Success.
    ", ret); } } ret = GetTop(queue); if(FAILURE == ret) { printf("Get Top Failure.
    "); } else { printf("Queue Top is %d.
    ", ret); } ret = QueueEmpty(queue); if(TRUE == ret) { printf("Queue is Empty.
    "); } else if(FALSE == ret) { printf("Queue is not Empty.
    "); } else if(FAILURE == ret) { printf("ERROR.
    "); } ret = QueueClear(queue); if(SUCCESS == ret) { printf("QueueClear Success.
    "); } else { printf("QueueClear Failure.
    "); } ret = QueueDestory(&queue); if(SUCCESS == ret) { printf("QueueDestory Success.
    "); } else { printf("QueueDestory Failure.
    "); } return 0; }

    좋은 웹페이지 즐겨찾기