编程语言
首页 > 编程语言> > 《数据结构和算法:链表实现队列》

《数据结构和算法:链表实现队列》

作者:互联网

写出先入先出队列的结构体Queue定义,并实现以下函数:(15分)

1,Queue *create(int size):创建一个能放size个int类型的队列,返回队列的指针。

2,int put(Queue *queue, int value):将value入队,返回0表示成功,-1表示出错。

3,int get(Queue *queue, int *pvalue):将数据出队并存入pvalue,返回0表示成功,-1表示出错。

typedef struct node_t
{
    int data;
    struct node_t *next;
}nodeList;

typedef struct queue_t
{
    nodeList *head;
    nodeList *end;
    int qSize;
}Queue;
Queue *create(int size)
{
    Queue *queue = NULL;
    nodeList  *node = NULL;
    int i = 0;

    queue = (Queue *)malloc(sizeof(Queue));
    if(NULL == queue)
    {
        perror("queue malloc:");
        return NULL;
    }
    memset(queue,0,sizeof(Queue));

    node  =  (nodeList *)malloc(sizeof(nodeList));
    if(NULL == node)
    {
        perror("node malloc:");
        return NULL;
    }

    memset(node,0,sizeof(nodeList));

    queue->head = node;
    queue->end = node;

    for(i=1;i<size;i++)
    {
        node = (nodeList *)malloc(sizeof(nodeList));
        if(NULL == node)
        {
            perror("node malloc:");
            return queue;
        }
        memset(node,0,sizeof(nodeList));
        node->data = i;
        queue->end->next = node;
        queue->end = node;
    }
    
        queue->qSize = size;

    return queue;

}
int put(Queue *queue, int value)
{
    nodeList *node = NULL;

    node = (nodeList *)malloc(sizeof(nodeList));
    if(NULL == node)
    {
        perror("malloc:");
        return -1;
    }
    memset(node,0,sizeof(nodeList));

    node->data = value;

    queue->qSize++;

    if(NULL == queue->head)
    {
        queue->head = node;
        queue->end = node;      
    }
    else
    {
        queue->end->next = node;
    }

    return 0;
}
int get(Queue *queue, int *pvalue)
{
    nodeList *node = NULL;

    if(queue->qSize == 0)
    {
        printf(" queue is empty \n");
        *pvalue = -1;
        return -1;
    }

    *pvalue = queue->head->data;
    node = queue->head->next; 
    free(queue->head);
    queue->head = node;

    queue->qSize--;

    return 0;
}

 

测试:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


typedef struct node_t
{
    int data;
    struct node_t *next;
}nodeList;

typedef struct queue_t
{
    nodeList *head;
    nodeList *end;
    int qSize;
}Queue;

Queue *create(int size)
{
    Queue *queue = NULL;
    nodeList  *node = NULL;
    int i = 0;

    queue = (Queue *)malloc(sizeof(Queue));
    if(NULL == queue)
    {
        perror("queue malloc:");
        return NULL;
    }
    memset(queue,0,sizeof(Queue));

    node  =  (nodeList *)malloc(sizeof(nodeList));
    if(NULL == node)
    {
        perror("node malloc:");
        return NULL;
    }

    memset(node,0,sizeof(nodeList));

    queue->head = node;
    queue->end = node;

    for(i=1;i<size;i++)
    {
        node = (nodeList *)malloc(sizeof(nodeList));
        if(NULL == node)
        {
            perror("node malloc:");
            return queue;
        }
        memset(node,0,sizeof(nodeList));
        node->data = i;
        queue->end->next = node;
        queue->end = node;
    }
    
    queue->qSize = size;

    return queue;

}

int put(Queue *queue, int value)
{
    nodeList *node = NULL;

    node = (nodeList *)malloc(sizeof(nodeList));
    if(NULL == node)
    {
        perror("malloc:");
        return -1;
    }
    memset(node,0,sizeof(nodeList));

    node->data = value;

    queue->qSize++;

    if(NULL == queue->head)
    {
        queue->head = node;
        queue->end = node;      
    }
    else
    {
        queue->end->next = node;
    }

    return 0;
}

int get(Queue *queue, int *pvalue)
{
    nodeList *node = NULL;

    if(queue->qSize == 0)
    {
        printf(" queue is empty \n");
        *pvalue = -1;
        return -1;
    }

    *pvalue = queue->head->data;
    node = queue->head->next; 
    free(queue->head);
    queue->head = node;

    queue->qSize--;

    return 0;
}

int getQueueSize(Queue *queue)
{
    return queue->qSize;
}

int test_queue(void)
{
    Queue *queue = NULL;
    int data = -1;
    int initSize = 5;

    queue = create(initSize);

    printf("size : %d \n",getQueueSize(queue));    //size : 5

    for(int i=0;i<initSize+1;i++)
    {
        get(queue,&data);
        printf("data : %d \n",data);      //0 1 2 3 4    queue is empty -1
    }

    printf("size : %d \n",getQueueSize(queue));   // 0

    put(queue,10);
    printf("size : %d \n",getQueueSize(queue));   //1

    put(queue,11);
    printf("size : %d \n",getQueueSize(queue));   //2

    get(queue,&data);
    printf("data : %d \n",data);                  //10

    get(queue,&data);
    printf("data : %d \n",data);                 //11

    printf("size : %d \n",getQueueSize(queue));  //0

}

int main(void)
{
    test_queue();
}

 

标签:node,queue,NULL,nodeList,队列,Queue,链表,int,数据结构
来源: https://www.cnblogs.com/xuxianshen/p/14196666.html