数据结构——内核链表使用(2)

完成编辑链式队列和循环队列的编写

循环队列实现

主函数 main.c ↓↓↓↓↓

#include
#include
#include
#include"clrqueue.h"


int main(void)
{
    queue_t *pstack = NULL;

    pstack = create_queue(6);

    enter_queue(pstack, 1);
    enter_queue(pstack, 2);
    enter_queue(pstack, 3);
    enter_queue(pstack, 4);
    enter_queue(pstack, 5);
    enter_queue(pstack, 6);

    while(!is_empty_queue(pstack))
    {
        printf("%d ", quit_queue(pstack));
    
    }
    printf("\n");
    destroy_queue(&pstack);

    return 0;
}

封装函数 ↓↓↓↓↓

#include
#include
#include
#include"queue.h"


//创建
struct list_head *create_queue(void)
{
    struct list_head *ptmd = NULL;

    ptmd = malloc(sizeof(struct list_head));
    if(NULL == ptmd)
    {
        return NULL;
    }

    INIT_LIST_HEAD(ptmd);

    return ptmd;
}

//写入
int enter_queue(struct list_head *phead, int tmpdata)
{
    lsz_t *ptmd = NULL;

    ptmd = malloc(sizeof(lsz_t));

    if(NULL == ptmd)
    {
        return -1;
    }

    ptmd->sum = tmpdata;

    llist_add_tai(&ptmd->node, phead);

    return 0;
}

//读取
int quit_queue(struct list_head *phead)
{
    lsz_t *ptmd = NULL;
    int i = 0;

    if(list_empty(phead))
    {
        return -1;
    }

    ptmd = list_entry(phead->next, lsz_t, node);

    list_del(&ptmd->node);
    i = ptmd->sum;
    free(ptmd);

    return i;
}

//销毁
int destroy_queue(struct list_head **pphead)
{
    while(!list_empty(*pphead))
    {
        quit_queue(*pphead);
    }

    free(*pphead);
    *pphead = NULL;

    return 0;
}

结构体函数↓↓↓↓↓

#ifndef _LINKED_H
#define _LINKED_H

#include"list.h"

typedef struct lsz
{
    struct list_head node;
    int sum;
}lsz_t;

extern struct list_head *create_queue(void);
extern int is_empty_queue(struct list_head *phead);
extern int enter_queue(struct list_head *phead, int tmpdata);
extern int quit_queue(struct list_head *phead);
extern int destroy_queue(struct list_head **phead);

#endif

-----------------------------------------------------------------------------------分割线

链表队列实现

主函数 main.c ↓↓↓↓↓

#include
#include
#include
#include"queue.h"

int main(void)
{
    struct list_head *phead = NULL;

	phead = create_queue();

	enter_queue(phead, 1);
	enter_queue(phead, 2);
	enter_queue(phead, 3);
	enter_queue(phead, 4);
	enter_queue(phead, 5);	
	
	while (!list_empty(phead))
	{
		printf("%d ", quit_queue(phead));
	}
	printf("\n");

	destroy_queue(&phead);

	return 0;
}

封装函数 ↓↓↓↓↓

#include
#include
#include
#include"queue.h"


//创建
struct list_head *create_queue(void)
{
    struct list_head *ptmd = NULL;

    ptmd = malloc(sizeof(struct list_head));
    if(NULL == ptmd)
    {
        return NULL;
    }

    INIT_LIST_HEAD(ptmd);

    return ptmd;
}

//写入
int enter_queue(struct list_head *phead, int tmpdata)
{
    lsz_t *ptmd = NULL;

    ptmd = malloc(sizeof(lsz_t));

    if(NULL == ptmd)
    {
        return -1;
    }

    ptmd->sum = tmpdata;

    llist_add_tai(&ptmd->node, phead);

    return 0;
}

//读取
int quit_queue(struct list_head *phead)
{
    lsz_t *ptmd = NULL;
    int i = 0;

    if(list_empty(phead))
    {
        return -1;
    }

    ptmd = list_entry(phead->next, lsz_t, node);

    list_del(&ptmd->node);
    i = ptmd->sum;
    free(ptmd);

    return i;
}

//销毁
int destroy_queue(struct list_head **pphead)
{
    while(!list_empty(*pphead))
    {
        quit_queue(*pphead);
    }

    free(*pphead);
    *pphead = NULL;

    return 0;
}

结构体函数  ↓↓↓↓↓

#ifndef _LINKED_H
#define _LINKED_H

#include"list.h"

typedef struct lsz
{
    struct list_head node;
    int sum;
}lsz_t;

extern struct list_head *create_queue(void);
extern int is_empty_queue(struct list_head *phead);
extern int enter_queue(struct list_head *phead, int tmpdata);
extern int quit_queue(struct list_head *phead);
extern int destroy_queue(struct list_head **phead);

#endif

你可能感兴趣的:(数据结构,链表)